1

I had a list, for example:

data=['January', 'February', 'March',
      2007, 2008, 2009,
      'value1', 'value2', 'value3']

Now I need to create the list of tuples in the following format

[('January', 2007,'value1'),('February', '2007','value2'),('March', 2007,'value3'),
('January', 2008,'value1'),('February', 2008,'value2'),('March', 2008,'value3'),
('January', 2009,'value1'),('February', 2009,'value2'),('March', 2009,'value3')] 

Please let me know the concept

jamylak
  • 128,818
  • 30
  • 231
  • 230
John
  • 35
  • 1
  • 2
  • 5
    This is not valid Python, so I don't know what you want the list to look like exactly. Please fix that. – ch3ka Apr 18 '12 at 14:22
  • It is not good practice to call a variable `list` because you lose access to the builtin `list`. – jamylak Apr 18 '12 at 16:10
  • @jamylak This appears to have been fixed and then reintroduced by editors - I'll edit back to using ``data``. I'm presuming the list of tuples was meant to have commas on line-ends as well. – Gareth Latty Apr 18 '12 at 18:32

3 Answers3

1

There are a number of solutions, as your question was a bit ambiguous about what you wanted, here are the solutions I would suggest.

Whatever you want, the first step is to split the list into independent lists. The cleanest way to do this is with a generator (there are other ways of doing this for generators as opposed to lists, but for your use case that's overkill):

def segments(l, n):
    for i in range(0, len(l), n): #Use xrange in python 2.x
        yield l[i:i+n]

Although it's entirely possible to use a generator expression, it isn't particularly readable:

(data[y:y+3] for y in range(0, len(data), 3))

Wherever I use segments(data, 3), you could use this generator expression instead, but I'll stick to the more readable version.

If you wanted output of matched (month, year, value), then the answer is very simple:

list(zip(*segments(data, 3)) #No need to use list() in 2.x

Produces:

[('January', 2007, 'value1'), ('Febraury', 2008, 'value2'), ('March', 2009, 'value3')]

We unpack our three lists as arguments into zip() which gives us a generator of (month, year, value) items.

If you wanted all of the combinations of (month, year, value) then you can use itertools.product():

from itertools import product
...
list(product(*segments(data, 3)) 

If you only wanted, as your output suggests, the set product of the month tied to the value and the year, then you will need:

from itertools import product
...
months, years, values = segments(data, 3)
[(month, year, value) for ((month, value), year) in product(zip(months, values), years)]
Community
  • 1
  • 1
Gareth Latty
  • 86,389
  • 17
  • 178
  • 183
1

Something like:

>>> l1 = ['January', 'February', 'March']
>>> l2 = [2007,2008,2009]
>>> l3 = ['value1','value2','value3']
>>> for year in l2:
...     print zip(l1, [year] * len(l1), l3)
...
[('January', 2007, 'value1'), ('February', 2007, 'value2'), ('March', 2007, 'value3')]
[('January', 2008, 'value1'), ('February', 2008, 'value2'), ('March', 2008, 'value3')]
[('January', 2009, 'value1'), ('February', 2009, 'value2'), ('March', 2009, 'value3')]

Splitting of the given list into tree pieces is left as an exercise for OP.

Roman Bodnarchuk
  • 29,461
  • 12
  • 59
  • 75
0
>>> from itertools import product,cycle
>>> data = ['January','February','March', 2007,2008,2009,'value1','value2','value3']
>>> block = 3
>>> months,years,values = zip(*[iter(data)]*block)
>>> [(m,y,v) for (y,m),v in zip(product(years,months),cycle(values))]
[('January', 2007, 'value1'), ('February', 2007, 'value2'), ('March', 2007, 'value3'),
 ('January', 2008, 'value1'), ('February', 2008, 'value2'), ('March', 2008, 'value3'), 
 ('January', 2009, 'value1'), ('February', 2009, 'value2'), ('March', 2009, 'value3')]
jamylak
  • 128,818
  • 30
  • 231
  • 230