Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为列表编制索引_Python_Indexing_Nested Lists - Fatal编程技术网

Python 为列表编制索引

Python 为列表编制索引,python,indexing,nested-lists,Python,Indexing,Nested Lists,很抱歉用这个可能很愚蠢的问题来打扰你,但我(再次)被这个问题困扰了一段时间 我有一份清单 abc = [['date1','number1'],['date2','number2']...] 日期可能是一样的。例如:日期1和日期2可能都是'02/02/2015',而日期3可能是'05/02/2015' 在下面的示例中,我希望获得元素的索引,其中日期与我提供函数的日期第一次匹配。比如说 function(abc,'02/02/2015') output: [0][0] (and only thi

很抱歉用这个可能很愚蠢的问题来打扰你,但我(再次)被这个问题困扰了一段时间

我有一份清单

abc = [['date1','number1'],['date2','number2']...]
日期可能是一样的。例如:日期1和日期2可能都是'02/02/2015',而日期3可能是'05/02/2015'

在下面的示例中,我希望获得元素的索引,其中日期与我提供函数的日期第一次匹配。比如说

function(abc,'02/02/2015')
output: [0][0] (and only this, so not [1][0] as well)


有人知道怎么做吗?谢谢大家!

您可以使用如下函数实现这一点:

def match_date(l, d):
    return list(filter(lambda x: x[0] == d, l))[0]
由于内置了
filter()
,它将匹配作为列表中每个元素的第一个参数给出的函数,并返回一个列表,其中包含函数返回的所有值
True
。因此,它将返回列表中匹配的所有日期的列表:

>>> def match_date(l, d):
...     return list(filter(lambda x: x[0] == d, l))[0]
... 
>>> abc = [['date1','number1'],['date2','number2']]
>>> match_date(abc, 'date2')
['date2', 'number2']
>>> abc = [['date1','number1'],['date2','number2'],['date2', 'number3'],['date3', 'number4']]
>>> match_date(abc, 'date2')
['date2', 'number2'], ['date2', 'number3']
从那里,您可以执行以下操作:

>>> abc.index(match_date(abc, 'date2')[0])
1
这将为您提供匹配的第一个元组的索引。我认为您不需要第二个索引,因为您知道它总是
[0]
,因为它是您的数据模型

要使其成为一项功能:

>>> def get_index_of_match_date(l, d):
...     return l.index(filter(lambda x: x[0] == d, l)[0])
... 
>>> get_index_of_match_date(abc, 'date2')
0
>>> get_index_of_match_date(abc, 'date2')
1
>>> get_index_of_match_date(abc, 'date3')
3
基本上,您希望遍历列表,并检查每个子列表中的第一个元素是否与所需的日期匹配。如果是这样,只需返回您当前所在的索引;否则继续迭代

>>> abc = [['02/02/2015', '1'], ['02/02/2015', '2'], ['05/02/2015', '3']]    
>>> firstMatch('02/02/2015', abc)
0
>>> firstMatch('05/02/2015', abc)
2

请注意,在Python3中,
filter
返回一个生成器,因此如果不将其强制转换为列表,它将无法工作。此外,此解决方案要求您在列表中迭代多次。实际上,对于
过滤器
注释。但对于第二条注释来说并非完全正确,因为它将为
filter()
完全解析列表一次,但索引将在匹配时停止,这使得它是一个小于
O(n*2)
的算法,因此它不是多个,即使它不是一次或小于一次。这种开销是完全可以接受的,除非你是谷歌,处理千兆字节的数据。这不是批评。多次迭代一个列表通常根本不是问题。我只是想指出这一点,特别是因为在第一次迭代中只需记住索引就可以避免(因为不需要知道其他元素也匹配)。我没有将其视为批评,也没有将其视为积极的批评!这就是为什么我在之前的评论中为我的选择辩护;-)我还相信,在python中,OP请求处理索引,这在大多数情况下是可以避免的,实际上可能是处理过滤列表的一个糟糕的解决方法(X-Y问题),它不会像我的第二个和第三个代码片段所示那样对重复项进行阴影处理。
def firstMatch (date, lst):
    for i, sublist in enumerate(lst):
        if sublist[0] == date:
            return i
>>> abc = [['02/02/2015', '1'], ['02/02/2015', '2'], ['05/02/2015', '3']]    
>>> firstMatch('02/02/2015', abc)
0
>>> firstMatch('05/02/2015', abc)
2