python - 方法调用可以链接到 'set()' 内置吗? (那么为何不?)

标签 python

如果我尝试:

mi_list = ['three', 'small', 'words']
mi_set = set(mi_list)
mi_set.remove('small')
print mi_set  

我得到:

set(['three', 'words'])  

这是我所期望的。而如果我尝试:

mi_list = ['three', 'small', 'words']
mi_set = set(mi_list).remove('small')
print mi_set

我得到:

None

为什么?

我怀疑如果我尝试删除一个不存在的元素(例如“大”),则会报告错误:

KeyError: 'big'

最佳答案

set.remove 不返回任何内容 (None)。

您的代码将 set.remove 的返回值分配给变量 mi_set。因此,mi_set 为 None。

关于python - 方法调用可以链接到 'set()' 内置吗? (那么为何不?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/751457/

相关文章:

python线程: memory model and visibility

javascript - 使用 angular js 客户端对 Python 服务器进行 rest 调用的示例

python - 如何找到 n 个数组中每个数组中包含的值(Python)?

Python:如何解决 python setup.py egg_info failed with error code 1

python - 解析字典中的文本并分成键和值

c# - 如何使用 C# 执行协程?

Python3 无穷大/NaN : Decimal vs. float

python - 如何检查网站上的值是否已更改

python - cython 将指针传递给扩展类型的构造函数

Python AWS SQS 与 MOTO 模拟