【问题标题】:Index a list of lists索引列表列表
【发布时间】:2015-03-08 15:42:13
【问题描述】:

很抱歉用这个可能很愚蠢的问题打扰您,但我已经(再次)被困在这个问题上一段时间了。

我有列表列表

abc = [['date1','number1'],['date2','number2']...]

日期可能相同。例如:date1 和 date2 可能都是 '02/02/2015',而 date3 可能是 '05/02/2015'。

按照示例进行操作时,我想获取日期第一次与我提供函数的日期匹配的元素的索引。例如,像

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

function(abc,'05/02/2015')
output: [2][0]

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

【问题讨论】:

    标签: python indexing nested-lists


    【解决方案1】:
    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
    

    【讨论】:

      【解决方案2】:

      您可以使用如下函数来实现:

      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
      

      【讨论】:

      • 请注意,在 Python 3 中,filter 返回一个生成器,因此如果不将其转换为列表,这将无法工作。此外,此解决方案需要您多次遍历列表。
      • 确实是 filter 评论。但对于第二条评论并不完全正确,因为它将为filter() 解析一次列表,但索引将在匹配时停止,这使其小于O(n*2) 算法,因此它不是 multiple i>,即使它不是一次少于一次。开销是完全可以接受的,除非你是谷歌并处理千兆字节的数据。
      • 这不是批评。多次迭代列表通常根本不是问题。我只是想指出这一点,特别是因为只需记住第一次迭代中的索引就可以避免这种情况(因为不需要知道其他元素也匹配)。
      • 我没有把它当作批评,或者只是作为积极的批评!这就是为什么我在之前的评论中证明我的选择是合理的;-) 我也相信,当我们在 python 中时,OP 对索引工作的请求(大多数情况下可以避免)实际上可能是一个糟糕的解决方法(X-Y 问题)处理过滤列表,不会影响重复项,如我的第二个和第三个 sn-ps 所示。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-20
      • 2016-10-07
      • 2021-06-05
      • 2019-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多