0

I have the following list of tuples:

[(2010, u'S03', u'AA26FedGovAccounts', 1.90537034315564, 46385659904.0), (2010, u'S03', u'AA27StLocGovAccounts', 1.5897291595379, 58351759360.0), (2010, u'S03', u'AA28CapitalAccounts', 1.79050465110027, 95550709760.0)]

When I use: viewData[:][:] it works as expected and outputs all the data.
When I use: viewData[2][2] it works correctly and outputs 'AA28CapitalAccounts'.
The problem is that both viewData[2][:] and viewData[:][2] are giving me the same result of: (2010, u'S03', u'AA28CapitalAccounts', 1.79050465110027, 95550709760.0). I was expecting viewData[:][2] to give me a list of u'AA26FedGovAccounts', u'AA27StLocGovAccounts', and u'AA28CapitalAccounts'.

This is being run on a webserver using Django. The code is:

c = connections['default'].cursor()
c.execute("SELECT * FROM {0}.\"{1}\"".format(analysis_schema, viewName))
viewData=c.fetchall()
values = { curDesc[2][0] : str(viewData[:][2]) }
Yoel
  • 9,144
  • 7
  • 42
  • 57
Jason Hawkins
  • 625
  • 1
  • 9
  • 24

3 Answers3

1

For viewData[2][:]

viewData[2] 

returns the third element in the outer list:

(2010, u'S03', u'AA28CapitalAccounts', 1.79050465110027, 95550709760.0)

Then the [:] part returns all of that tuple:


For viewData[:][2]

viewData[:]

returns the entire outer list

Then the [2] part returns the third element:

(2010, u'S03', u'AA28CapitalAccounts', 1.79050465110027, 95550709760.0)

Which is the same as the other method.


Since [:] returns a list containing every element, you're not actually slicing anything off, just getting the same list as before.

Here Is a useful, general question about Python's slice notation.

Community
  • 1
  • 1
flau
  • 1,328
  • 1
  • 20
  • 36
1

Both of them will obviously return the same values.

viewData[:][2] will not give a list of 'AA26FedGovAccounts', 'AA27StLocGovAccounts', and 'AA28CapitalAccounts' instead you need to iterate over the list. The reason is well explained by @iwin.

The following code is what you need:

[item[2] for item in viewData]
Sudip Kafle
  • 4,286
  • 5
  • 36
  • 49
  • 1
    Thanks. That's what I did. I was already doing that for another part of constructing the values field. I got stuck in assuming a function I developed for a similar case would work with this one and didn't consider the changed logic. – Jason Hawkins Aug 12 '14 at 16:15
0

I think you are missing the lists with the arrays of numpy. If all of your sub-lists are the same size, it is possible to convert your lists to an array:

>>> import numpy as np
>>> tmp = [(2010, u'S03', u'AA26FedGovAccounts', 1.90537034315564, 46385659904.0),
           (2010, u'S03', u'AA27StLocGovAccounts', 1.5897291595379, 58351759360.0),
           (2010, u'S03', u'AA28CapitalAccounts', 1.79050465110027, 95550709760.0)]
>>> viewData = np.array(tmp)

# Column-wise
>>> print viewData[:, 2]
[u'AA26FedGovAccounts' u'AA27StLocGovAccounts' u'AA28CapitalAccounts']

# Row-wise
>>> print viewData[2, :] # same as print viewData[2]
[u'2010' u'S03' u'AA28CapitalAccounts' u'1.7905046511' u'95550709760.0']

Notice the syntax: viewData[:, 2] not viewData[:][2]

Taha
  • 709
  • 5
  • 10