为什么这个拆包工作 [英] why does this unpacking work
问题描述
我有点困惑,但我确定这是微不足道的。我很困惑为什么会这样做:
>> t =((''hello'',''goodbye''),
(''more' ',''少''),
('''''''''没什么''),
(''好'',''坏' '))
>> t
((''hello'',''goodbye''),(''more'',''less''),('''''', ''没什么''),
(''好'',''坏''))
< blockquote class =post_quotes>
>> for x in t:
print x
('h ello'',''再见'')
(''more'',''less'')
('''''''''没什么' ')
(''好'',''坏'')
>> for x,y in t:
print x,y
你好再见
更少
什么都没有
好坏
>>>
我知道t返回一个包含其他元组的元组。
然后''代表x in t''返回嵌套元组本身。
但是我不明白为什么你可以使用''代表x,y in t''当t
真的只返回一件事。我看到这有效,但我不能完全概念化如何。我认为''对于x,y in t''只会在t $ / $
返回两元组时才会起作用,这不是。
似乎正在发生的事情是''for x,y in t''表现得像:
for x in t:
for y,z在x:
#然后它正确地完成它
但如果是这样,为什么会这样?这似乎不是非常直观的行为。
谢谢。
它是'只是序列拆包。您知道这有效吗?:
pair =(" California,San Francisco)
州,城市=对>
打印城市
#''旧金山''
打印状态
#''加州''
John Salerno写道:
我有点困惑,但我确定这是微不足道的。我很困惑为什么会这样做:
>> t =(( ''你好'',''再见''),
(''more'',''less''),
(''''',''没什么''),
(''好'',''坏''))
>> t
((''hello'',''再见''),(''更多'',''少''),('''''','没什么''),
(''good'','''坏''))
>> for x in t:
打印x
(''你好'',''再见'')
(''more'', ''少'')
(''某事' ,''没什么'')
(''好'',''坏'')
>> for x,y in t:
print x,y
hello再见
更少
什么都没有
好坏
>>>
我知道t会返回一个包含其他元组的元组。
然后''for x in t''返回嵌套元组本身。
但我不明白为什么你可以使用''for x,y in t''t
真的只返回一件事。我看到这有效,但我不能完全概念化如何。我认为''对于x,y in t''只会在t $ / $
返回两元组时才会起作用,这不是。
似乎正在发生的事情是''for x,y in t''表现得像:
for x in t:
for y,z在x:
#然后它正确地完成它
但如果是这样,为什么会这样?这似乎不是非常直观的行为。
谢谢。
在星期五,2006-10-20 15:14,John Salerno写道:
我有点困惑,但我确定这是微不足道的。我很困惑为什么会这样做:
>> t =(( ''你好'',''再见''),
(''more'',''less''),
(''''',''没什么''),
(''好'',''坏''))
>> t
((''hello'',''再见''),(''更多'',''少''),('''''','没什么''),
(''good'','''坏''))
>> for x in t:
打印x
(''你好'',''再见'')
(''more'', ''少'')
(''某事' ,''没什么'')
(''好'',''坏'')
>> for x,y in t:
print x,y
hello再见
更少
什么都没有
好坏
>>>
我知道t返回一个包含其他元组的元组。
t不会返回任何事情,t *是*嵌套元组。
然后''for x in t''返回嵌套元组本身。
它再次没有返回任何东西。它将元组的每个元素分配给x,逐个执行,为每个元素执行循环体。
但我不知道我明白为什么你可以使用''代表x,y in t''当t $ / b $ b实际上只返回一件事。我看到这有效,但我不能完全概念化如何。我认为''for x,y in t''只有在
返回两元组的情况下才有效,但事实并非如此。
您正在考虑x,y = t。
似乎正在发生的是''for x,y in t''表现得像:
for x in t:
for y,z in x:
#然后它正确地做了
不,它实际上表现得像
for x in t:
y,z = t
#用y和z做点什么
你好像有困难从解包元组的概念中区分循环的概念。\\ b $
元组。这个难度是因为在上面的例子中,你正在循环使用
a元组元组并在运行中解包每个内部元组。
希望这会有所帮助,
Carsten。
周五,2006-10-20 at 15:37,Carsten Haese写道:
for x in t:
y,z = t
#做y和z
这里的错字,当然我的意思是y,z = x。
-Carsten
I''m a little confused, but I''m sure this is something trivial. I''m
confused about why this works:
>>t = ((''hello'', ''goodbye''),
(''more'', ''less''),
(''something'', ''nothing''),
(''good'', ''bad''))
>>t
((''hello'', ''goodbye''), (''more'', ''less''), (''something'', ''nothing''),
(''good'', ''bad''))
>>for x in t:
print x
(''hello'', ''goodbye'')
(''more'', ''less'')
(''something'', ''nothing'')
(''good'', ''bad'')
>>for x,y in t:
print x,y
hello goodbye
more less
something nothing
good bad
>>>
I understand that t returns a single tuple that contains other tuples.
Then ''for x in t'' returns the nested tuples themselves.
But what I don''t understand is why you can use ''for x,y in t'' when t
really only returns one thing. I see that this works, but I can''t quite
conceptualize how. I thought ''for x,y in t'' would only work if t
returned a two-tuple, which it doesn''t.
What seems to be happening is that ''for x,y in t'' is acting like:
for x in t:
for y,z in x:
#then it does it correctly
But if so, why is this? It doesn''t seem like very intuitive behavior.
Thanks.
It''s just sequence unpacking. Did you know that this works?:
pair = ("California","San Francisco")
state, city = pair
print city
# ''San Francisco''
print state
# ''California''
John Salerno wrote:I''m a little confused, but I''m sure this is something trivial. I''m
confused about why this works:
>>t = ((''hello'', ''goodbye''),
(''more'', ''less''),
(''something'', ''nothing''),
(''good'', ''bad''))>>t
((''hello'', ''goodbye''), (''more'', ''less''), (''something'', ''nothing''),
(''good'', ''bad''))>>for x in t:
print x
(''hello'', ''goodbye'')
(''more'', ''less'')
(''something'', ''nothing'')
(''good'', ''bad'')>>for x,y in t:
print x,y
hello goodbye
more less
something nothing
good bad>>>
I understand that t returns a single tuple that contains other tuples.
Then ''for x in t'' returns the nested tuples themselves.
But what I don''t understand is why you can use ''for x,y in t'' when t
really only returns one thing. I see that this works, but I can''t quite
conceptualize how. I thought ''for x,y in t'' would only work if t
returned a two-tuple, which it doesn''t.
What seems to be happening is that ''for x,y in t'' is acting like:
for x in t:
for y,z in x:
#then it does it correctly
But if so, why is this? It doesn''t seem like very intuitive behavior.
Thanks.
On Fri, 2006-10-20 at 15:14, John Salerno wrote:I''m a little confused, but I''m sure this is something trivial. I''m
confused about why this works:
>>t = ((''hello'', ''goodbye''),
(''more'', ''less''),
(''something'', ''nothing''),
(''good'', ''bad''))>>t
((''hello'', ''goodbye''), (''more'', ''less''), (''something'', ''nothing''),
(''good'', ''bad''))>>for x in t:
print x
(''hello'', ''goodbye'')
(''more'', ''less'')
(''something'', ''nothing'')
(''good'', ''bad'')>>for x,y in t:
print x,y
hello goodbye
more less
something nothing
good bad>>>
I understand that t returns a single tuple that contains other tuples.t doesn''t "return" anything, t *is* a nested tuple.
Then ''for x in t'' returns the nested tuples themselves.It again doesn''t "return" anything. It assigns each element of tuple t
to x, one by one, executing the loop body for each element.
But what I don''t understand is why you can use ''for x,y in t'' when t
really only returns one thing. I see that this works, but I can''t quite
conceptualize how. I thought ''for x,y in t'' would only work if t
returned a two-tuple, which it doesn''t.You''re thinking of "x,y = t".
What seems to be happening is that ''for x,y in t'' is acting like:
for x in t:
for y,z in x:
#then it does it correctlyNo, it''s actually behaving like
for x in t:
y,z = t
# do something with y and z
You seem to have difficulty distinguishing the concept of looping over a
tuple from the concept of unpacking a tuple. This difficulty is
compounded by the fact that, in your example above, you are looping over
a tuple of tuples and unpacking each inner tuple on the fly.
Hope this helps,
Carsten.
On Fri, 2006-10-20 at 15:37, Carsten Haese wrote:for x in t:
y,z = t
# do something with y and zTypo here, of course I mean y,z = x.
-Carsten
这篇关于为什么这个拆包工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!