Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why x[i][:]=x[:][i] where x is a list of lists?

Tags:

python

list

I am working on a list of lists and accessing columns has been very confusing.

Let's assume x is defined as following:

  x = [[int(np.random.rand()*100) for i in xrange(5)] for x in xrange(10)]
  pprint.pprint(x)
[[86, 92, 95, 78, 68],
 [76, 80, 44, 30, 73],
 [48, 85, 99, 35, 14],
 [3, 84, 50, 39, 47],
 [3, 7, 67, 28, 65],
 [19, 13, 98, 53, 33],
 [9, 97, 35, 25, 89],
 [48, 3, 48, 5, 1],
 [21, 40, 72, 61, 62],
 [58, 43, 84, 69, 26]]

Now, both x[1][:] and x[:][1] yield the same result:

[76, 80, 44, 30, 73]

Can someone explain why? Thank you

like image 501
ipluto Avatar asked Jul 10 '14 22:07

ipluto


People also ask

How do I list a list in Python?

Python provides an option of creating a list within a list. If put simply, it is a nested list but with one or more lists inside as an element. Here, [a,b], [c,d], and [e,f] are separate lists which are passed as elements to make a new list. This is a list of lists.

How do you check if a word is in a list of lists Python?

Given a list of lists, the task is to determine whether the given element exists in any sublist or not. Given below are a few methods to solve the given task. Method #1: Using any() any() method return true whenever a particular element is present in a given iterator.

What does for X in this list in Python mean?

Basically * indicates n number of countless elements. Example : x=1,2,4,6,9,8 print(type(x)) print(x)

Is a list a variable?

No. A list is an object.


1 Answers

The behavior is pretty simple to understand if you break up the two indexing operations of each of your expressions into separate pieces.

  • x[1] will be the second value from your list of lists (the list [76, 80, 44, 30, 73]).
  • x[1][:] is a copy of x[1] (a slice that spans the whole list).
  • x[:] is a (shallow) copy of x (the list of lists).
  • x[:][1] is the second value from the copied list of lists, which is the same object as x[1].

So, the two expressions work out to be equal. Note that because the first expression copies the list (with the [:] slice at the end), they're not both the same object (x[1][:] is x[:][1] will be False).

If you were using a 2D numpy array, you'd get different behavior, since you can slice in arbitrary dimensions (using slightly different syntax):

import numpy as np

x = np.array([[86, 92, 95, 78, 68],
              [76, 80, 44, 30, 73],
              [48, 85, 99, 35, 14],
              [3, 84, 50, 39, 47],
              [3, 7, 67, 28, 65],
              [19, 13, 98, 53, 33],
              [9, 97, 35, 25, 89],
              [48, 3, 48, 5, 1],
              [21, 40, 72, 61, 62],
              [58, 43, 84, 69, 26]])

print(x[1,:]) # prints the values of the second row: [76 80 44 30 73]
print(x[:,1]) # prints the values of the second column: [92 80 85 84  7 13 97  3 40 43]

This may be what you were looking for.

like image 136
Blckknght Avatar answered Sep 24 '22 07:09

Blckknght