Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.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_List_Position_Term - Fatal编程技术网

根据Python中的值在列表中查找位置?

根据Python中的值在列表中查找位置?,python,list,position,term,Python,List,Position,Term,我对Python有点陌生,我正在研究一些新的代码形式。 我在两个列表中选择了5个介于1和20之间的随机数。像这样 list = [] listn = [] import random for i in range(5): newvar = random.randomint(1,20) list.append(newvar) newvart = random.randomint(1,20) listn.append(newvart) 然后在同一代码中选择另

我对Python有点陌生,我正在研究一些新的代码形式。 我在两个列表中选择了5个介于1和20之间的随机数。像这样

list = []
listn = []
import random
for i in range(5):
     newvar = random.randomint(1,20)
     list.append(newvar)
     newvart = random.randomint(1,20)
     listn.append(newvart)
然后在同一代码中选择另一个变量

evar = random.randomint(1,20)
我想做的是看看这个数字是否在两个列表中,如果是,它们是否在列表中的相同位置。首先,我应该做以下几点:

if (evar in list) and (evar in listn):

但我不知道怎么做剩下的。我想知道evar是否在两个列表中,并且在两个列表中的位置是否相同(即,它是list和listn中的第三个数字)。我该怎么做呢?

假设第一次找到的位置应该是相同的,使用:

允许所有位置使用:

或者更好:
zip()
基于以下建议的解决方案:

注意:找到第一个
True
项时立即返回,而无需不必要地枚举其余项。

编辑:这与JF在下面的评论中发布的一行中的基本思想相同:

if (evar in list):
    if (evar2 in listn):
        if (evar == evar2 and list.index(evar2) == listn.index(evar2):
            do_something()
any(x1==x2==x1的evar,zip中的x2(列表1,列表2))


以下是一些额外的提示:

  • 应避免使用
    list
    作为变量名,因为它会隐藏内置变量
  • 没有随机的。随机的,我猜你的意思是随机的
  • 生成随机列表可以通过列表理解而不是循环很好地完成,如下所示:
    [random.randint(1,20)for uu in xrange(5)]

Python在这些方面非常简单,非常容易处理。只需使用list.index(var),它返回var在列表中得到的索引

from random import randint 
list1 = []
list2 = []
for i in range(5):
    list1.append(randint(1,20))
    list2.append(randint(1,20))
evan = randint(1,20) 
if (evan in list1 and evan in list 2) and (list1.index(evan) == list2.index(evan)):
    print 'They are at the same place'.

如果您需要知道匹配发生的索引,可以使用如下内容

try:
    idx = next(i for i,x in enumerate(list1) if evar==x==list2[i])
    ...
except StopIteration:
    # not found
    ...
或者更简单地使用J.F.塞巴斯蒂安的建议

idx = next((i for i,x in enumerate(list1) if evar==x==list2[i]), -1)

如果没有匹配项,将返回
-1

注意:它可能位于两个不同位置的一个列表中。注意-不要将
列表
用作变量名!您将对内置类型
list
及其构造函数
list()
进行阴影处理。您需要知道它们匹配的位置吗?啊,是的,非常好。我将把我的一行代码编辑为该行,因为它将正确短路,并更紧密地反映答案中函数的逻辑。索引将只返回第一次出现的情况,如果
idx
,则列表1中的第二次出现可能与列表2中的第一次出现相匹配,即使在“未找到”中也应该有值然后可以抑制案例
StopIteration
idx=next(…),not_found_idx)
@J.F.Sebastian,非常好。我认为
-1
可以,因为这就是
str.find()所做的
>>> def foo(list1, list2, evar):
...   for x1, x2 in zip(list1, list2):
...     if x1 == x2 == evar:
...       return True
...   else:
...     return False
... 
>>> foo([1, 2, 69, 3], [3, 4, 69, 5], 69)
True
>>> foo([1, 2, 69, 3], [3, 4, 69, 5], 3)
False
>>> foo([1, 2, 2, 3], [3, 4, 2, 5], 2)
True
from random import randint 
list1 = []
list2 = []
for i in range(5):
    list1.append(randint(1,20))
    list2.append(randint(1,20))
evan = randint(1,20) 
if (evan in list1 and evan in list 2) and (list1.index(evan) == list2.index(evan)):
    print 'They are at the same place'.
try:
    idx = next(i for i,x in enumerate(list1) if evar==x==list2[i])
    ...
except StopIteration:
    # not found
    ...
idx = next((i for i,x in enumerate(list1) if evar==x==list2[i]), -1)