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
6
votes
4 answers

Is there a way of recover from an Exception in Directory.EnumerateFiles?

In .NET 4, there's this Directory.EnumerateFiles() method with recursion that seems handy. However, if an Exception occurs within a recursion, how can I continue/recover from that and continuing enumerate the rest of the files? try { var files =…
Magnus Johansson
  • 28,010
  • 19
  • 106
  • 164
6
votes
3 answers

Changing the numberings of nested lists in an Enumerate environment, in LaTeX

I want to produce the following in LaTeX: 1. Item 2. Item 3a. Item 3b. Item 4. Item 5. Item Basically I have already tried using nested enumerate environments, but I have a problem with implementing the different numberings. How can…
Andreas Grech
  • 105,982
  • 98
  • 297
  • 360
6
votes
1 answer

How to enumerate the strings of a context-free grammar?

What algorithm do you use to enumerate the strings generated by a context-free grammar? It seems doable when there is no recursion, but I can't figure out how to do it in the general case, which might contain all kinds of (possibly indirect)…
user541686
  • 205,094
  • 128
  • 528
  • 886
5
votes
2 answers

Combine enumerate + itertools.izip in Python

I would like to iterate + enumerate over two lists in Python. The following code looks ugly. Is there any better solution? for id, elements in enumerate(itertools.izip(as, bs)): a = elements[0] b = elements[1] # do something with id, a and…
Nikolay Vyahhi
  • 1,432
  • 1
  • 16
  • 30
5
votes
1 answer

How to iterate over azure.core.paging.ItemPaged?

I have an iterator object . When I iterate over it for a first time (see code below), it prints the results. However, when I execute this code a second time, it prints nothing. for i, r…
Fluxy
  • 2,838
  • 6
  • 34
  • 63
5
votes
2 answers

Using buffered reader for large .csv files, Python

I'm trying to open large .csv files (16k lines+, ~15 columns) in a python script, and am having some issues. I use the built in open() function to open the file, then declare a csv.DictReader using the input file. The loop is structured like…
Trey
  • 348
  • 5
  • 16
5
votes
1 answer

Why is "for i in range(len(arr))" considered un-Pythonic?

Edit: I want to clarify that I am asking here about situations when the index is explicitly required. I know that for item in items is better when it is not required. I have a long term habit of thinking of list items as my_list[i] or arr[i]. I find…
Robin Andrews
  • 3,514
  • 11
  • 43
  • 111
5
votes
2 answers

Getting all Substrings with length 4 out of infinite list

I'm quite new to Haskell and I'm trying to solve the following problem: I have a function, that produces an infinite list of strings with different lengths. But the number of strings of a certain length is restricted. Now I want to extract all…
T.Naz
  • 125
  • 8
5
votes
3 answers

I ran the following code in Python3.5.2 and got the corresponding output >>> example = ['a','b','c','d','e'] >>> enumerate(example) I'm unable to understand what is the meaning of this output.Why didn't the…
Masquerade
  • 3,580
  • 5
  • 20
  • 37
5
votes
2 answers

skip first two elements in an enumeration -- python

This is not what I want from enumerate: >>> seasons = ['Spring', 'summer', 'fall', 'winter'] >>> list(enumerate(seasons, start =2)) [(2, 'Spring'), (3, 'summer'), (4, 'fall'), (5, 'winter')] This IS the functionality I want from enumerate: >>>…
user2738183
5
votes
8 answers

Is there an equivalent to the range-based `enumerate` loop from python in modern C++?

Is there an equivalent to the range-based enumerate loop from python in C++? I would imagine something like this. enumerateLoop (auto counter, auto el, container) { charges.at(counter) = el[0]; aa.at(counter) = el[1]; } Can this be done…
Max Linke
  • 1,705
  • 2
  • 18
  • 24
5
votes
2 answers

Create cartesian product (powerset?) of JavaScript objects by looping through unknown number of arrays

I'm a beginner, so please pardon my ignorance if this is trivial. I have a javascript object of unknown length, and each property's value is an array (also of unknown length to me). For example: var obj = {"varA":[1,2,3], …
5
votes
1 answer

python dict function on enumerate object

If I have an enumerate object x, why does doing the following: dict(x) clear all the items in the enumerate sequence?
cockadoodledo
  • 371
  • 1
  • 3
  • 10
5
votes
3 answers

Adding column in CSV python and enumerating it

my CSV looks like John,Bomb,Dawn 3,4,5 3,4,5 3,4,5 I want to add ID column in front like so: ID,John,Bomb,Dawn 1,3,4,5 2,3,4,5 3,3,4,5 using enumerate function, but I don't know how. Here's my code so far: import csv with open("testi.csv", 'rb')…
user3454635
  • 127
  • 1
  • 1
  • 6
5
votes
2 answers

What kind of objects does enumerate returns in python?

I'm reading python cookbook, it mentioned that enumerate() returns an iterator yielding all the pairs(two -item tuples) of the form(index, item) And I can use d=dict(enumerate(L)) to make a dict. For my understanding, I thought enumerate() returns a…
Zen
  • 4,381
  • 5
  • 29
  • 56