我想交叉检查两个嵌套列表值,如果我得到一个匹配,将list1中的值附加到list2中。Python - 嵌套列表
例如;
list1 = [[ip, hostname], [ip, hostname], [ip, hostname]]
list2 = [[ip, ip_upper, type, hostname, location], [ip, ip_upper, type, hostname, location], [ip, ip_upper, type, hostname, location]]
我要检查,如果在list1[x][0]
值在list2
,如果是这样,与List1[x][1]
取代List2[x][3]
值。
我正在尝试的是;
count = 0;
for row in list2:
if row[0] in hosts[count][0]:
new_hostname = hosts[count][1]
row[4].append(new_hostname)
count += 1
else:
continue
count += 1
我知道上面是不正确的,我努力弄清楚如何在遍历list2时访问值list1。原因是因为我需要检查每一行,然后检查该行的值,然后修改该行内的特定值。
感谢您的RAPID回应!
我试图实现给定的代码,但试图从我的列表中创建一个词时,我遇到了麻烦:
哇,你们这些家伙!
def doStuff(list1, list2):
mydic = dict(list2)
for l in list1:
l[3] = mydic.get(l[0], l[3])
return mydic
new_dic = doStuff(hostname, old_rows)
我收到错误;
mydic = dict(list2)
ValueError: dictionary update sequence element #0 has length 6; 2 is required
任何想法?
您有具体问题?如果是这样,请编辑您的帖子并包含您的问题。 –
可以'''list2'''命令改变吗? – wwii
只是FYI,但如果'list'内的元素在质量上是异构的,那么它可能不应该是'list'。 – TigerhawkT3