简体   繁体   English

如何获取列表列表中元素的索引

[英]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.

相关问题 如何在列表列表中获取外部列表元素的索引? - How can I get an index of an outer list element in a list of lists? Python如何通过知道列表中列表的第一个元素来获取列表中列表的索引? - Python how do you get the index of a list in a list, by knowing the first element of the list in lists? 如何将列表列表转换为数据帧并将列表的第一个元素作为索引 - How to covert a list of lists into dataframe and make the first element of the lists as the index 如何获取列表列表中的每个元素? - How to get every element in a list of list of lists? 如何从两个单独的列表中获取公共 integer 元素的索引并将其插入另一个列表? - How do I get the index of the common integer element from two separate lists and plug it to another list? 如何从列表列表中获取包含特定元素的列表 - How to get list which contains certain element from lists of lists 如果列表中的元素具有满足的特定索引和条件,如何更改它? - How to change an element in a list of lists if it has a specific index and condition that is met? 如何从具有2个列表的列表中获取相同的索引元素 - How to take same index element from a list with 2 lists 如何使用 python 中的列表设置列表内元素的索引? - How to set index of element inside list with lists in python? 如何按给定索引处的元素对列表/元组的列表/元组进行排序? - How to sort a list/tuple of lists/tuples by the element at a given index?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM