如果我尝试:
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/