简体   繁体   English

Python:打印列表项不在单独的索引列表中

[英]Python: Print List Items Not in Separate Index List

I have two lists: 我有两个清单:
list = ["a","b","c","d"]
i_to_skip = [0,2]

I'd like to print everything in list except for the indices in i_to_skip . 除了i_to_skip的索引,我想打印list中的i_to_skip I've tried the following, which just returns a generator object: 我尝试了以下操作,它仅返回一个生成器对象:

print(x for x in list if x not in i_to_skip)

The reason your comprehension does work is that x is the value , like "a" , not the index , like 0 , and of course "a" is not in [0, 2] . 您的理解确实起作用的原因是x ,例如"a" ,而不是索引 ,例如0 ,当然"a"不在[0, 2]

To get the index along with the value, you need enumerate . 要获取索引和值,您需要enumerate Then you can do this: 然后,您可以执行以下操作:

print([x for i, x in enumerate(list) if i not in i_to_skip])

Also, note that printing a generator expression (as you did) is just going to print something like <generator object <genexpr> at 0x1055fd8b8> ; 另外,请注意,打印生成器表达式(如您所做的那样)将仅打印类似<generator object <genexpr> at 0x1055fd8b8> that's why I converted your code to printing out a list comprehension, so you get ['b', 'd'] instead. 这就是为什么我将您的代码转换为打印列表理解的原因,所以您得到的是['b', 'd']


If you instead wanted to print, say, one line at a time, you could loop over the generator expression: 相反,如果您想一次打印一行,则可以遍历生成器表达式:

for x in (x for i, x in enumerate(list) if i not in i_to_skip):
    print(x)

But really, it's easier to just collapse that into a single loop: 但实际上,将其折叠成一个循环会更容易:

for i, x in emumerate(list):
    if i not in i_to_skip:
        print(x)

Or, even simpler, format the whole thing in a single expression, maybe like this: 或者,甚至更简单地,将整个内容格式化为单个表达式,也许像这样:

print('\n'.join(x for i, x in enumerate(list) if i not in i_to_skip))

… or even let print do it for you: …甚至让print为您完成:

print(*(x for i, x in enumerate(list) if i not in i_to_skip), sep='\n')

Finally, as a side note, calling your list list is a bad idea; 最后,作为一个补充说明,调用您的列表list是一个坏主意; it hides the type/constructor function, which may want to use later on, and it also makes your code misleading. 它隐藏了类型/构造函数,稍后可能要使用它,并且还会使您的代码产生误导。

Your print result is a generator , you need to put your list comprehension in a list sign ( [] ) or use list() function, also you need to check the indices not the values , so you can figure it out with enumerate() : 您的打印结果是一个生成器 ,您需要将列表理解放在列表符号( [] )或使用list()函数中,还需要检查索引而不是值,因此可以使用enumerate()来确定它:

>>> print ([j for i,j in enumerate(list) if i not in i_to_skip] )
['b', 'd']
mylist = ["a","b","c","d"]
print([x for i,x in enumerate(mylist) if i not in i_to_skip])

You shouldn't call your list list or you're hiding the list type itself. 您不应该调用列表list或者隐藏list类型本身。

enumerate will give you the indexes and corresponding elements of the list. enumerate将为您提供列表的索引和相应元素。

Putting the comprehension in square brackets makes it into a list, which is easier to print than a generator. 将理解内容放在方括号中会使其成为一个列表,比生成器更容易打印。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM