[英]Match elements between two lists but index for index
Lets say I have these 2 listst:可以说我有这两个列表:
lst1 = ['A1','B1','C1']
lst2 = ['A1','B2']
For every element in lst1 compare to the corresponding element in lst2 by index (1-1 not elem 1 to all elems in lst2).对于 lst1 中的每个元素,按索引与 lst2 中的相应元素进行比较(1-1 而不是元素 1 到 lst2 中的所有元素)。 If a match is found print an output like this:
'match found between A1 and A1 at index 0 in both lists'
如果找到匹配项,则打印 output 如下所示:'match found between A1 and A1 at index 0 in both lists'
If the elements don't match an output like this should be given:
'Elements at index 1 do not match"
如果元素与 output 不匹配,应该给出这样的:'Elements at index 1 do not match"
If element at index 2 in lst1 has no coresponding index in lst2 print out:
'For element at index 3, 'C1', no corresponding match found in lst3.
如果 lst1 中索引 2 处的元素在 lst2 中没有对应的索引,则打印输出:'For element at index 3, 'C1', no corresponding match found in lst3.
What i've tried:我试过的:
for i in range(len(lst1)):
if lst1[i] == lst2[i]:
return 'Match between...'
else:
return 'Not matched...'
It failes when the lists do not match in lenght, besides that i'm sure there's a smarter way to do this.当列表的长度不匹配时,它会失败,除此之外,我确信有一种更聪明的方法可以做到这一点。
Try this:尝试这个:
lst1 = ['A1','B1','C1']
lst2 = ['A1','B2']
max_length = max(len(lst1), len(lst2))
for i in range(max_length):
try:
if lst1[i] == lst2[i]:
print(f'match found between {lst1[i]} and {lst2[i]} at index {i} in both lists')
else:
print(f"Elements at index {i} do not match")
except:
if len(lst1) >= i:
print(f"For element at index {i}, {lst1[i]}, no corresponding match found in lst2.")
else:
print(f"For element at index {i}, no corresponding match found in lst2. {lst2[i]}")
Which will produce this output:这将产生这个 output:
match found between A1 and A1 at index 0 in both lists
Elements at index 1 do not match
For element at index 2, C1, no corresponding match found in lst2.
In this situation, I would urge you to use itertools.zip_longest()
.在这种情况下,我会敦促您使用itertools.zip_longest()
。
from itertools import zip_longest
lst1 = ["A1", "B1", "C1"]
lst2 = ["A1", "B2"]
ziplist = list(zip_longest(lst1, lst2))
for i in range(len(ziplist)):
print(ziplist[i][0], ziplist[i][1])
if (ziplist[i][0] is None) and (ziplist[i][1] is not None):
print(
"For element at index {}, {} , no corresponding match found in lst1".format(
i, ziplist[i][1]
)
)
elif (ziplist[i][0] is not None) and (ziplist[i][1] is None):
print(
"For element at index {}, {} , no corresponding match found in lst1".format(
i, ziplist[i][0]
)
)
elif ziplist[i][0] == ziplist[i][1]:
print(
"match found between {} and {} at index {} in both lists".format(
ziplist[i][0], ziplist[i][1], i
)
)
elif ziplist[i][0] != ziplist[i][1]:
print("Elements at index {} do not match".format(i))
I am using zip_longest(lst1, lst2, fillvalue=None)
fillvalue as None over here but you can use whatever fillvalue you want according to your data in the list so you can easily identify if something doesn't exist.我在这里使用zip_longest(lst1, lst2, fillvalue=None)
fillvalue as None 但是您可以根据列表中的数据使用您想要的任何填充值,这样您就可以轻松识别是否存在某些内容。 You can read more about itertools.zip_longest()
here .您可以在此处阅读有关itertools.zip_longest()
的更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.