Re: How to flatten only one sub list of list of lists

2017-04-01 Thread Robert L.
On 3/1/2017, Sayth Renshaw wrote:

> How can I flatten just a specific sublist of each list in a list of lists?
> 
> So if I had this data
> 
> 
> [   ['46295', 'Montauk', '3', '60', '85', ['19', '5', '1', '0 $277790.00']],
> ['46295', 'Dark Eyes', '5', '59', '83', ['6', '4', '1', '0 $105625.00']],
> ['46295', 'Machinegun Jubs', '6', '53', '77', ['6', '2', '1', '1 
> $71685.00']],
> ['46295', 'Zara Bay', '1', '53', '77', ['12', '2', '3', '3 $112645.00']]]
> 
> 
> How can I make it be
> 
> 
> [   ['46295', 'Montauk', '3', '60', '85', '19', '5', '1', '0 $277790.00'],
> ['46295', 'Dark Eyes', '5', '59', '83', '6', '4', '1', '0 $105625.00'],
> ['46295', 'Machinegun Jubs', '6', '53', '77', '6', '2', '1', '1 
> $71685.00'],
> ['46295', 'Zara Bay', '1', '53', '77', '12', '2', '3', '3 $112645.00']]
> 
> Been looking around but most solutions just entirely flatten everything.
> This was popular on SO but yeah it flattens everything  I want to be more 
> selective
> 
> def flatten(lst):
> for elem in lst:
> if type(elem) in (tuple, list):
> for i in flatten(elem):
> yield i
> else:
> yield elem
> 

[['46295', 'Montauk', '3', '60', '85', ['19', '5', '1', '0 $277790.00']],
 ['46295', 'Dark Eyes', '5', '59', '83', ['6', '4', '1', '0 $105625.00']],
 ['46295', 'Machinegun Jubs', '6', '53', '77', ['6', '2', '1', '1 $71685.00']],
 ['46295', 'Zara Bay', '1', '53', '77', ['12', '2', '3', '3 $112645.00']]].
map( &:flatten )

 ===>
[["46295", "Montauk", "3", "60", "85", "19", "5", "1", "0 $277790.00"],
 ["46295", "Dark Eyes", "5", "59", "83", "6", "4", "1", "0 $105625.00"],
 ["46295", "Machinegun Jubs", "6", "53", "77", "6", "2", "1", "1 $71685.00"],
 ["46295", "Zara Bay", "1", "53", "77", "12", "2", "3", "3 $112645.00"]]

-- 
[T]he driving force behind mass immigration is the organized Jewish community,
which feels its interests are best served by a "diverse" society divided
between antagonistic groups that can be easily dominated by their cohesive and
privileged tribe.
http://www.renseradioarchives.com/dduke/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to flatten only one sub list of list of lists

2017-03-01 Thread Sayth Renshaw

> Replace the slice row[index:index+1] with row[index], either by building a 
> new list or in place:
> 
> >>> def show(data):
> ...for item in data: print(item)
> ... 
> >>> def flatten_one(rows, index):
> ... return [r[:index] + r[index] + r[index+1:] for r in rows]
> ... 

> >>> def flatten_inplace(rows, index):
> ... for row in rows:
> ... row[index:index+1] = row[index]
> ... 
> >>> flatten_inplace(data, 5)
> >>> show(data)
> ['46295', 'Montauk', '3', '60', '85', '19', '5', '1', '0 $277790.00']
> ['46295', 'Dark Eyes', '5', '59', '83', '6', '4', '1', '0 $105625.00']
> ['46295', 'Machinegun Jubs', '6', '53', '77', '6', '2', '1', '1 $71685.00']
> ['46295', 'Zara Bay', '1', '53', '77', '12', '2', '3', '3 $112645.00']

I went for the one I can understand which was inplace

def flatten_inplace(rows, index):
for row in rows:
row[index:index + 1] = row[index]

return rows

See now if I can make it more adaptable to use it in some other situations, 
quite useful.

Thanks

Sayth
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to flatten only one sub list of list of lists

2017-02-28 Thread Peter Otten
Sayth Renshaw wrote:

> How can I flatten just a specific sublist of each list in a list of lists?
> 
> So if I had this data
> 
> 
> [   ['46295', 'Montauk', '3', '60', '85', ['19', '5', '1', '0
> [   [$277790.00']],
> ['46295', 'Dark Eyes', '5', '59', '83', ['6', '4', '1', '0
> [$105625.00']], '46295', 'Machinegun Jubs', '6', '53', '77', ['6',
> ['2', '1', '1 $71685.00']], '46295', 'Zara Bay', '1', '53', '77',
> [['12', '2', '3', '3 $112645.00']]]
> 
> 
> How can I make it be
> 
> 
> [   ['46295', 'Montauk', '3', '60', '85', '19', '5', '1', '0 $277790.00'],
> ['46295', 'Dark Eyes', '5', '59', '83', '6', '4', '1', '0
> [$105625.00'], '46295', 'Machinegun Jubs', '6', '53', '77', '6', '2',
> ['1', '1 $71685.00'], '46295', 'Zara Bay', '1', '53', '77', '12', '2',
> ['3', '3 $112645.00']]
> 
> Been looking around but most solutions just entirely flatten everything.
> This was popular on SO but yeah it flattens everything  I want to be more
> selective
> 
> def flatten(lst):
> for elem in lst:
> if type(elem) in (tuple, list):
> for i in flatten(elem):
> yield i
> else:
> yield elem
> 
> What I am thinking is that if for each list the sublist should be at index
> 1, so
> 
> [0][1]
> [1][1]
> [2][1]
> 
> for item in list:
> item[1] - somehow flatten.
> 
> Thoughts?

Replace the slice row[index:index+1] with row[index], either by building a new 
list or in place:

>>> def show(data):
...for item in data: print(item)
... 
>>> def flatten_one(rows, index):
... return [r[:index] + r[index] + r[index+1:] for r in rows]
... 
>>> show(data)
['46295', 'Montauk', '3', '60', '85', ['19', '5', '1', '0 $277790.00']]
['46295', 'Dark Eyes', '5', '59', '83', ['6', '4', '1', '0 $105625.00']]
['46295', 'Machinegun Jubs', '6', '53', '77', ['6', '2', '1', '1 $71685.00']]
['46295', 'Zara Bay', '1', '53', '77', ['12', '2', '3', '3 $112645.00']]
>>> show(flatten_one(data, 5))
['46295', 'Montauk', '3', '60', '85', '19', '5', '1', '0 $277790.00']
['46295', 'Dark Eyes', '5', '59', '83', '6', '4', '1', '0 $105625.00']
['46295', 'Machinegun Jubs', '6', '53', '77', '6', '2', '1', '1 $71685.00']
['46295', 'Zara Bay', '1', '53', '77', '12', '2', '3', '3 $112645.00']
>>> def flatten_inplace(rows, index):
... for row in rows:
... row[index:index+1] = row[index]
... 
>>> flatten_inplace(data, 5)
>>> show(data)
['46295', 'Montauk', '3', '60', '85', '19', '5', '1', '0 $277790.00']
['46295', 'Dark Eyes', '5', '59', '83', '6', '4', '1', '0 $105625.00']
['46295', 'Machinegun Jubs', '6', '53', '77', '6', '2', '1', '1 $71685.00']
['46295', 'Zara Bay', '1', '53', '77', '12', '2', '3', '3 $112645.00']



-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to flatten only one sub list of list of lists

2017-02-28 Thread Jussi Piitulainen
Sayth Renshaw writes:

> How can I flatten just a specific sublist of each list in a list of lists?
>
> So if I had this data
>
>
> [   ['46295', 'Montauk', '3', '60', '85', ['19', '5', '1', '0 $277790.00']],
> ['46295', 'Dark Eyes', '5', '59', '83', ['6', '4', '1', '0 $105625.00']],
> ['46295', 'Machinegun Jubs', '6', '53', '77', ['6', '2', '1', '1 
> $71685.00']],
> ['46295', 'Zara Bay', '1', '53', '77', ['12', '2', '3', '3 $112645.00']]]
>
>
> How can I make it be
>
>
> [   ['46295', 'Montauk', '3', '60', '85', '19', '5', '1', '0 $277790.00'],
> ['46295', 'Dark Eyes', '5', '59', '83', '6', '4', '1', '0 $105625.00'],
> ['46295', 'Machinegun Jubs', '6', '53', '77', '6', '2', '1', '1 
> $71685.00'],
> ['46295', 'Zara Bay', '1', '53', '77', '12', '2', '3', '3 $112645.00']]

Here's two ways. First makes a copy, second flattens each list in place,
both assume it's the last member (at index -1) of the list that needs
flattening.

datami = [   ['46295', 'Montauk', '3', '60', '85', ['19', '5', '1', '0 
$277790.00']],
 ['46295', 'Dark Eyes', '5', '59', '83', ['6', '4', '1', '0 
$105625.00']],
 ['46295', 'Machinegun Jubs', '6', '53', '77', ['6', '2', '1', '1 
$71685.00']],
 ['46295', 'Zara Bay', '1', '53', '77', ['12', '2', '3', '3 
$112645.00']]]

flat = [ data[:-1] + data[-1] for data in datami ]

for data in datami: data.extend(data.pop())

print('flat copy of datami:', *flat, sep = '\n')
print('flattened datami:', *datami, sep = '\n')
-- 
https://mail.python.org/mailman/listinfo/python-list


How to flatten only one sub list of list of lists

2017-02-28 Thread Sayth Renshaw
How can I flatten just a specific sublist of each list in a list of lists?

So if I had this data


[   ['46295', 'Montauk', '3', '60', '85', ['19', '5', '1', '0 $277790.00']],
['46295', 'Dark Eyes', '5', '59', '83', ['6', '4', '1', '0 $105625.00']],
['46295', 'Machinegun Jubs', '6', '53', '77', ['6', '2', '1', '1 
$71685.00']],
['46295', 'Zara Bay', '1', '53', '77', ['12', '2', '3', '3 $112645.00']]]


How can I make it be


[   ['46295', 'Montauk', '3', '60', '85', '19', '5', '1', '0 $277790.00'],
['46295', 'Dark Eyes', '5', '59', '83', '6', '4', '1', '0 $105625.00'],
['46295', 'Machinegun Jubs', '6', '53', '77', '6', '2', '1', '1 $71685.00'],
['46295', 'Zara Bay', '1', '53', '77', '12', '2', '3', '3 $112645.00']]

Been looking around but most solutions just entirely flatten everything.
This was popular on SO but yeah it flattens everything  I want to be more 
selective

def flatten(lst):
for elem in lst:
if type(elem) in (tuple, list):
for i in flatten(elem):
yield i
else:
yield elem

What I am thinking is that if for each list the sublist should be at index 1, so

[0][1]
[1][1]
[2][1]

for item in list:
item[1] - somehow flatten.

Thoughts?

Sayth
-- 
https://mail.python.org/mailman/listinfo/python-list