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

时间:2019-04-10 22:00:13

标签: python list loops iteration nested-loops

我有两个列表(a和b)

它们每行只有两个索引。

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]的任何迭代是否存在{{1 }} 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 = '' 的一个值,但是应该有2000个唯一的b_age值?

6 个答案:

答案 0 :(得分:0)

假设const routes: Routes = [ { path: '', redirectTo: 'main', pathMatch: 'full' }, { path: 'main', component: MainComponent }, { path: 'gwt', component: EmptyComponent } ]; 中的名称是唯一的,则可以从a创建一个字典,以避免在替换a中的空字符串值时一遍又一遍地循环。例如(在示例中添加了几项,以说明如果b中的名称在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理解。

此外,在不清楚的情况下,各种dict(a)引用是为了方便解开嵌套对,但是您可以只使用单个变量并使用诸如以下的索引值进行访问:

k, v

答案 1 :(得分:0)

您将要确定年龄,以便可以对b中的每一行进行一系列快速O(1)查找。我将从以下内容开始:

# 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

答案 2 :(得分:0)

您可以尝试通过执行aa_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...]

答案 3 :(得分:0)

如果要更新function check_dd() { if(document.getElementById('show_pic').value == "") { document.getElementById('test').style.display = 'none'; } else { document.getElementById('test').style.display = 'block'; } } <select class="default" id="show_pic" onchange="check_dd();"> <option value="" selected>Select question...</option> <option value="1">Question One</option> <option value="2">Question Two</option> <option value="3">Question Three</option> <option value="4">Question Four</option> <option value="5">Question Five</option> <option value="6">Question Six</option> </select> <div id="test" style="display:none;"><img src="images/dcard.jpg" width="60" height="120" alt=""/></div> 中的值,则需要遍历b的行索引。遍历将不起作用,因为它们不会保持链接回到b中的源行/列。

此外,大概是要在b中名称的 none 不匹配的情况下,而不是仅 >当前名称不匹配。

尝试以下方法:

b

答案 4 :(得分:0)

使用列表,您可以:

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)

输出

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

答案 5 :(得分:0)

我认为和其他许多人一样;在这里使用字典会使生活变得更加轻松,您可以将其转换为字典,处理数据并附加年龄,然后根据需要转换回列表。这段代码正是这样做的:

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('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')

输出:

[['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', '']]
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++