![](/img/trans.png)
[英]Compare two list of tuples to create a new tuple containing highest values
[英]compare list of tuples with eachother then use information to create a new list
所以我有一個包含元組的列表,每個元組在它的零索引中都有一個代碼,在第一個索引中有一個名稱
[('s530', 'smythe'), ('s530', 'smith'), ('d120', 'davies'), ('d120', 'davis')]
基本上我想做的是將列表中的每個元組與嵌套循環進行比較,並確定哪些名稱具有相同的代碼。 然后用這個信息構造一行output然后存入一個新的列表
因此,例如,從元組的示例列表中,我想創建一個這樣的字符串列表
['smythe and smith have the same code','davies and davis have the same code']
a=[('s530', 'smythe'), ('s530', 'smith'), ('d120', 'davies'), ('d120', 'davis')]
def process(n):
ret = []
for i in n:
for j in n:
if i==j:
break
if i[0]==j[0]:
ret.append(f"{i[1]} has same code as {j[1]}")
return ret
print(process(a))
這是一個簡單的方法,它只是使用了兩次嵌套循環並忽略了同一件事
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.