Python 如何获取Set对象中元素的索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18329308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get index of element in Set object
提问by Lolek
I have something like this:
我有这样的事情:
numberList = {}
for item in results:
data = json.loads(item[0])
if data[key] in itemList:
numberList[itemList.index(data[key])] += 1
print numberList
where itemList is 'set' object. How can I access index of single element in it ?
其中 itemList 是“设置”对象。如何访问其中单个元素的索引?
采纳答案by mculhane
A set is just an unordered collection of unique elements. So, an element is either in a set or it isn't. This means that no element in a set has an index.
集合只是唯一元素的无序集合。因此,一个元素要么在集合中,要么不在集合中。这意味着集合中的任何元素都没有索引。
Consider the set {1, 2, 3}
. The set contains 3 elements: 1, 2, and 3. There's no concept of indices or order here; the set just contains those 3 values.
考虑集合{1, 2, 3}
。该集合包含 3 个元素:1、2 和 3。这里没有索引或顺序的概念;该集合仅包含这 3 个值。
So, if data[key] in itemList
returns True
, then data[key]
is an element of the itemList
set, but there's no index that you can obtain.
因此,如果data[key] in itemList
返回True
,则data[key]
是itemList
集合的一个元素,但是您无法获得索引。