Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.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 ValueError:需要超过1个值才能解包_Python - Fatal编程技术网

Python ValueError:需要超过1个值才能解包

Python ValueError:需要超过1个值才能解包,python,Python,我想更改由findall()函数返回的元组列表中的内容。我不确定是否可以像这样将元素从字符串更改为整数。错误总是显示我需要不止一个值 Ntuple=[] match = re.findall(r'AnswerCount="(\d+)"\s*CommentCount="(\d+)"', x) print match for tuples in match: for posts, comments in tuples: posts, comments = int(pos

我想更改由findall()函数返回的元组列表中的内容。我不确定是否可以像这样将元素从字符串更改为整数。错误总是显示我需要不止一个值

Ntuple=[]

match = re.findall(r'AnswerCount="(\d+)"\s*CommentCount="(\d+)"', x)

print match

for tuples in match:
    for posts, comments in tuples:
        posts, comments = int(posts), (int(posts)+int(comments))  ## <- error

print match
Ntuple=[]
match=re.findall(r'AnswerCount=“(\d+)”\s*CommentCount=“(\d+””,x)
打印匹配
对于匹配的元组:
对于帖子,元组中的注释:

posts,comments=int(posts),(int(posts)+int(comments))###
match
是元组列表。对其进行迭代的正确方法是:

  matches = re.findall(r'AnswerCount="(\d+)"\s*CommentCount="(\d+)"', x)
  for posts, comments in matches:
    posts, comments = int(posts), (int(posts)+int(comments))

从字符串到整数的转换很好。

问题出在
行中,对于以元组表示的帖子、注释:
。这里的
tuples
实际上是一个包含两个字符串的元组,因此不需要对其进行迭代。您可能需要以下内容:

matches = re.findall(...)
for posts, comments in matches:
    ....

match中元组的
是正确的,因为match是
re.findall
(即列表)的返回值。虽然它的名字很混乱。是的,这次没有出现错误。但是,当我打印新的匹配项以查看元组的更改时,它仍然保持不变。为什么我的元组作业根本不起作用?@interjay:是的,谢谢。我认为列表的元素是tuple,tuple的元素是两个“posts”和“comments”,因此我为循环编写了两个。似乎一个元组不能在for循环中交互。这就是为什么它看起来是一个错误。我说得对吗?:)@user942891:您可以迭代一个元组,但是一次只能得到一个字符串。这里需要同时获取两个字符串,以便将它们分配给
posts
comments
变量。当您指定多个这样的变量时,元组将自动解压,因此无需对其进行迭代。@interjay:谢谢。这次,我明白了。而且,我还有一些问题要解决。为什么“posts,comments=int(posts),(int(posts)+int(comments))”根本没有改变元组列表??我真的很感谢你的帮助@AnneS:该行仅更改变量
posts
comments
的值。如果要更改列表,则必须更新其元素。我会创建一个新的列表,例如通过使用列表理解:
newList=[(int(posts),int(posts)+int(comments)),对于posts,匹配中的comments]