[英]How to get index of element in list of lists
I'm very new to python and I'm trying to get the index of an element in a list of lists.我是 python 的新手,我正在尝试获取列表列表中元素的索引。 There goes my list:
这是我的清单:
Data = [['0', '999.8', '1.78e-3'], ['5', '1000', '1.52e-3'], ['10', '999.7', '1.31e-3'], ['15', '999.1', '1.14e-3'], ['20', '998.2', '1.00e-3'], ['25', '997.0', '0.89e-3'], ['30', '995.7', '0.80e-3'], ['40', '992.2', '0.65e-3']]
I want to find the index of'10'.我想找到'10'的索引。 There is my code:
有我的代码:
for element in data:
for e in element:
index_valeur = e.index('10')
print(index_valeur)
It doesn't seem to work and this is the error message:它似乎不起作用,这是错误消息:
ValueError: substring not found
How can I get the index of the value?我怎样才能得到值的索引?
Pythonic best way is to use Pandas, my RAW attempt;-) Pythonic 最好的方法是使用 Pandas,我的 RAW 尝试;-)
import numpy as np
import pandas as pd
import io
mycsv = '''
T,rho,mu
0,999.8,1.78e-3
5,1000,1.52e-3
10,999.7,1.31e-3
15,999.1,1.14e-3
20,998.2,1.00e-3
25,997.0,0.89e-3
30,995.7,0.80e-3
40,992.2,0.65e-3
'''
myNum = float(input("Enter number: "))
df = pd.read_csv(io.StringIO(mycsv))
print(sorted(df['T'].values.tolist(), key= lambda x:abs(x-myNum))[:2])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.