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. 16 1355
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 correctly
No, 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 z
Typo here, of course I mean y,z = x.
-Carsten
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.
It makes perfect sense: in fact, you have kind of explained it
yourself!
Think of the for statement as returning the next element of some
sequence; in this case it's a tuple. Then on the left side, the
unpacking occurs. Using "for x in t", means that effectively no
unpackig occurs, so you get the tuple. However, since the in is
returning a tuple, using "for x,y in t", the tuple returned gets
unpacked.
Hope that helps.
Jon.
At Friday 20/10/2006 16: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')) 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 conceptualiz e how. I thought 'for x,y in t' would only work if t returned a two-tuple, which it doesn't.
You can think of
for x in t:
whatever
as meaning "for each element contained in t, name it x and do whatever"
The other concept involved is unpacking:
>>w = (1,2,3) x,y,z = w x
1
When you say "for x,y in t:" there is an implicit unpacking, it means
"for each element contained in t, unpack it into x and y and do whatever"
>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
No, it acts like:
for w in t:
x,y = w
...
--
Gabriel Genellina
Softlab SRL
_______________ _______________ _______________ _____
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar jo********@gmai l.com wrote:
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'
Yes, I understand that. What confused me was if it had been written like
this:
pair = (("California", "San Francisco"))
Carsten Haese wrote:
You seem to have difficulty distinguishing the concept of looping over a
tuple from the concept of unpacking a tuple.
I think you're right. It's starting to make more sense now. I think when
I saw:
for x,y in t
I was expecting 't' to be a two-tuple for it to work. Maybe writing it as:
for (x,y) in t
sort of helps to show that '(x,y)' is equivalent to one object in 't'.
That makes it look a little more cohesive in my mind, I guess, or helps
me to see it map out against 't' better.
In <%b************ ****@news.tufts .edu>, John Salerno wrote: jo********@gmai l.com wrote:
>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'
Yes, I understand that. What confused me was if it had been written like
this:
pair = (("California", "San Francisco"))
Uhm, you mean::
pair = (("California", "San Francisco"),)
Note the extra comma to make that "a tuple in a tuple".
Ciao,
Marc 'BlackJack' Rintsch
At Friday 20/10/2006 17:29, John Salerno wrote:
>I was expecting 't' to be a two-tuple for it to work. Maybe writing it as:
for (x,y) in t
sort of helps to show that '(x,y)' is equivalent to one object in 't'. That makes it look a little more cohesive in my mind, I guess, or helps me to see it map out against 't' better.
Note that it's the *comma* in an expression list what creates a
tuple, *not* the parens. A similar rule applies on the target side of
an assignment:
x,y = (1,2)
(x,y) = [1,2]
[x,y] = 1,2
and all variations are all equivalent.
The left part of a for statement is like an assignment.
With this in mind, it's not surprise that
for (x,y) in t: pass
for x,y in t: pass
are exactly the same.
<http://docs.python.org/ref/exprlists.html>
<http://docs.python.org/ref/assignment.html >
--
Gabriel Genellina
Softlab SRL
_______________ _______________ _______________ _____
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Dave Benjamin |
last post by:
Hey all,
I just realized you can very easily implement a sequence grouping function
using Python 2.3's fancy slicing support:
def group(values, size):
return map(None, * for i in range(size)])
>>> group(range(20), 4)
|
by: Jon Perez |
last post by:
I saw this code snippet:
sock.listen(20)
for _ in range(20):
newsock, client_addr = sock.accept()
print "Client connected:", client_addr
data = ""
why use _ for this example? Is there any
optimization to be had using it?
|
by: Paul McGuire |
last post by:
I'm trying to manage some configuration data in a list of tuples, and I
unpack the values with something like this:
configList =
for data in configList:
name,a,b,c = data
... do something with a,b, and c
Now I would like to add a special fourth config value to "T":
("T",1,5,4,0.005),
|
by: Chris |
last post by:
Hi
I'm attempting to write a client for an existing p2p network.
The protocol defines that ints are packed into 4 bytes for transfer.
// Creating the byte vector using the following is fine:
for(int i=3; i>=0; i--) {
vecBuff.push_back(value&0xff);
value/=256;
}
|
by: Claudio Grondi |
last post by:
I need to unpack on a Windows 2000 machine
some Wikipedia media .tar archives which are
compressed with TAR 1.14 (support for long file
names and maybe some other features) .
It seems, that Pythons tarfile module is able to list far
more files inside the archives than WinRAR or 7zip or
TotalCommander, but will it unpack all available files
(largest archive size 17 GByte)?
If tarfile is build on TAR 1.14 or TAR 1.15 it will be
| |
by: Suyog_Linux |
last post by:
I wish to know how the free()function knows how much memory to be
freed as we only give pointer to allocated memory as an argument to
free(). Does system use an internal variable to store allocated memory
when we use malloc().
Plz help......
|
by: ram |
last post by:
Stupid question #983098403:
I can't seem to pass an unpacked sequence and keyword arguments to a
function at the same time. What am I doing wrong?
def f(*args, **kw):
for a in args:
print 'arg:', a
for (k,v) in kw.iteritems():
print k, '=', v
|
by: Soren S. Jorgensen |
last post by:
Hi,
I'm trying to read some messages (native structs) from a kernel mode
mini-filter driver. I'm using my own implementation of IAsyncResult to
pack/unpack the NativeOverlapped structure, and waiting for the result to
arrive.
To read the messages frm the kernek driver i'm using native function:
HRESULT WINAPI FilterGetMessage(
IN HANDLE hPort,
|
by: Martin Geisler |
last post by:
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
iEYEARECAAYFAkjlQNwACgkQ6nfwy35F3Tj8ywCgox+XdmeDTAKdN9Q8KZAvfNe4
0/4AmwZGClr8zmonPAFnFsAOtHn4JhfY
=hTwE
-----END PGP SIGNATURE-----
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |