Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get index of element in Set object

Tags:

python

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 ?

like image 791
Lolek Avatar asked Aug 20 '13 07:08

Lolek


People also ask

Can you get index of element in set Python?

There is no index attached to any element in a python set. So they do not support any indexing or slicing operation.

Do sets have an index?

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.

Do sets have indexes JS?

By contrast, Sets are a keyed collection. Instead of using indices, Sets order their data using keys. A Set's elements are iterable in the order of insertion, and it cannot contain any duplicate data.


1 Answers

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.

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.

like image 187
mculhane Avatar answered Oct 06 '22 21:10

mculhane