MHS
MHS

Reputation: 2350

How to Flatten a list of items in a tuple, using python?

I have a list of tuples of data:

data = [('Date', 'Type', 'Product'),
        ('2013/03/07', 'Electronic', 'TV, Radio, Microwave'),
        ('2013/03/07', 'leather', 'Gucci Wallet')]

I want to duplicate and make my data list simpler like this:

data = [('Date', 'Type', 'Product'),
        ('2013/03/07', 'Electronic', 'TV'),
        ('2013/03/07', 'Electronic', 'Radio'),
        ('2013/03/07', 'Electronic', 'Microwave'),
        ('2013/03/07', 'leather', 'Gucci Wallet')]

Please help me doing this.

Upvotes: 0

Views: 161

Answers (6)

Abhijit
Abhijit

Reputation: 63737

A good problem to utilize itertools.

Read the solution as Flatten the list of pairs of items generated by splitting with ','

list(chain(*(product(*imap(str.split, e)) for e in data)))

And here is the demonstration

>>> from pprint import PrettyPrinter
>>> pp = PrettyPrinter(indent = 4)
>>> data = [('Date', 'Type', 'Product'),
        ('2013/03/07', 'Electronic', 'TV, Radio, Microwave'),
        ('2013/03/07', 'leather', 'Gucci Wallet')]
>>> from itertools import izip, imap, product, chain
>>> data = list(chain(*(product(*imap(str.split, e)) for e in data)))
>>> pp.pprint(data)
[   ('Date', 'Type', 'Product'),
    ('2013/03/07', 'Electronic', 'TV,'),
    ('2013/03/07', 'Electronic', 'Radio,'),
    ('2013/03/07', 'Electronic', 'Microwave'),
    ('2013/03/07', 'leather', 'Gucci'),
    ('2013/03/07', 'leather', 'Wallet')]

Update from OP

data = list(chain(*(product(*imap(str.split(','), e)) for e in refined_data))) , I used this line to flatten my code but it showed this error: "type object argument after * must be a sequence, not generator", the simple split breaks all the words even with space and any special character, please help me

Option 1:

>>> from operator import methodcaller
>>> list(chain(*(product(*imap(methodcaller("split", ","), e)) for e in data)))

Option 2:

>>> list(chain(*(product(*(s.split(",") for s in e)) for e in data)))

Upvotes: 4

zzk
zzk

Reputation: 1387

result = data[:1]
for item in data[1:]:
  (date, category, products) = item
  result.extend(map(lambda product: (date, category, product), tuple(products.split(', '))))

print result

This is as pythonic as I could...

output:

[('Date', 'Type', 'Product'), 
('2013/03/07', 'Electronic', 'TV'), 
('2013/03/07', 'Electronic', 'Radio'), 
('2013/03/07', 'Electronic', 'Microwave'), 
('2013/03/07', 'leather', 'Gucci Wallet')]

Upvotes: 0

Denis
Denis

Reputation: 7343

So I think I have more pythonic solve for this problem and my code is:

result_lst = []
for tup in data[1:]:
    result_lst+=[tup[0:2] + tuple([product]) for product in tup[2].split(',')]
print result
OUT:
[('2013/03/07', 'Electronic', 'TV'),
('2013/03/07', 'Electronic', ' Radio'),
('2013/03/07', 'Electronic', ' Microwave'),
('2013/03/07', 'leather', 'Gucci Wallet')]

Upvotes: 0

avasal
avasal

Reputation: 14854

since the 3rd element is a comma separated string, you can check for it's existence and split accordingly

In [131]: data
Out[131]:
[('Date', 'Type', 'Product'),
 ('2013/03/07', 'Electronic', 'TV, Radio, Microwave'),
 ('2013/03/07', 'leather', 'Gucci Wallet')]

In [132]: data2 = []

In [133]: for item in data:
   .....:     if item[2].find(',') > -1:
   .....:         x =  [(item[0], item[1], x.strip()) for x in item[2].split(',')]
   .....:         for i in x:
   .....:             data2.append(i)
   .....:     else:
   .....:         data2.append(item)
   .....:

In [134]: data2
Out[134]:
[('Date', 'Type', 'Product'),
 ('2013/03/07', 'Electronic', 'TV'),
 ('2013/03/07', 'Electronic', 'Radio'),
 ('2013/03/07', 'Electronic', 'Microwave'),
 ('2013/03/07', 'leather', 'Gucci Wallet')]

Upvotes: 1

AI Generated Response
AI Generated Response

Reputation: 8845

I think a method of doing this would be

def mycopy(lst):
    newlst = []
    for tup in lst:
        newitems = tup[-1].split(',')
        rest = tup[:-1]
        for i in newitems:
            newlst.append(rest+(i,))
    return newlst

This retains order, but operates on a new list (not in place). I'll write an in place one if needed.

Upvotes: 0

jitendra
jitendra

Reputation: 1458

This code should help you make your data simpler.

data = [('Date', 'Type', 'Product'), ('2013/03/07', 'Electronic', 'TV, Radio, Microwave'), ('2013/03/07', 'leather', 'Gucci Wallet')]

for tup in data:
    items=tup[2].split(',');
    if len(items)>1:
        date=tup[0];
        typ=tup[1];
        data.remove(tup);
        for i in items:
            data.append(tuple([date,typ,i]));

PS: This might not maintain the original order.

Upvotes: 0

Related Questions