通过合并多个(4)其他列表来创建新列表

时间:2018-06-19 12:55:35

标签: python

我试图从其他列表new_list创建一个列表a,b,c,d,现在已经搜索了一段时间,这就是我想出的

a = [1] 
b = ['b1','b2','b3',...] 
c = ['c1','c2','c3',...] 
d = ['d1','d2','d3',...] 
count = 0 
output =[] 
for item in [b,c,d]:     
     output.append(item[0])
     count += 1
new_list = list(a + output) 
print (a1)'

output : [1, 'b1', 'c1', 'd1']

它打印出我想要的内容,但是我的问题是我无法继续... 我需要继续打印下一个和下一个new_list,直到列表b,c,d的元素用完为止。

wanted output:
[1, 'b1', 'c1', 'd1']
[1, 'b2', 'c2', 'd2']
[1, 'b3', 'c3', 'd3']
[..... and so on....]

我尝试了count中的item[0]嵌套循环并提取了每个元素,但这是我所遇到的最接近的方法,确实可以使用一些帮助或说明。

5 个答案:

答案 0 :(得分:1)

itertools.repeat()用于第一个列表(实际上是可迭代),然后zip()使用所有可迭代项。

>>> zip(itertools.repeat(1), b, c, d)
[(1, 'b1', 'c1', 'd1'), (1, 'b2', 'c2', 'd2'), (1, 'b3', 'c3', 'd3')]

答案 1 :(得分:1)

假设b,c,d的长度相同,则可以压缩元素(首先将a扩展到所需的长度):

a = [1] 
b = ['b1','b2','b3'] 
c = ['c1','c2','c3']
d = ['d1','d2','d3'] 

a2 = a*len(b)

result = [list(x) for x in zip(a2,b,c,d)]

print(result)
>>>[[1, 'b1', 'c1', 'd1'], [1, 'b2', 'c2', 'd2'], [1, 'b3', 'c3', 'd3']]

答案 2 :(得分:0)

zip_longestfillvalue一起用于处理一个列表中的空值:

from itertools import zip_longest

a = [1] 
b = ['b1','b2','b3'] 
c = ['c1','c2','c3'] 
d = ['d1','d2','d3']

print(list(zip_longest(a, b, c, d, fillvalue=a[0])))

# [(1, 'b1', 'c1', 'd1'), (1, 'b2', 'c2', 'd2'), (1, 'b3', 'c3', 'd3')]

答案 3 :(得分:0)

其他答案产生元组列表。如果您想要一个列表列表,我建议您进行列表理解:

result = [ [a[0], b_i, c_i, d_i] for b_i, c_i, d_i in zip(b, c, d) ]

答案 4 :(得分:0)

import copy

a = [1] 
b = ['b1','b2','b3'] 
c = ['c1','c2','c3'] 
d = ['d1','d2','d3']

lists = [b,c,d]

for x in range(len(lists[0])):
    temp_list = copy.deepcopy(a)
    print(temp_list + [this_list[x] for this_list in lists])

>>[1, 'b1', 'c1', 'd1']
  [1, 'b2', 'c2', 'd2']
  [1, 'b3', 'c3', 'd3']

这里是我的朋友!

我们假设每个列表的长度等于列表的总数(不带a)

  

这将适用于任意数量的任意长度的列表

import copy

a = [1] 
b = ['b1','b2','b3'] 
c = ['c1','c2','c3'] 
d = ['d1','d2','d3']
e = ['e1', 'e2', 'e3', 'e4', 'e5', 'e6']
f = ['f1', 'f2', 'f3', 'f4']

lists = [b,c,d,e,f]
lengths = [len(x) for x in lists]
max_len = max(lengths)

rotated_array = []
for x in range(max_len):
    temp_list = copy.deepcopy(a)
    for l in lists:
        try:
            temp_list.append(l[x])
        except IndexError:
            pass
    rotated_array.append(temp_list)

for row in rotated_array:
    print(row)


>>[1, 'b1', 'c1', 'd1', 'e1', 'f1']
  [1, 'b2', 'c2', 'd2', 'e2', 'f2']
  [1, 'b3', 'c3', 'd3', 'e3', 'f3']
  [1, 'e4', 'f4']
  [1, 'e5']
  [1, 'e6']
  

带有解释的相同代码

# copy module import to use deepcopy. (explanation on usage bellow)
import copy

# initialize lists to work with
a = [1] 
b = ['b1','b2','b3'] 
c = ['c1','c2','c3'] 
d = ['d1','d2','d3']
e = ['e1', 'e2', 'e3', 'e4', 'e5', 'e6']
f = ['f1', 'f2', 'f3', 'f4']

# a list of all lists. It is easier to work with - and iterate.
lists = [b,c,d,e,f]

# a list with the lengths of the other lists. should be: [3, 3, 3, 6, 4]
lengths = [len(x) for x in lists]

# max number of the list above - this is the max length of all lists
max_len = max(lengths)

# now we are going to loop all lists for the length of the longest one.
# looping through a smaller list will cause an IndexError because it will run out of items.
# ex. looping 6 times to list b. First 3 items are ok. There is no fourth. - so b[3] (which is fourth item) will cause IndexError

# initialize an empty list - a list of lists will be an array
rotated_array = []

# iterate for max loops
for x in range(max_len):

    # use deepcopy instead of : temp_list = a , it is a matter of memory and pointers. Deepcopy allocates new space in memory and the temp_list will have its own.
    temp_list = copy.deepcopy(a)

    # now for every list a,b,c,d... we will take the (x)th item and append it in the temp list
    for l in lists:
        try:

            #  temp list is simply [1] at the beginning (deepcopy). Next loop :[1, 'b1'], Next : [1, 'b1', 'c1']
            temp_list.append(l[x])

        # more simple: if the list that we try to take its item does not have this item (when trying to take by number -index)
        except IndexError:

            # pass will just not do anything and continue to the next loop
            pass

    # append temp_list in rotated list. first its empty :[], Next loop a single row is appended: [[1, 'b1', 'c1', 'd1', 'e1', 'f1']], Next: [[1, 'b1', 'c1', 'd1', 'e1', 'f1'], [1, 'b2', 'c2', 'd2', 'e2', 'f2']]
    rotated_array.append(temp_list)

# print the array - printing row after row will cause the effect af an array looking like the ones we are used to
for row in rotated_array:
    print(row)



>>[1, 'b1', 'c1', 'd1', 'e1', 'f1']
  [1, 'b2', 'c2', 'd2', 'e2', 'f2']
  [1, 'b3', 'c3', 'd3', 'e3', 'f3']
  [1, 'e4', 'f4']
  [1, 'e5']
  [1, 'e6']