Questions tagged [enumerate]

Related to functions or methods that return enumerations or enumerated-types. These can be objects, data types or data structures in which each of the items of a set has a one-to-one correspondence with a set of ordered numbers. An example is the `enumerate()` built-in function in Python.

Related to functions or methods that return enumerations or enumerated-types. These can be objects, data types or data structures in which each of the items of a set has a one-to-one correspondence with a set of ordered numbers. An example is the enumerate() built-in function in Python.

In Python, the function enumerate(sequence, start=0) sequence must be an object which supports iteration. enumerate() returns an enumerate object which consists of tuples containing a count (from start) and the values obtained from iterating over sequence (from the Python Standard Library official documentation)

975 questions
4
votes
1 answer

Enumerating through a dictionary in Python

I am trying to enumerate through a dictionary like this but it does not work. What's the simplest way to iterate through a dictionary in python while enumerating each entry? for i, k, v in enumerate(my_dict.iteritems()): print i, k, v
user2399453
  • 2,930
  • 5
  • 33
  • 60
4
votes
4 answers

Nested lists to nested dicts

I've a list with master keys and a list of list of lists, where the first value of each enclosed list (like 'key_01') shall be a sub key for the corresponding values (like 'val_01', 'val_02'). The data is shown here: master_keys = ["Master_01",…
MrPadlog
  • 115
  • 1
  • 8
4
votes
6 answers

How to create a template operator for enums

I need a general template class without macro magic that I can use like this: template class enum_operators { E& operator++( E& orig ) { orig = static_cast< E >( orig + 1 ); return orig; } }; enum colors :…
csbako
  • 181
  • 1
  • 7
4
votes
4 answers

TypeError: argument to reversed() must be a sequence

How come enumerate does not produce a sequence? ----> 1 BytesInt('1B') 12 def BytesInt(s): 13 suffixes = ['B','KB','MB','GB','TB','PB','EB','ZB','YB'] ---> 14 for power,suffix in reversed(enumerate(suffixes)): 15 if…
ArekBulski
  • 4,520
  • 4
  • 39
  • 61
4
votes
3 answers

enumerating a list in a list

I have a date with events that happened on the date. I want to enumerate the list of events on the date when i show the calendar. Also I need to be able to delete an event from the list. def command_add(date, event, calendar): if date not in…
Sinoda
  • 245
  • 1
  • 3
  • 9
4
votes
4 answers

How to enumerate over selected elements from an iterable keeping original indices?

I run into the case of using an enumerator on selected elements form an iterable (i.e. a sequence or an iterator or similar) and want that the original indices were being returned instead of the default count, starting by 0 and going up to…
elegent
  • 3,857
  • 3
  • 23
  • 36
4
votes
1 answer

Enumerate list in groovy

I'm trying to use something like the python enumerate() method in groovy. E.g.: list = ['book','pencil','laptop','coffee'] for product, line in enumerate(list, 1): print "Product %s in the %sth line" % (product, line) Output should be: Product…
Alejandro
  • 434
  • 1
  • 3
  • 13
4
votes
4 answers

Python: Enumerate a list of string 'keys' into ints

I searched for a while but didn't find anything that explained exactly what I'm trying to do. Basically I have a list of string "labels", e.g. ["brown", "black", "blue", "brown", "brown", "black"] etc. What I want to do is convert this into a list…
gpanders
  • 1,301
  • 2
  • 15
  • 23
4
votes
1 answer

Iterate over numpy matrix of unknown dimension

I have a multidimensional numpy array I'd like to iterate over. I want to be able to access not only the values, but also their indices. Unfortunately, for idx,val in enumerate(my_array): doesn't seem to work when my_array is multidimensional. (I'd…
user1558954
  • 43
  • 1
  • 3
3
votes
1 answer

subclassing the builtin enumerate in python

Consider the code below. I am trying to subclass the builtin enumerate so that it prints a line for every turn of the for loop. The code seems to be working, which is surprising, because I have never called super().__init__(x). So, what is happening…
NNN
  • 697
  • 1
  • 5
  • 15
3
votes
3 answers

Problem with enumerated type in Java

I am very new to Java-programming, and have some problems in getting an enumerated type to work. In my program I have declared the following static variables: class Employee { enum Gender {MALE, FEMALE}; static final double NORMAL_WORK_WEEK =…
Kristian
  • 1,239
  • 12
  • 31
  • 44
3
votes
3 answers

Enumerate in python to For loop

I want to convert enumerate to for loop. subsets = [0]*15 subsets[0] = 2 subsets = [index + subsets[subsets[index]] for (index,value) in enumerate(subsets)] print(subsets) and the similar subsets = [0]*15 subsets[0] = 2 for index,value in…
3
votes
1 answer

Pytorch: How to get the first N item from dataloader

There are 3000 pictures in my list, but I only want the first N of them, like 1000, for training. I wonder how can I achieve this by changing the loop code: for (image, label) in enumerate(train_loader):
Hannah
  • 69
  • 2
  • 6
3
votes
2 answers

How to print specific pairs of a numpy array

I want to make especific pairs in a numpy array and I show what I want with a simple print function. I have two arrays: points=np.arange(1,15) Then I have another array: repetition= np.array([4, 4, 2, 2, 1, 1]) Now, I want to print the following…
Ali_d
  • 1,089
  • 9
  • 24
3
votes
5 answers

Enumerating over list with strings gives wrong results

I was surprised to see that these two don't output the same results. Why is that? For a list numbers = ['01', '02', '03'] >>> for val in numbers: ... print(val) 01 02 03 while >>> for i, val in numbers: ... print(val) 1 2 3
hlzl
  • 479
  • 2
  • 5
  • 17