Python - get index from nested list where condition is met

Paul

Please could someone help me with getting an index of an item in a nested list where a certain condition is met using Python 2.7? I know that there are similar questions on StackOverflow about this but I can't seem to find good examples that deal with "nested" lists.

I have a list of data which is hundreds of thousands of lines long, in the format below:

data =[
["","","","28.04.2015 09:34:38",1.52411,1.52428,17],
["","","","28.04.2015 09:34:40",1.52415,1.52433,18],
["","","","28.04.2015 09:34:42",1.52425,1.52444,19],
["","","","28.04.2015 09:34:44",1.52417,1.52435,18],
["","","","28.04.2015 09:34:46",1.52421,1.52440,19],
["","","","28.04.2015 09:34:48",1.52426,1.52446,20],
["","","","28.04.2015 09:34:50",1.52429,1.52444,15],
["","","","28.04.2015 09:34:58",1.52423,1.52441,18],
["","","","28.04.2015 09:35:00",1.52416,1.52434,18],
["","","","28.04.2015 09:35:02",1.52416,1.52433,17],
["","","","28.04.2015 09:35:04",1.52416,1.52434,18],
["","","","28.04.2015 09:35:06",1.52406,1.52422,16],
["","","","28.04.2015 09:35:10",1.52406,1.52421,15],
["","","","28.04.2015 09:35:14",1.52427,1.52444,17],
["","","","28.04.2015 09:35:16",1.52424,1.52443,19],
["","","","28.04.2015 09:35:18",1.52434,1.52453,19],
["","","","28.04.2015 09:35:20",1.52434,1.52451,17],
["","","","28.04.2015 09:35:22",1.52438,1.52456,18],
["","","","28.04.2015 09:35:24",1.52432,1.52451,19],
["","","","28.04.2015 09:35:28",1.52445,1.52464,19],
["","","","28.04.2015 09:35:34",1.52435,1.52451,16],
["","","","28.04.2015 09:35:36",1.52432,1.52449,17],
["","","","28.04.2015 09:35:38",1.52429,1.52448,19]]

For each row I want to compare the data in "column 5" (the first col of decimal numbers) to a certain value (lets use 1.52440 as an example) and return the index of the first row where the data is greater than my certain value.

I have made code that does this the 'traditional' way using a for-row-in-data type loop, but I would like to use a better (faster) method if possible and cannot seem to produce the expected result.

The rather poor attempt that I have made so far is:

pricedata = [n[4] for n in data]
myindex = (x for x in enumerate(pricedata) if x > 1.5440).next()

The first row extracts the price data col as a new list. I am not sure this is really necessary but as my understanding of list comprehensions is poor I was trying to break things into steps I understand.

I dont really understand what the second line is doing, but it seems to return (0, 1.52411) - the first item in the list - regardless of what comparison value I enter.

I have also tried:

myindex = [x for x in enumerate(pricedata) if x > 1.5440][0]

and it seems to produce the same result.

I thought the comprehension was saying:

"Make a list of price's for each price you look at in the list-of-indexed-prices if the price-you-are-looking-at is greater than 1.5440", but it seems I am mistaken!

Please could someone point out the error of my ways and help me out? Thank you for any assistance!

Moses Koledoye

Problem is, you're comparing a tuple with a float, since enumerate returns tuples of the index and item, however, tuples are deemed greater than floats in Python 2:

>>> () > 4.
True

Therefore, the first tuple produced by enumerate always yields a match.


To solve this, you should instead unpack the tuple first, and return the first matching index from your generator expression using next:

next(i for i, x in enumerate(data) if x[4] > 1.52415)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Python - Get sublist of list if condition is met

Get value from a Nested MongoDocs where conditions are met

Index of First Value Where Condition Not Met Numpy

Get maximum element of list of list if condition is met

python change value in nested dictionary if condition is met

Remove list from list of lists if condition is met

Python Pandas Concat "WHERE" a Condition is met

Python: sum values in column where condition is met

append last element of nested lists in another nested list if condition met

SQL nested select where condition is met or if row not in table

Get number of rows from mysql where condition is met and the max value in 1 query

How can I get a previous row from where the condition is met in data frame in Pandas

Sample from list of lists until condition is met

How to create Numpy matrix of row index where a certain condition is met?

Python Get list of dictionaries from nested dictionary where value is greater than

How to get a value from a dcitionary if a condition is met?

How to Index Values From a where Condition Tuple in Python?

Get a value from a list where a condition is true using Select()

How to get the ids of nested array once the condition is met?

C# Linq add element to list of object where condition is met

Keep certain elements in a list of tuples where condition is met

Filter list where nested attributes match condition

Get max value of column for rows where a condition is met

Get elements from list in python by ¿index?

Python list select from second dimension where first dimension = condition

Get nested set ancestor where condition

Get items from list where index not equal to an int using LINQ

ElasticSearch exclude result from being filtered where a condition is met

How to return multiple varchar from a stored procedure where condition is met

TOP Ranking

HotTag

Archive