[英]Python : How to add for each element of a list every element from another list?
I'm really new to Python and I would like to manage some lists that I created.我对 Python 真的很陌生,我想管理我创建的一些列表。 So, in list AI have the last two digits of years and in list BI have numbers from 000 to 999.因此,在列表 AI 中具有年份的最后两位数字,在列表 BI 中具有从 000 到 999 的数字。
For each year in list A, I want to add every number in list BI guess using a loop...对于列表 A 中的每一年,我想使用循环添加列表 BI 中的每个数字猜测...
listA = ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '60', '61', '62', '63', '64', '65', '66', '67', '68', '69', '70', '71', '72', '73', '74', '75', '76', '77', '78', '79', '80', '81', '82', '83', '84', '85', '86', '87', '88', '89', '90', '91', '92', '93', '94', '95', '96', '97', '98', '99']
listB = ['000', '001', '002', '003', ...]
* somethingsomething loop * * 某事循环 *
listC = ['00000', '00001', '00002', ...]
Thank you!谢谢!
Use itertools.product
.使用itertools.product
。
from itertools import product
listA = ['00', '01', '02', '03']
listB = ['000', '001', '002', '003']
listC = [a + b for a, b in product(listB, listA)] # ['00000', '00001', '00002', ...]
The product
function iterates through the Cartesian product of the supplied iterables (the k-tuples of all combinations drawing one from each list). product
函数遍历提供的可迭代对象的笛卡尔积(所有组合的 k 元组从每个列表中抽取一个)。 String concatenation is used to join the elements of the tuple ( a + b
).字符串连接用于连接元组 ( a + b
) 的元素。 The list comprehension puts them all into a big ol' list.列表理解将它们全部放入一个大列表中。
While itertools.product
is handy, a simple nested comprehension will do just as fine:虽然itertools.product
很方便,但一个简单的嵌套itertools.product
也同样适用:
list_a = ['00', '01', '02', '03'] # Python conventions call for snake_case
list_b = ['000', '001', '002', '003']
list_c = [b + a for b in list_b for a in list_a]
# ['00000', '00001', '00002', '00003', ... ]
Note here that the first for
has the wider scope, which sometimes seems counter-intuitive compared to natural English.请注意,第一个for
的范围更广,与自然英语相比,这有时似乎违反直觉。
You will need to iterate over listB
, so you would do:您将需要遍历listB
,因此您将执行以下操作:
for itemB in listB:
And then for each of these items, you will need to match it with every element in listA
, so you would do another nested loop over listA
.然后每个这些项目中,你需要在每一个元素匹配它listA
,所以你会做另一个循环嵌套在listA
。
for itemA in listA:
And then inside both loops, you would have all combinations of itemA
and itemB
.然后在两个循环中,您将拥有itemA
和itemB
所有组合。 So you will just need to concatenate them and add them to an output list.所以你只需要连接它们并将它们添加到输出列表中。
ouptut = []
for itemB in listB:
for itemA in listA:
output.append(itemB + itemA)
itertools.product
as mentioned in another solution, could give you a more concise solution, since it replaces the two for
loops.另一个解决方案中提到的itertools.product
可以为您提供更简洁的解决方案,因为它替换了两个for
循环。
> Code to reproduce listA
listA = []
for i in range(0,10):
for j in range(0,10):
e = str(i)+str(j)
listA.append(e)
print(listA)
> Code to reproduce listB
listB = []
for i in range(0,10):
for j in range(0,10):
for k in range(0,10):
e = str(i)+str(j)+str(k)
listB.append(e)
print(listB)
> Now we just iterate over each element of listA and append one by one each element of listB, and keep appending the result to listC to get the final output list
listC = []
for i in listA:
for j in listB:
listC.append(i+j)
print(listC)
You can use the inbuild function zip()您可以使用内置函数 zip()
For example:例如:
class SumList():
def __init__(self, this_list):
self.mylist = this_list
def __add__(self, other):
new_list = [ x + y for x,y in zip(self.mylist, other.mylist)]
return SumList(new_list)
def __repr__(self):
return str(self.mylist)
cc = SumList([1,2,3,4])
dd = SumList([100,200,300,400])
ee = cc + dd
print(ee)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.