如何一次遍历两个列表列表并将一个列表中的值替换为另一个列表?

问题描述 投票:1回答:6

我有两个列表(a和b)

它们每行只有2个索引。

a(50,000行)看起来像这样:

|name|age|
|----|---|
|Dany|021|
|Alex|035|

作为列表列表,如下所示:

[['Dany', '021'],['Alex','035'], etc...]

b(2000行)看起来像这样:

|name|age|
|----|---|
|Paul|   |
|Leon|   |

作为列表列表,如下所示:

[['Paul', ''],['Leon',''], etc...]

问题:我想同时迭代ab - 对于a的每次迭代,如果a[0]b[0],我想将相应的a[1]添加到b[1]

举个例子,我想通过浏览我的b列表来添加年份到我的a列表,检查名称是否在a列表中,如果是,则取相应的年龄并将其添加到b列表中以获得相应的名称。

我已经尝试了一个嵌套循环(迭代通过b和每次迭代,迭代通过a来检查aa[0]b的任何迭代是否存在于b[0]for row in b[1:]: # Excluding the headers b_name = row[0] b_age = row[1] for row in a[1:]: if b_name in row[0]: b_age = row[1] else: b_age = '' 迭代中)但是在此之后不断迷失。

b_age

问题是我最终得到b_age的一个值,但应该有2000个独特的a值?

python list loops iteration nested-loops
6个回答
0
投票

假设a中的名称是唯一的,您可以从b创建一个dict,以避免在替换b中的空字符串值时反复循环遍历它。例如(在您的示例中添加了几个项目,以说明如果在a中不存在a = [['Dany', '021'], ['Alex','035'], ['Joe', '054']] b = [['Alex',''], ['Dany', ''], ['Jane', '']] d = {k: v for k, v in a} b = [[k, d[k]] if k in d else [k, v] for k, v in b] print(b) # [['Alex', '035'], ['Dany', '021'], ['Jane', '']] 中的名称会发生​​什么):

dict(a)

如果您实际使用的列表只是示例中的一对简单列表,那么您可以使用k, v替换上面的dict理解。

此外,如果不清楚,各种{x[0]: x[1] for x in a} 引用是为了方便解压缩嵌套对,但您可以使用单个变量并使用索引值进行访问,如:

a

0
投票

您可以尝试通过执行a_dict = dict(a){'Dany': '021', 'Alex': '035', etc...} 变成字典,这将导致如下所示:

for person in b:
    if person[0] in a_dict:
        person[1] = a_dict[person[0]]

然后你可以做这样简单的事情:

b

那应该在[['Paul', ''], ['Leon', ''], ['Alex', '035'], etc...] 给你这样的东西:

b

0
投票

如果要更新b中的值,则需要循环遍历b的行索引。循环使用值将无法工作,因为它们不会将它们的链接保持在b中的源行/列。

此外,假设您只想在a中的所有名称都不匹配,而不仅仅是当前名称不匹配时,您希望将空白年龄指定为for b_row_index in range(1, len(b)): # Excluding the headers b_name = b[b_row_index][0] for a_row in a[1:]: if b_name in a_row[0]: b[b_row_index][1] = a_row[1] break else: b[b_row_index][1] = '' 中的第二列。

试试这个:

b

0
投票

你想要制作一个年龄的字典,这样你就可以为# Make a dictionary of names to their ages age = dict(a) for row in b: try: # Set the age of this row to the age of row[0] row[1] = age[row[0]] except KeyError: # End up here if row[0] is not in the "ages" dict pass 中的每一行做一系列快速的O(1)查找。我会从以下内容开始:

a = [['Dany', '021'],['Alex','035'], ['Paul', '060'],['Leon','070']]
b = [['Paul', ''],['Leon','']]

for i, b_item in enumerate(b):
    for a_item in a:
        if b_item[0]==a_item[0]:
            b[i] = a_item
            break

print(b)

0
投票

使用列表,您可以:

[['Paul', '060'], ['Leon', '070']]

输出:

a = [['Dany', '021'], ['Alex','035'], ['Joe', '054']]
b = [['Alex',''], ['Dany', ''], ['Jane', '']]

print(a)
print(b)
print('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')

# convert to dict for simplicity
a_dictionary  = dict(zip([e[0] for e in a], [e[1] for e in a]))
b_dictionary  = dict(zip([e[0] for e in b], [e[1] for e in b]))
a_intersect_b = list(set(a_dictionary.keys()) & set(b_dictionary.keys()))

print(a_dictionary)
print(b_dictionary)
print(a_intersect_b)
print('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')

# copy ages to b
for k in a_intersect_b:
    b_dictionary[k] = a_dictionary[k]

print(a_dictionary)
print(b_dictionary)
print('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')

# go back to lists
a = [[name, age] for name, age in zip(a_dictionary.keys(), a_dictionary.values())]
b = [[name, age] for name, age in zip(b_dictionary.keys(), b_dictionary.values())]

print(a)
print(b)
print('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')

0
投票

我想正如许多人提到的那样;在这里使用词典,可以让生活更轻松,你可以转换为字典,处理你的数据和追加年龄,然后转换回列表,如果这是你需要的。这段代码完全是这样的:

[['Dany', '021'], ['Alex', '035'], ['Joe', '054']]
[['Alex', ''], ['Dany', ''], ['Jane', '']]
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
{'Dany': '021', 'Alex': '035', 'Joe': '054'}
{'Alex': '', 'Dany': '', 'Jane': ''}
['Alex', 'Dany']
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
{'Dany': '021', 'Alex': '035', 'Joe': '054'}
{'Alex': '035', 'Dany': '021', 'Jane': ''}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
[['Dany', '021'], ['Alex', '035'], ['Joe', '054']]
[['Alex', '035'], ['Dany', '021'], ['Jane', '']]
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

输出:

qazxswpoi
© www.soinside.com 2019 - 2024. All rights reserved.