我有两个列表(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...]
问题:我想同时遍历a
和b
-对于a
的每次迭代,如果a[0]
在b[0]
中,我想将相应的a[1]
添加到b[1]
中。
用通俗的话来说,我想通过浏览b
列表,检查姓名是否在a
列表中,并将年龄添加到a
列表中,相应的年龄并将其添加到相应名称的b
列表中。
我尝试了一个嵌套循环(通过b迭代,对于每次迭代,迭代a
,以检查a
处a[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
值?
答案 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)
您可以尝试通过执行a
将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...]
答案 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', '']]
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++