a=[[2,3],[3,4]]
b=[[5,6],[7,8],[9,10]]
c=[[11,12],[13,14],[15,16],[17,18]]
c1=[[11,12],[13,14],[15,16],[17,18]]
listr=[]
for number in range(96):
listr.append(number)
list = [[]]*96
for e in a:
for f in b:
for g in c:
for h in d:
for i in listr:
list[i].append(e)
list[i].append(f)
list[i].append(g)
print list
I am having real difficulty with this simple problem. I would like to create a list of lists of every combination possible from the above lists. If the list repeats, as in [[2,3],[5,6],[11,12],[11,12]] would not be good, the first combination would be [[2,3],[5,6],[11,12],[13,14]]. This isnt a great start but I know it isnt hard but my programming skills arent strong.
The final list would look like
[[[2,3],[5,6],[11,12],[13,14]],[[2,3],[5,6],[11,12],[15,16]],[[2,3],[5,6],[11,12],[17,18]],...,[[3,4],[9,10],[15,16],[17,18]]]
I would also like to add the 1st number of each list in each individual list and add them together. [[31],[33],[35],...,[44]]
You may want to use itertools.product
to solve this.
Assuming you want combinations from a
, b
, c
and d
in groups of 4 (and based on your expected output I think you have a typo in your c1
which I'm calling d
, adapt as necessary):
>>> import itertools
>>> a = [[2, 3], [3, 4]] # are you sure this isn't actually [[1, 2], [3, 4]]?
>>> b = [[5, 6], [7, 8], [9, 10]]
>>> c = [[11, 12], [13, 14]]
>>> d = [[15, 16], [17, 18]]
>>>
>>> list(itertools.product(a, b, c, d))
[([2, 3], [5, 6], [11, 12], [15, 16]), # pretty printed for readability
([2, 3], [5, 6], [11, 12], [17, 18]),
([2, 3], [5, 6], [13, 14], [15, 16]),
([2, 3], [5, 6], [13, 14], [17, 18]),
...
([3, 4], [9, 10], [13, 14], [17, 18])]
>>> len(list(itertools.product(a, b, c, d)))
24
And btw, when you're trying to create a list of int's, instead of:
listr=[]
for number in range(96):
listr.append(number)
you only need to do:
listr = range(96) # in Python2
# or
listr = list(range(96)) # in Python3
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.