代码之家  ›  专栏  ›  技术社区  ›  fmsf

如何获取用“if element in list”找到的元素的索引或元素本身

  •  2
  • fmsf  · 技术社区  · 16 年前

    有没有直接的方法?

    if element in aList:
       #get the element from the list
    

    aList = [ ([1,2,3],4) , ([5,6,7],8) ]
    element = [5,6,7]
    if element in aList
         #print the 8
    
    7 回复  |  直到 16 年前
        1
  •  3
  •   Roger Pate Roger Pate    16 年前
    L = [([1, 2, 3], 4), ([5, 6, 7], 8)]
    element = [5, 6, 7]
    
    for a, b in L:
      if a == element:
        print b
        break
    else:
      print "not found"
    

    但听起来你想用字典:

    L = [([1, 2, 3], 4), ([5, 6, 7], 8)]
    element = [5, 6, 7]
    
    D = dict((tuple(a), b) for a, b in L)
    # keys must be hashable: list is not, but tuple is
    # or you could just build the dict directly:
    #D = {(1,2,3): 4, (5,6,7): 8}
    
    v = D.get(tuple(element))
    if v is not None:
      print v
    else:
      print "not found"
    

    请注意,虽然使用 下一个 下面,我设想您的代码的实际情况(而不是人为的示例)是做一些至少稍微复杂一点的事情,以便使用块作为 其他的

        2
  •  1
  •   Vicki Laidler    16 年前

    打印元素本身没有任何意义,因为测试中已经有了它:

    if element in lst:
        print element
    

    如果需要索引,有一个索引方法:

    if element in lst:
        print lst.index(element)
    

    如果你问这个问题是因为你想循环浏览一个列表并同时处理值和索引,那么一定要使用枚举习惯用法:

    for i, val in enumerate(lst):
      print "list index": i
      print "corresponding value": val
    
        3
  •  1
  •   John La Rooy    16 年前
    >>> aList = [ ([1,2,3],4) , ([5,6,7],8) ]
    >>> element = [5,6,7]
    

    如果您只想检查第一个元素是否存在

    >>> any(element==x[0] for x in aList)
    True
    

    找到相应的值

    >>> next(x[1] for x in aList if element==x[0])
    8
    
        4
  •  0
  •   ghostdog74    16 年前
    >>> aList = [ ([1,2,3],4) , ([5,6,7],8) ]
    >>> for i in aList:
    ...     if [5,6,7] in i:
    ...          print i[-1]
    ...
    8
    
        5
  •  0
  •   Alex Martelli    16 年前

    [5, 6, 7] 一项 aList 你的表演,所以 if 会失败,你提出的问题根本不适合你。更一般地说,在这样一个 如果 如果

    where = next((x for x in aList if x[0] == element), None)
    if where:
      print(x[1])
    

    更一般地说 next 而在 print 必须依赖于 主义者 --在你的例子中, x[0] x[1] 工作得很好,但是在一个稍微不同的示例中,您可能需要不同的表达式。没有一种“通用”方式完全忽略了数据的实际结构和“神奇的工作方式”!-)

        6
  •  0
  •   hasen    16 年前

    你问题中的代码有点奇怪。但是,假设你正在学习基础知识:

    获取元素的索引:

    其实很简单: list.index(element) . 当然,假设元素只出现一次。如果它出现多次,可以使用额外的参数:

    list.index(element, start_index) :它将从这里开始搜索 start_index . 还有:
    list.index(element, start_index, end_index) :我认为这是自言自语。

    在for循环中获取索引

    enumerate 列表:

    for index, element in enumerate(some_list):
        # here, element is some_list[index]
    

    在这里, 是一个接受列表并返回元组列表的函数。说你的名单是 ['a', 'b', 'c'] 列举 [ (1, 'a'), (2, 'b'), (3, 'c') ]

    当您对其进行迭代时,每个项都是一个元组,您可以对该元组进行解包。

    元组解包基本上是这样的:

    >>> t = (1, 'a')
    >>> x, y = t
    >>> t
    (1, 'a')
    >>> x
    1
    >>> y
    'a'
    >>> 
    
        7
  •  0
  •   Pratik Deoghare    16 年前

    一个可能的解决方案。

    aList = [ ([1,2,3],4) , ([5,6,7],8) ]
    element = [5,6,7]
    
    >>> print(*[y for x,y in aList if element == x])
    8