列表推导:如何为x的每次循环迭代创建一个新的列表,其中两个“列表"中的一项都被替换.比赛? [英] List Comprehensions: How do I crank out a new list for every loop iteration of x where an item in both "lists" match?
本文介绍了列表推导:如何为x的每次循环迭代创建一个新的列表,其中两个“列表"中的一项都被替换.比赛?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
allDasTickets = ["9255955", "9255958", "9255960", "9255977"]
[j for j in allDasTickets for x in allDasTickets if x != j]
因此,我得到了以下列表的列表:
So I get an list of the following lists:
['9255958', '9255960', '9255977']
['9255955', '9255958', '9255977']
['9255958', '9255960', '9255977']
['9255955', '9255958', '9255960']
即缺少匹配项的列表(我希望这是我上面提到的内容)
i.e. a list with the matching one missing (I hope that's what I've put above)
推荐答案
您忘记了内括号.之所以需要它们,是因为您试图生成一个列表列表,而不只是一个平面列表.
You forgot the inner brackets. You need them because you're trying to generate a list of lists, instead of just a flat list.
allDasTickets = ["9255955", "9255958", "9255960", "9255977"]
[[j for j in allDasTickets if x != j] for x in allDasTickets]
收益
[['9255958', '9255960', '9255977'],
['9255955', '9255960', '9255977'],
['9255955', '9255958', '9255977'],
['9255955', '9255958', '9255960']]
这篇关于列表推导:如何为x的每次循环迭代创建一个新的列表,其中两个“列表"中的一项都被替换.比赛?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文