What happens in this code?

x,y,z = [1,2,3], [4,5,6], [7,8,9] for a,b,c in x,y,z: print(a,b,c) 

Output:

 1 2 3 4 5 6 7 8 9 

I cannot mentally navigate any logic here to draw this conclusion. I know about a zip function to make this code behave the way I intend it; but I'm just trying to understand why it works this way when you are not using the zip function.

Is this intentional functionality, a function that you can iterate over multiple lists in this way sequentially? Sorting?

+7
python
source share
7 answers

You already have good answers, but I think that considering this equivalent option will help make it clearer:

 x,y,z = [1,2,3], [4,5,6], [7,8,9] for t in x,y,z: a, b, c = t print(a,b,c) 

You are not surprised that t sequentially bound to x , y and z , right? Exactly the same thing happens in your source code, except that:

 a, b, c = t 

not so obvious.

+8
source share

Oh man, this is a mess. This is too simple using python iterable unpacking . The operator a, b, c = iterable simply assigns the variables a , b and c to the elements of iterable . In this case, iterable should have 3 elements.

First you have:

 x,y,z = [1,2,3], [4,5,6], [7,8,9] # Which is: x = [1, 2, 3] y = [4, 5, 6] z = [7, 8, 9] 

then you have:

 for a, b, c in x, y, z: print(a, b, c) # Which is: temp = (x, y, z) for item in temp: a = item[0] b = item[1] c = item[2] print(a, b, c) 

One more note: the operator mytuple = 1, 2, 3 same as mytuple = (1, 2, 3) .

+6
source share

Its pretty simple code right.

  • This assigns three lists x , y and z .

     x,y,z = [1,2,3], [4,5,6], [7,8,9] 
  • This creates a tuple (x,y,z) and will iterate over each element.

     for a,b,c in x,y,z: 

    However, a,b,c means 3 objects are expected in iterables

  • Then it prints a , b and c .

      print(a,b,c) 

If you want to see what happens, I would suggest changing one of the elements in y :

 x,y,z = [1,2,3], [3,4,5,6], [7,8,9] for a,b,c in x,y,z: print(a,b,c) 1 2 3 Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: too many values to unpack 

Or by removing one of x :

 x,y,z = [1,2], [4,5,6], [7,8,9] for a,b,c in x,y,z: print(a,b,c) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: need more than 2 values to unpack 
+2
source share
 x,y,z = [1,2,3], [4,5,6], [7,8,9] 

is the same as saying

 x = [1,2,3] y = [4,5,6] z = [7,8,9] 

Further

 for a,b,c in x,y,z: 

equivalently

 for a,b,c in [x,y,z]: 

which simply says: "Take x , y and z in turn. Assign their contents to a , b and c , respectively, ie a=x[0] , b=x[1] , c=x[2] .
It eventually turns into

 a,b,c = x print(a,b,c) a,b,c = y print(a,b,c) a,b,c = z print(a,b,c) 
+2
source share

This is an unusual python thing. Implicit tuple creation.

Here you create an anonymous tuple on the right.

 x,y,z = [1,2,3], [4,5,6], [7,8,9] 

This is a similar code:

 a, b = 1, 2 

same:

 a, b = (1, 2) 

or

  a = 1 b = 2 

This allows you to use a common python trick (idiom). You can change values ​​without a temporary variable:

 a, b = b, a 

The same thing happens when the key and dictionary values ​​interact:

 for i, j in my_dict.items(): print i, j 

In your code, a different temporary tuple is created in the for loop:

 for a,b,c in (x,y,z): print(a,b,c) 

It means:

 for a, b,c in ([1,2,3], [4,5,6], [7,8,9]): print(a,b,c) 

In other words: rewrite this code for something more legible. Python does not perform its own mantra: Explicit is better than implicit.

By the way, see the fun Python Zen by typing import this in the Python shell.

+2
source share

Everything is explained here: http://docs.python.org/2/tutorial/datastructures.html#tuples-and-sequences

The first part understands that commas implicitly create tuples. So the first line is equivalent:

 x,y,z = ([1,2,3], [4,5,6], [7,8,9]) 

It also means that your for loop is equivalent:

 for a,b,c in ([1,2,3], [4,5,6], [7,8,9]): 

The second part to understand is unpacking the sequence. This means that if you assign a variable from n to n in length, Python appropriately assigns elements in the sequence. So, the first part is effective:

 x = [1,2,3] y = [4,5,6] z = [7,8,9] 

And the for loop is the same as:

 for t in (x,y,z): a = t[0] b = t[1] c = t[2] print(a,b,c) 
+1
source share

It looks like you have 3 arrays

 x = [1,2,3] y = [4,5,6] z = [7,8,9] 

and a, b, c represent elements in each array. So it looks like the for loop iterates over 3 arrays and maps the elements to a, b and c. Then print them. Again, I don't know python

0
source share

All Articles