python中的Zip与列表无法正常工作

Jaf*_*son 3 python zip numpy list python-2.7

这是我尝试过的:

>>> d
array([ 0.71428573,  0.69230771,  0.69999999], dtype=float32)
>>> f
[('name', 999), ('ddd', 33), ('mm', 112)]
>>> for n1,s1,normal in zip(d,f):
...     print(n1,s1,normal)
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: need more than 2 values to unpack
Run Code Online (Sandbox Code Playgroud)

然后我尝试了这个:

>>> for (name,confidence),normal in zip(d,f):
...     print(name,confidence,normal)
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'numpy.float32' object is not iterable
Run Code Online (Sandbox Code Playgroud)

哪里,

d = ['Jonathan Walsh','Patrick Walsh','John Welsh']
array = np.array(d)
from pyxdameraulevenshtein import damerau_levenshtein_distance_ndarray, normalized_damerau_levenshtein_distance_ndarray
 d = normalized_damerau_levenshtein_distance_ndarray('jwalsh', array)
Run Code Online (Sandbox Code Playgroud)

那么,让我知道我需要做什么来同时打印价值观?我在Windows 10上使用Python2.7.13.

Ash*_*ary 8

f 是一个嵌套列表,因此要将其项目解压缩到您需要执行的各个变量:

>>> for n1, (s1, normal) in zip(d, f):
...     print(n1, s1, normal)
...
(0.71428573, 'name', 999)
(0.69230771, 'ddd', 33)
(0.69999999, 'mm', 112)
Run Code Online (Sandbox Code Playgroud)

这基本上相当于:

>>> a, (b, c) = [1, (2, 3)]
>>> a, b, c
(1, 2, 3)
Run Code Online (Sandbox Code Playgroud)

虽然这将失败,因为a可以分配给1现在的bc只有一个项目和Python抱怨说,它需要在RHS列表中的一个多个项目或我们用LHS相同的结构.

>>> a, b, c = [1, (2, 3)]
Traceback (most recent call last):
  File "<ipython-input-9-c8a9ecc8f325>", line 1, in <module>
    a, b, c = [1, (2, 3)]
ValueError: need more than 2 values to unpack
Run Code Online (Sandbox Code Playgroud)

来自docs:

如果目标列表是以逗号分隔的目标列表:对象必须是具有与目标列表中的目标相同数量的项目的可迭代项,并且项目将从左到右分配给相应的目标.