【发布时间】:2020-12-18 07:28:36
【问题描述】:
#orignal list
list_one = [['a','b'],['a','c'],['b','c'],['b','a']]
li=[]
for i in list_one:
for j in list_one:
if i[0] == j[1] and j[0] == i[1]:
li.append([i,j])
print(li)
#[[['a', 'b'], ['b', 'a']], [['b', 'a'], ['a', 'b']]]
我只需要输出 [a,b] a 和 b 可以根据条件而变化,例如 a 可以是苹果或任何东西
【问题讨论】:
-
这能回答你的问题吗? finding duplicates in a list of lists
-
@CarloZanocco "...如果列表的元素不按顺序"
-
@ThierryLathuille 为什么不订购呢?他没有提出不排序列表的约束问题。他只是说他们不按顺序。
-
@CarloZanocco 我在您建议的副本中没有看到任何可以回答这个问题的答案...
-
考虑到@ThierryLathuille 也许你想保留列表的顺序,所以检查here
标签: python python-3.x for-loop nested-lists