本文介绍了del并设置提案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 您可以执行以下操作: a = [1,2,3,4,5] del a [0] 和 a = {1:''1'',2:''2'',3:''3'',4:' '4'',5:''5''} del a [1] 为什么套装不一样(特别是因为套装是基于一个 字典)? a = set([1,2,3,4,5]) del a [1] 是的我知道集合有一个删除方法(如列表),但是因为字典 没有一个删除方法,不应该设置更像字典 而不像列表? IMHO del for sets非常直观。我想现在改变太晚了。 -Larry 解决方案 设置不支持下标,所以如果你不能去 ''a_set [某事]'',为什么你能够能够这样的表达能够获得''del''?订阅甚至意味着没有del的?它没有意义,只会是 不一致。 不,集合本身就是一种数据类型。它们基于内部的b 字典(至少在CPython中),但这是一个隐藏的 实现细节,没有强调。 最有可能。 干杯, 克里斯 - 沿着鬣蜥的路径...... http:/ /rebertia.com Traceback(最近一次调用最后一次): 文件"< pyshell#15>",第1行,< module> a [0] TypeError:''set''对象是不可索引的 无论是否需要索引都没有意义。 还值得注意的是从容器中删除对象 (.remove)与提议将对象转到GC(del。 ..) 不得不同意del []在套装上有任何意义。 累了,已经晚了,所以可能打字垃圾,但是当我开始阅读小组的时候感觉还好。 Jon。 ``del``并不建议对象转到GC,至少不是更多 然后`remove()`方法呢。就像`remove()```del``只删除 对象的引用一样。总是。 Ciao, Marc''BlackJack''Rintsch You can do the following: a = [1,2,3,4,5]del a[0] and a = {1:''1'', 2: ''2'', 3: ''3'', 4:''4'', 5:''5''}del a[1] why doesn''t it work the same for sets (particularly since sets are based on adictionary)? a = set([1,2,3,4,5])del a[1] Yes I know that sets have a remove method (like lists), but since dictionariesdon''t have a remove method, shouldn''t sets behave like more like dictionariesand less like lists? IMHO del for sets is quite intuitive. I guess it is toolate to change now. -Larry 解决方案 Sets don''t support subscripting, so if you can''t go''a_set[something]'', why would you expect to be able to be able to''del'' such an expression? What would the subscription even meanwithout the ''del''? It doesn''t make sense and would just beinconsistent. No, sets are a datatype unto themselves. They are based ondictionaries internally (at least in CPython), but that''s animplemention detail to be hidden, not emphasized. Most likely. Cheers,Chris--Follow the path of the Iguana... http://rebertia.com Traceback (most recent call last):File "<pyshell#15>", line 1, in <module>a[0]TypeError: ''set'' object is unindexable No point it needing to be indexable either. It''s also worth noting that removing an object from a container(.remove) is different than proposing the object goes to GC (del...) Have to disagree that del[] on a set makes any sense. Tired, and it''s late, so probably typing rubbish, but felt okay when Istarted reading the group :) Jon.``del`` doesn''t propose that the object goes to GC, at least not morethen a `remove()` method does. Just like `remove()` ``del`` only removesreferences to objects. Always. Ciao,Marc ''BlackJack'' Rintsch 这篇关于del并设置提案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-17 20:40