解包元组时的问题...... [英] problems when unpacking tuple ...

查看:91
本文介绍了解包元组时的问题......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,


也许我盯着显示器看了太久,因为我找不到

的bug ...

我的脚本transition_filter.py从以下几行开始:


导入sys


for sys.stdin中的行:

try:

for a,b,c,d in line.split():

pass


除了ValueError,错误:

print line.split()

raise err


输出(当给出我要解析的数据时)是:

[''''','''',''0.04'',''0'']

追溯(最近一次调用最后一次):

文件" transition_filter.py",第10行,在?

引发错误

ValueError:需要超过3个值才能解压


这里出了什么问题?为什么python认为

我想将line.split()的结果解包为三个

值而不是四个?我一定很累,但我只是
看不出问题。任何线索??


谢谢,


- 哈罗德 -

Dear all,

Maybe I stared on the monitor for too long, because I cannot find the
bug ...
My script "transition_filter.py" starts with the following lines:

import sys

for line in sys.stdin :
try :
for a,b,c,d in line.split() :
pass

except ValueError , err :
print line.split()
raise err

The output (when given the data I want to parse) is:
[''0.0'',''1'',''0.04'',''0'']
Traceback (most recent call last):
File "transition_filter.py", line 10, in ?
raise err
ValueError: need more than 3 values to unpack

What is going wrong here? Why does python think that
I want to unpack the outcome of line.split() into three
values instead of four? I must be really tired, but I just
cannot see the problem. Any clues??

Thanks,

- harold -

推荐答案

>对于a,b,c,d in line.split():
> for a,b,c,d in line.split() :

[snip]
输出(当给出我要解析的数据时)是:
[''0.0'',''1'',''0.04'',''0'']
[snip]
The output (when given the data I want to parse) is:
[''0.0'',''1'',''0.04'',''0'']




你会注意到你''没有将任何参数传递给

split()。默认情况下,它会在空格上分割,并且您输入的
输入中没有任何空格。因此,你实际上只获得*一个*(而不是三个)元素。请尝试使用split(",")代替




-tkc




You''ll notice that you''re not passing any parameters to
split(). By default, it splits on whitespace, and your
input doesn''t have any whitespace in it. Thus, you''re
actually only getting *one* (not three) elements back. Try
using split(",") instead.

-tkc



harold:
输出(当给出我要解析的数据时)是:


如果你告诉我们那些数据,并告诉我们你使用的是什么版本的Python,我们可以重现问题来调查它。

ValueError:需要3个以上的值解包

为什么python认为我想将
line.split()的结果解压缩成三个值而不是四个?
The output (when given the data I want to parse) is:
If you''d told us that data, and told us what version of Python you''re
using, we could have reproduced the problem to look into it.
ValueError: need more than 3 values to unpack

Why does python think that I want to unpack the outcome of
line.split() into three values instead of four?




这不是它所说的。它说结果中只有3个值,

它需要更多(准确地说是4个)。


-
$ b $bRenéPijlman



That''s not what it says. It says there are only 3 values in the outcome,
and it needs more (4 to be precise).

--
René Pijlman




harold写道:

harold wrote:
亲爱的,

也许我盯着显示器看了太久,因为我找不到
错误...
我的脚本transition_filter.py从以下几行开始:对于sys.stdin中的行导入sys

:尝试:
对于a,b,c,d in line.split():
传递

除了ValueError,错误:
打印line.split()
提高错误

输出(当给出我要解析的数据时)是:
[''0.0'',''1',''0.04'',''0'']
追溯(最近一次通话)最后):
文件transition_filter.py,第10行,在?
引发错误
ValueError:需要超过3个值来解包

什么是错了吗?为什么python认为
我想将line.split()的结果解压缩为三个值而不是四个?我一定很累,但我只是看不出问题。任何线索??
Dear all,

Maybe I stared on the monitor for too long, because I cannot find the
bug ...
My script "transition_filter.py" starts with the following lines:

import sys

for line in sys.stdin :
try :
for a,b,c,d in line.split() :
pass

except ValueError , err :
print line.split()
raise err

The output (when given the data I want to parse) is:
[''0.0'',''1'',''0.04'',''0'']
Traceback (most recent call last):
File "transition_filter.py", line 10, in ?
raise err
ValueError: need more than 3 values to unpack

What is going wrong here? Why does python think that
I want to unpack the outcome of line.split() into three
values instead of four? I must be really tired, but I just
cannot see the problem. Any clues??




3个值来自列表''0.0'中的第一个元素 -

将其更改为' '0''你应该得到:ValueError:需要超过1个值

来解包。

见?它试图从列表中的每个元素获得a,b,c,d而不是

整个列表。


Gerard



The 3 values are coming from the first element in the list ''0.0'' -
change it to ''0'' and you should get: ValueError: need more than 1 value
to unpack.
See? it''s trying to get a,b,c,d from each element of the list not the
whole list.

Gerard


这篇关于解包元组时的问题......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆