Python:在列表中查找 [英] Python: Find in list

查看:195
本文介绍了Python:在列表中查找的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  item = someSortOfSelection()
if myList中的item:
doMySpecialFunction(item)

但是有时它不适用于我所有的项目,就好像它们不是在列表中被识别(当它是一个字符串列表)。

是在列表中找到一个项目的最pythonic方法:<$ c $至于你的第一个问题:那个代码是完美无缺的,这个代码是完美的。如果 item 等于 myList 中的元素之一,那么应该工作正常。也许你试图找到一个不匹配其中一个项目的字符串,或者你正在使用一个不准确的浮点值。



至于你的第二个问题:实际上有几种可能的方法,如果在列表中找到的东西。



检查是否有内容



这是您描述的用例:检查列表中是否有内容。如您所知,您可以在运算符中使用

  3 in [1,2,3]#=> True 



过滤集合



即,找到满足一定条件的序列中的所有元素。您可以使用列表理解或生成器表达式:

  matches = [x for x in x如果fulfills_some_condition(x)] 
matches =(x for x in lst if x> 6)

后者将返回一个生成器,你可以将它想象成一种懒惰的列表,只有当你遍历它时才会生成。顺便说一句,第一个完全相同于

$ p $ matches = filter(fulfills_some_condition,lst)

在这里,您可以看到工作中的高阶函数。在Python 3中, filter 不会返回一个列表,而是一个类似生成器的对象。



找到第一个如果你只想匹配一个条件的第一个东西(但你不知道它是什么),那么使用for循环(可能使用还有 else 子句,这并不是真正的众所周知的)。您也可以使用

 下一个(x for lst if ...)

这将返回第一个匹配,或者如果找不到,则会引发 StopIteration 。或者,您可以使用

 下一个((x for lst中的x,如果...),[默认值])



寻找物品的位置



,还有 index 方法,如果你想知道列表中某个元素的位置,有时可能有用:

b
$ b

  [1,2,3] .index(2)#=> 1 
[1,2,3] .index(4)#=> ValueError

但是,请注意,如果您有重复, .index 始终返回最低的索引:

$ $ $ $ $ $ $ $ $ 1,2,3,2] .index(2)#= > 1

如果有重复项,并且您想要所有索引,则可以使用枚举()代替:

  3,2])if x == 2]#=> [1,3] 


I have come across this delightful:

item = someSortOfSelection()
if item in myList:
    doMySpecialFunction(item)

but sometimes it does not work with all my items, as if they weren't recognized in the list (when it's a list of string).

is it the most 'pythonic' way of finding an item in a list: if x in l:?

解决方案

As for your first question: that code is perfectly fine and should work if item equals one of the elements inside myList. Maybe you try to find a string that does not exactly match one of the items or maybe you are using a float value which suffers from inaccuracy.

As for your second question: There's actually several possible ways if "finding" things in lists.

Checking if something is inside

This is the use case you describe: Checking whether something is inside a list or not. As you know, you can use the in operator for that:

3 in [1, 2, 3] # => True

Filtering a collection

That is, finding all elements in a sequence that meet a certain condition. You can use list comprehension or generator expressions for that:

matches = [x for x in lst if fulfills_some_condition(x)]
matches = (x for x in lst if x > 6)

The latter will return a generator which you can imagine as a sort of lazy list that will only be built as soon as you iterate through it. By the way, the first one is exactly equivalent to

matches = filter(fulfills_some_condition, lst)

in Python 2. Here you can see higher-order functions at work. In Python 3, filter doesn't return a list, but a generator-like object.

Finding the first occurrence

If you only want the first thing that matches a condition (but you don't know what it is yet), it's fine to use a for loop (possibly using the else clause as well, which is not really well-known). You can also use

next(x for x in lst if ...)

which will return the first match or raise a StopIteration if none is found. Alternatively, you can use

next((x for x in lst if ...), [default value])

Finding the location of an item

For lists, there's also the index method that can sometimes be useful if you want to know where a certain element is in the list:

[1,2,3].index(2) # => 1
[1,2,3].index(4) # => ValueError

However, note that if you have duplicates, .index always returns the lowest index:

[1,2,3,2].index(2) # => 1

If there are duplicates and you want all the indexes then you can use enumerate() instead:

[i for i,x in enumerate([1,2,3,2]) if x==2] # => [1, 3]

这篇关于Python:在列表中查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆