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
5
votes
1 answer

Scala mkString except the last one

I would like to do the following in scala: val l = List("An apple", "a pear", "a grapefruit", "some bread") ... some one-line simple function ... "An apple, a pear, a grapefruit and some bread" What would be the shortest way to write it that…
Mikaël Mayer
  • 10,425
  • 6
  • 64
  • 101
5
votes
4 answers

Enumerate list of elements starting from the second element

I have this list [['a', 'a', 'a', 'a'], ['b', 'b', 'b', 'b', 'b'], ['c', 'c', 'c', 'c', 'c']] and I want to concatenate 2nd and 3rd elements in each row, starting from the second row, to make something like this: [['a', 'a', 'a', 'a'], ['b',…
Zhorzh Alexandr
  • 1,469
  • 2
  • 17
  • 28
5
votes
1 answer

Read csv, then enumerate

At the moment, I have a list directly in my code and am using enumerate so I can use the list index numbers for the user to select one of the items. Looks like this (just header row included) fmpList = [['Date', 'ID', 'Plot No', 'Modified', 'Lat',…
Op.Ivy
  • 81
  • 1
  • 3
  • 9
5
votes
1 answer

Getting driver files for a particular device

I would like to know how I can get all the driver files for a particular device just like the Device Manager does? I have the following code: procedure TdlgMain.Test(const DeviceIndex: Integer); var PnPHandle: HDEVINFO; DevData: TSPDevInfoData; …
Pateman
  • 2,727
  • 3
  • 28
  • 43
4
votes
3 answers

enumerate causes incompatible type mypy error

The following code: from typing import Union def process(actions: Union[list[str], list[int]]) -> None: for pos, action in enumerate(actions): act(action) def act(action: Union[str, int]) -> None: print(action) generates a mypy…
James Roberts
  • 195
  • 1
  • 4
  • 13
4
votes
2 answers

Using key value pair assignments for a dictionary as an iterator in a for loop

I just recently came across this cool hack in Python. This: d = {} for i, d[i] in enumerate('abc'): pass >>> d {0: 'a', 1: 'b', 2: 'c'} >>> This assigns key value pairs to a empty dictionary from the iterator. I would like to know how Cython…
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
4
votes
3 answers

Get the first item of an IEnumerable and return the rest as IEnumerable, iterating through only once

I've got an enumerable that contains responses from a service call that come in gradually. I can't do ToList on the enumerable as that would block until all responses are received instead of listing them as they come. I also can't iterate twice…
Bartosz Wójtowicz
  • 1,321
  • 10
  • 18
4
votes
1 answer

Multi-threading Julia shows error with enumerate iterator

Why does this very simple code result in an error in Julia 1.1? Threads.@threads for (index,value) in enumerate([0.1,0.2,0.3]) println(value^index) end The error shown is: Error thrown in threaded loop on thread 0:…
coolsv
  • 781
  • 5
  • 16
4
votes
2 answers

How to enumerate from the middle of tuple

By default, enumerate() starts counting at 0 but if you give it a second integer argument, it'll start from that number instead but how can we enumerate from a particular value of count and elem. for example, we want to pass count as '2' and elem as…
aryan
  • 379
  • 1
  • 3
  • 6
4
votes
7 answers

Accessing a tuple in enumerate?

I want to iterate through a list and sum all the elements. Except, if the number is a 5, I want to skip the number following that 5. So for example: x=[1,2,3,4,5,6,7,5,4,3] #should results in 30. I'm just not sure how I can access the index of a…
Metal.days
  • 85
  • 7
4
votes
6 answers

Serial port enumeration in Delphi using SetupDiGetClassDevs

I'm trying to enumerate "friendly names" for COM ports. The ports may dynamically change as USB-serial devices are connected and disconnected at runtime. Based on the possible methods described in this question, I am attempting to use the…
tim11g
  • 1,935
  • 5
  • 27
  • 41
4
votes
2 answers

Can someone explain to me why this second method does not fully update the string?

I've been trying to write a function which converts under_score_words to camelCaseWords. The following is how I'd do something like this in the past; functionName = "function_for_test_case" for character in functionName: if character == "_": …
user6512120
4
votes
1 answer

How do I automatically number items throughout an R markdown document?

I am looking for a way to automatically number examples throughout an R markdown document. I know that automatic numbering is possible with a list, such as: 1. Item 1 1. Item 2 1. Item 3 The problem is that this will not work when the items appear…
Namenlos
  • 475
  • 5
  • 17
4
votes
2 answers

Values being altered in numpy array

So I have a 2D numpy array (256,256), containing values between 0 and 10, which is essentially an image. I need to remove the 0 values and set them to NaN so that I can plot the array using a specific library (APLpy). However whenever I try and…
Sam Billington
  • 299
  • 1
  • 4
  • 11
4
votes
2 answers

How can I search a list in python and print at which location(s) in that list my criteria is located?

I am generating a list of 12 random numbers: nums = [] for num in range(12): nums.append(random.randint(1,20)) I would then like to search 'nums' for a specific number, say '12' and use an if statement to print if it showed up and where in…
Austin
  • 114
  • 7