在函数调用结果中调用“del”导致“SyntaxError:不能删除函数调用”

时间:2022-03-31 22:43:25

Consider below example:

考虑下面的例子:

random_tile = random.choice(tiles)
del random_tile

It first assigns a random element from the list tiles to a variable, and then calls a function on that variable.

它首先从列表块中分配一个随机元素到一个变量,然后调用该变量上的函数。

Then if we want to shorten the code as follows:

如果我们想缩短代码如下:

del random.choice(tiles)

We would get a SyntaxError: can't delete function call. I tried eval() with no luck. How can this be solved?

我们会得到一个SyntaxError:不能删除函数调用。我试了eval(),没有运气。如何解决这个问题?

EDIT:

编辑:

What I am trying to do is to remove a element from the tiles list at random. I thought I found a more compact way of doing it other than using random.randint() as the index, but I guess not.

我要做的是随机从tiles列表中移除一个元素。我认为我找到了一种更紧凑的方法,而不是使用随机数。randint()作为索引,但我想没有。

Is there a pythonic/conventional way of doing this otherwise?

是否有一种python /传统的方法来做这个?

4 个解决方案

#1


4  

del is not a function, it is a keyword to create del statements e.g. del var. You can't use it as a function or on a function, it can only be applied to a variable name, list of variable names or subscriptions and slicings e.g.

del不是一个函数,它是创建del语句的关键字,例如del var。你不能将它用作函数或函数,它只能应用于变量名、变量名列表或订阅和分割。

del a
del a,b
del l[0]
del l[:]

To remove the random item you can try this

要删除随机项目,您可以尝试这个。

random_tile.remove(random.choice(tiles))

BUT it is not best way to remove items because it could mean a internal search for items and item comparison, also won't work if items are not unique, so best would be to get random index and delete that

但它不是最好的移除项的方法,因为它可能意味着对项和项比较的内部搜索,如果项不是唯一的,也不会起作用,所以最好是获取随机索引并删除它。

index = random.randint(0, len(l)-1)
del l[index]

#2


2  

If you're trying to actually modify your tiles list, your first attempt doesn't do it either. It just assigns to your variable, then wipes that variable. That's because del works on the variable itself, not what it points to. Most other functions work on the value some variable points to. So unless you actually need to use del, what you're trying to do will work fine.

如果你想修改你的列表,你的第一次尝试也不会。它只是分配给你的变量,然后擦去那个变量。这是因为del对变量本身起作用,而不是它指向什么。大多数其他函数的作用是一些变量指向的值。所以,除非你真的需要使用del,否则你要做的事情会很好。

Also, if you are trying to use del, i'm pretty sure you're using it wrong. If you elaborate more on what you're trying to do, we can probably help you.

另外,如果你想用del,我很确定你用错了。如果你详细说明你想做什么,我们可能会帮助你。

#3


1  

There are a number of ways to remove a random element from a list. Here's one.

从列表中删除随机元素有很多方法。这是一个。

>>> import random
>>> my_list = [1, 2, 3, 4, 5]
>>> my_list.remove(random.choice(my_list))
>>> my_list
[1, 2, 4, 5]

#4


0  

Given a list tiles this does the trick:

给定一个列表框,这是一个技巧:

import random
tiles.remove(random.choice(tiles))

#1


4  

del is not a function, it is a keyword to create del statements e.g. del var. You can't use it as a function or on a function, it can only be applied to a variable name, list of variable names or subscriptions and slicings e.g.

del不是一个函数,它是创建del语句的关键字,例如del var。你不能将它用作函数或函数,它只能应用于变量名、变量名列表或订阅和分割。

del a
del a,b
del l[0]
del l[:]

To remove the random item you can try this

要删除随机项目,您可以尝试这个。

random_tile.remove(random.choice(tiles))

BUT it is not best way to remove items because it could mean a internal search for items and item comparison, also won't work if items are not unique, so best would be to get random index and delete that

但它不是最好的移除项的方法,因为它可能意味着对项和项比较的内部搜索,如果项不是唯一的,也不会起作用,所以最好是获取随机索引并删除它。

index = random.randint(0, len(l)-1)
del l[index]

#2


2  

If you're trying to actually modify your tiles list, your first attempt doesn't do it either. It just assigns to your variable, then wipes that variable. That's because del works on the variable itself, not what it points to. Most other functions work on the value some variable points to. So unless you actually need to use del, what you're trying to do will work fine.

如果你想修改你的列表,你的第一次尝试也不会。它只是分配给你的变量,然后擦去那个变量。这是因为del对变量本身起作用,而不是它指向什么。大多数其他函数的作用是一些变量指向的值。所以,除非你真的需要使用del,否则你要做的事情会很好。

Also, if you are trying to use del, i'm pretty sure you're using it wrong. If you elaborate more on what you're trying to do, we can probably help you.

另外,如果你想用del,我很确定你用错了。如果你详细说明你想做什么,我们可能会帮助你。

#3


1  

There are a number of ways to remove a random element from a list. Here's one.

从列表中删除随机元素有很多方法。这是一个。

>>> import random
>>> my_list = [1, 2, 3, 4, 5]
>>> my_list.remove(random.choice(my_list))
>>> my_list
[1, 2, 4, 5]

#4


0  

Given a list tiles this does the trick:

给定一个列表框,这是一个技巧:

import random
tiles.remove(random.choice(tiles))