how to fix "not enough values to unpack (expected 3, got 1)" with multi input value with list
Question:
I want to input multi coordinate value like (x1,y1,z1),(x2y2z2)…,(xn,yn,zn) to the input
program:
def coordinate(getdata):
l = getdata
lx = [x for x,y,z in l]
ly = [y for x,y,z in l]
lz = [z for x,y,z in l]
x = lx
xv = []
for f in range(len(x)-1):
bx = x[f+1] - x[f]
xv.append(bx)
print(xv)
y = ly
yv = []
for g in range(len(y)-1):
by = y[g+1] - y[g]
yv.append(by)
print(yv)
z = lz
zv = []
for h in range(len(z)-1):
bz = z[h+1] - z[h]
zv.append(bz)
print(zv)
while True:
getdata = input("coordinate in (x, y, z):")
coordinate(getdata)
the error message:
not enough values to unpack (expected 3, got 1)
I did try to fix this problem, and do the test like following
while True:
getdata = input("coordinate in (x, y, z):")
print (getdata)
print ([getdata])
the result:
(x1,y1,z1),(x2y2z2)...,(xn,yn,zn)
['(x1,y1,z1),(x2y2z2)...,(xn,yn,zn)']
then i did the other test like following:
l = ['(x1,y1,z1),(x2y2z2)...,(xn,yn,zn)']
lx = [x for x,y,z in l]
ly = [y for x,y,z in l]
lz = [z for x,y,z in l]
then it shows:
not enough values to unpack (expected 3)
Answers:
User input is treated as a string, as shown in the code you posted. You will need to evaluate the user input as Python code using ast.literal_eval()
:
import ast
getdata = input("coordinate in (x, y, z):")
l = ast.literal_eval(f"[{getdata}]")
lx = [x for x,y,z in l]
ly = [y for x,y,z in l]
lz = [z for x,y,z in l]
print(lx, ly, lz)
Sample output:
coordinate in (x, y, z):(1,2,3),(4,5,6),(7,8,9)
[1, 4, 7] [2, 5, 8] [3, 6, 9]
I am assuming that you are trying this with Python 3.x
In Python 3.x the input function always returns a user input as string only.
Because of this you get an exception when the statement
lx=[x for x,y,z in l] runs as it gets only one string object but it is expecting 3 objects.
You need to convert the input to a list of tuples before executing the list comprehension statements.
In Python 2.x the above code works without any issues as the input function interpreters the user input and converts that to appropriate type.
I want to input multi coordinate value like (x1,y1,z1),(x2y2z2)…,(xn,yn,zn) to the input
program:
def coordinate(getdata):
l = getdata
lx = [x for x,y,z in l]
ly = [y for x,y,z in l]
lz = [z for x,y,z in l]
x = lx
xv = []
for f in range(len(x)-1):
bx = x[f+1] - x[f]
xv.append(bx)
print(xv)
y = ly
yv = []
for g in range(len(y)-1):
by = y[g+1] - y[g]
yv.append(by)
print(yv)
z = lz
zv = []
for h in range(len(z)-1):
bz = z[h+1] - z[h]
zv.append(bz)
print(zv)
while True:
getdata = input("coordinate in (x, y, z):")
coordinate(getdata)
the error message:
not enough values to unpack (expected 3, got 1)
I did try to fix this problem, and do the test like following
while True:
getdata = input("coordinate in (x, y, z):")
print (getdata)
print ([getdata])
the result:
(x1,y1,z1),(x2y2z2)...,(xn,yn,zn)
['(x1,y1,z1),(x2y2z2)...,(xn,yn,zn)']
then i did the other test like following:
l = ['(x1,y1,z1),(x2y2z2)...,(xn,yn,zn)']
lx = [x for x,y,z in l]
ly = [y for x,y,z in l]
lz = [z for x,y,z in l]
then it shows:
not enough values to unpack (expected 3)
User input is treated as a string, as shown in the code you posted. You will need to evaluate the user input as Python code using ast.literal_eval()
:
import ast
getdata = input("coordinate in (x, y, z):")
l = ast.literal_eval(f"[{getdata}]")
lx = [x for x,y,z in l]
ly = [y for x,y,z in l]
lz = [z for x,y,z in l]
print(lx, ly, lz)
Sample output:
coordinate in (x, y, z):(1,2,3),(4,5,6),(7,8,9)
[1, 4, 7] [2, 5, 8] [3, 6, 9]
I am assuming that you are trying this with Python 3.x
In Python 3.x the input function always returns a user input as string only.
Because of this you get an exception when the statement
lx=[x for x,y,z in l] runs as it gets only one string object but it is expecting 3 objects.
You need to convert the input to a list of tuples before executing the list comprehension statements.
In Python 2.x the above code works without any issues as the input function interpreters the user input and converts that to appropriate type.