Zip in python doesn't work as expected with lists
Here's what I've tried:
>>> 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
Then I tried this:
>>> 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
Where
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)
Please let me know what I need to do to print values ββat the same time? I am using Python2.7.13 on Windows 10.
source to share
f
is a nested list, hence to unpack its element into separate variables you need to do:
>>> for n1, (s1, normal) in zip(d, f):
... print(n1, s1, normal)
...
(0.71428573, 'name', 999)
(0.69230771, 'ddd', 33)
(0.69999999, 'mm', 112)
This is mostly equivalent to:
>>> a, (b, c) = [1, (2, 3)]
>>> a, b, c
(1, 2, 3)
Until this happens, because it a
can be assigned 1
, but now there is only one element for b
and c
, and Python complains that it needs another element in the RHS list, or we are using the same structures on the 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
From the docs:
If the target list is a comma-separated list of targets: the object must be iterable with the same number of elements as the targets in the target list, and the elements are assigned from left to right to match the targets.
source to share