【发布时间】:2018-04-02 20:43:56
【问题描述】:
nums = [13, 1, 2, 13, 2, 1, 13]
index = [1,3]
unwanted = set()
for i in index:
unwanted.add(nums[i])
print(unwanted)
有什么方法可以将中间的 3 行代码放在一行中吗? 所以,像
new = [i for i in nums if i not in unwanted]
我是 python 新手,想了解“i for i in nums ....”的作用。 在一个典型的 for 循环中,我们只写
for i in item_list
....
我们不会在“for”前面添加“i”。我想知道“i”在“for”前面做什么。
【问题讨论】:
标签: python python-3.x list for-loop set