請有人可以幫助我獲取嵌套列表中的項目索引,其中使用Python 2.7滿足特定條件?我知道在StackOverflow上有類似的問題,但我似乎無法找到處理「嵌套」列表的好例子。Python - 從滿足條件的嵌套列表中獲取索引
我有數據的列表,這是長行數十萬,在下面的格式:
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]]
對於每一行我想要的數據「5列」比較(第一關口十進制數字)設置爲某個值(以1.52440爲例),並返回數據大於我的特定值的第一行的索引。
我已經做了這種使用for-row-in-data類型循環的'傳統'方式的代碼,但是我希望儘可能使用更好(更快)的方法,而且似乎無法產生預期的結果。
,我已經取得迄今已是相當差的嘗試:
pricedata = [n[4] for n in data]
myindex = (x for x in enumerate(pricedata) if x > 1.5440).next()
第一行中提取價格數據山坳作爲一個新的列表。我不確定這是否真的有必要,但是由於我對列表解析的理解很差,我試圖將事情分解成我理解的步驟。
我真的不明白第二行正在做什麼,但它似乎返回(0,1.52411) - 列表中的第一項 - 無論我輸入什麼比較值。
我也曾嘗試:
myindex = [x for x in enumerate(pricedata) if x > 1.5440][0]
,它似乎產生相同的結果。
我認爲理解是說:
「讓價的列表供您在列表的索引,價格看,如果價格任您正在看的,比大於每個價格1.5440「,但似乎我錯了!
請有人指出我的方式的錯誤,並幫助我嗎?感謝您的幫助!
列表似乎並沒有進行排序 – depperm