[英]Using python keywords with list comprehension
How do i use a python keyword with a list comprehension, for example a del keyword in the following list comprehension. 我如何在列表理解中使用python关键字,例如在以下列表理解中使用del关键字。
[del df[x] for x in y]
thanks 谢谢
List comprehensions are a way to represent an expression. 列表推导是表示表达式的一种方式。 They cannot include statements.
它们不能包含语句。 Use a regular loop.
使用常规循环。
for x in y:
del df[x]
I am guessing you want to delete using list comprehension. 我猜您想使用列表理解功能删除。 Here's how you can do that-
这是您可以执行的操作-
df = [x for x in df if x not in to_remove]
Here's an example 这是一个例子
>>> df =[1,2,3]
>>> to_remove=[1,2]
>>> df = [x for x in df if x not in to_remove]
>>> df
[3]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.