简体   繁体   English

如何在python中合并2个不同长度的列表

[英]How do merge 2 lists of different lengths in python

In Python 3, I'm looking got merge 2 lists of different lengths. 在Python 3中,我正在查找合并2个不同长度的列表。 I've went through a lot of the other threads here, but none seem to be addressing my issue. 我在这里经历了很多其他话题,但是似乎都没有解决我的问题。

I have: 我有:

List1 = ['A','B','C']
List2 = ['ab', 'ac', 'ad', 'ae']

Output = [['Aab', 'Aac', 'Aad', 'Aae'],['Bab','Bac','Bad','Bae'],['Cab','Cac','Cad','Cae']]

Thanks in advance! 提前致谢!

UPDATE -- thanks ndclt for the solution for the above problem. 更新 -感谢ndclt为上述问题提供的解决方案。

My real problem is: 我的真正问题是:

List1 = ['A','B','C']
List2 = ['a','b','c','d','e','f','g','h','i']

Output desired = ['Aa', 'Ab', 'Ac', 'Bd', 'Be', 'Bf', 'Cg', 'Ch', 'Ci']

Here's a one-liner: 这里是单线:

It creates your nested lists using for loop comprehensions. 它使用for循环理解来创建嵌套列表。

List1 = ['A','B','C']
List2 = ['ab', 'ac', 'ad', 'ae']

out = [[an + bn for bn in List2] for an in List1]
print(out)
# >>> [['Aab', 'Aac', 'Aad', 'Aae'], ['Bab', 'Bac', 'Bad', 'Bae'], ['Cab', 'Cac', 'Cad', 'Cae']]

Try this: 尝试这个:

list1 = ['A','B','C']
list2 = ['ab', 'ac', 'ad', 'ae']
lst = []
finaly_lst = []
for i in list1:
    for j in list2:
        lst.append(i+j)
    finaly_lst.append(lst)
    lst = []
print(finaly_lst)

Output: 输出:

[['Aab', 'Aac', 'Aad', 'Aae'], ['Bab', 'Bac', 'Bad', 'Bae'], ['Cab', 'Cac', 'Cad', 'Cae']]

Not really nice but, it works: 并不是很好,但是它可以工作:

List1 = ['A','B','C']
List2 = ['ab', 'ac', 'ad', 'ae']

final = []
for one in List1:
    intermediate = []
    for two in List2:
        intermediate.append(one+two)
    final.append(intermediate)

print(final)
[['Aab', 'Aac', 'Aad', 'Aae'], ['Bab', 'Bac', 'Bad', 'Bae'], ['Cab', 'Cac', 'Cad', 'Cae']]

If nested is not mandatory: 如果嵌套不是必须的:

from itertools import product
List1 = ['A','B','C']
List2 = ['ab', 'ac', 'ad', 'ae']

final = [one+two for one, two in product(List1, List2)]
print(final)
['Aab', 'Aac', 'Aad', 'Aae', 'Bab', 'Bac', 'Bad', 'Bae', 'Cab', 'Cac', 'Cad', 'Cae']
Result = [];
for i in List1:
    p = [];
    for j in List2:
        p.append( ''.join( [i,j] ) );
    Result.append(p);

Try this on for a size. 试穿一个尺寸。 The overall count that both loops would take is len(List1)*len(List2) . 这两个循环的总计数为len(List1)*len(List2) But list declaration inside loop p = [] is initiated empty for every iteration of List1. 但是,对于List1的每次迭代,循环p = []列表声明都是空的。 The ''.join(list) joins elements of the list in a string with character. ''.join(list)用字符将字符串中的列表元素连接起来。 It is an part of string class. 它是string类的一部分。

You can find all functions for the module/class by typing dir(module) or dir(class) into interpreter. 您可以通过在解释器中输入dir(module)dir(class)来找到模块​​/类的所有函数。

Ok so i am assuming your both lists length are divisible and gives integer as your data is provided .. 好的,所以我假设您的两个列表长度都是可分割的,并且在提供数据时给出整数。

from operator import add

List1 = ['A','B','C']
List2 = ['a','b','c','d','e','f','g','h','i']

List = []

for i in List1:
    List.append(i*(len(List2)//len(List1)))

List1 = []

for i in List:
    for j in i:
    List1.append(j)

final_list = list(map(add, List1, List2))

#Output 
#['Aa', 'Ab', 'Ac', 'Bd', 'Be', 'Bf', 'Cg', 'Ch', 'Ci']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM