简体   繁体   English

Python:如何为列表的每个元素添加另一个列表中的每个元素?

[英]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 .然后在两个循环中,您将拥有itemAitemB所有组合。 So you will just need to concatenate them and add them to an output list.所以你只需要连接它们并将它们添加到输出列表中。

Full Solution完整解决方案

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.

相关问题 如何从列表中删除随机元素并将其添加到python中的另一个列表中 - How to remove a random element from a list and add it to another list in python Python - 如何编写循环以根据另一个列表的元素为列表中的每个 df 添加一列 python - Python - How to write a loop to add a column for every df in a list based on another list's element python 将列表中的每个元素与另一个列表中的每个元素进行比较 - Compare each element in a list with every element in another list 将列表中的每个元素连接到Python中的另一个元素 - Joining each element of a list to another element in Python 使用另一个列表中的每个值在python列表中查找元素 - Lookup element in python list using each value from another list 在Python中将列表的每个元素与另一个列表的每个元素相乘/添加/分配的有效方法 - Efficient way to multiply/add/devide each element of a list with each element of another list in Python 在python列表中的每个列表中添加元素 - Add an element to each list in a list in python 如何使列表中的每个元素在python中成为另一个列表 - How to make each element in a list another list in python 如何在 Python 中检查列表的每个元素,是否包含在另一个列表中? - How to check in Python for each element of a list, whether it is contained in another list? 如何使用 Python 将列表的每个元素添加到新列表? - How to add each element of a list to a new list using Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM