Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.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 使用zip对象的索引访问其值_Python - Fatal编程技术网

Python 使用zip对象的索引访问其值

Python 使用zip对象的索引访问其值,python,Python,我试图理解如何访问zip对象,并试图通过使用.index()来了解如何使用索引访问zip对象中的值,就像我们之前在Python 2.x中使用的那样,但它似乎在Python3中不起作用 这是密码 def find_neighbors(index): i, j = index print([(i + 1, j), (i - 1, j), (i, j + 1), (i, j - 1)]) return [(i + 1, j), (i - 1, j), (i, j + 1), (

我试图理解如何访问zip对象,并试图通过使用.index()来了解如何使用索引访问zip对象中的值,就像我们之前在Python 2.x中使用的那样,但它似乎在Python3中不起作用

这是密码

def find_neighbors(index):
    i, j = index
    print([(i + 1, j), (i - 1, j), (i, j + 1), (i, j - 1)])
    return [(i + 1, j), (i - 1, j), (i, j + 1), (i, j - 1)]

list1 = (211,209,210,210)
list2 = (72,72,73,71)
points = zip(list1,list2)

for i, index in enumerate(points):
    for x in find_neighbors(index):
          if x is not in points: continue
          j = points.index(x)
当我运行代码时,我得到以下错误:AttributeError:'zip'对象没有属性'index'


有没有一种新的方法来执行相同的机制

看起来你希望
是一个列表,但是
zip
对象不是列表

如果要将其转换为列表,请执行以下操作:

points = list(zip(list1,list2))

zip
用于在Python 2中返回列表,但现在在Python 3中是一个生成器,因此必须先使用
list
构造函数将其转换为列表,然后才能使用
index
方法:

points = list(zip(list1,list2))

正如其他人所说:zip返回一个zip对象,它是一个迭代器,您可以使用它生成列表或字典等其他内容。另外,(a,b,c)返回一个元组,而[a,b,c]返回一个列表

>>> type((1,2,3))
<class 'tuple'>
>>> type([1,2,3])
<class 'list'>
>>> 
请注意,我在您的数据中添加了一个相邻点,以便我们可以看到函数find it。在对一个路由或另一个路由进行过多的工作之前,先考虑一下列表和元组之间的区别。我的代码是用Python 3.7编写的


最后,请记住,您可以使用字典
pointDict=dict(zip(list1,list2))
,这在您的程序中可能更有用,尤其是当您需要查找内容时。它可能更像python。

当我转换它时,现在我得到了ValueError:(211,72)不在列表中,即使我在压缩对象上有值(211,72)??你确定这是你真正的代码吗?如果x不在中,则它包含一个语法错误
,但在修复后,我没有得到您描述的错误。
def find_neighbors(point):
i = point[0]
j = point[1]
print(f"List of neighbours of {point}: {[(i + 1, j), (i - 1, j), (i, j + 1), (i, j - 1)]}")
return [(i + 1, j), (i - 1, j), (i, j + 1), (i, j - 1)]

list1 = [211,209,210,210,211]
list2 = [72,72,73,71,73]
pointsList = list(zip(list1,list2))

#These loops are just to show what is going on comment them out or delete them
for i in pointsList:
    print(i)
    print(f"The first element is {i[0]} and the second is {i[1]}")

#or let Python unpack the tuples - it depends what you want
for i, j in pointsList:
    print(i,j)

for point in pointsList:
print(f"In the loop for find_neighbours of: {point}")
for testPoint in find_neighbors(point):
    print(f"Testing {testPoint}")
    if testPoint not in pointsList:
        print(f"Point {testPoint} is not there.")
        continue
    elif testPoint in pointsList:
        print(f"*******************Point {testPoint} is there.***************************")