1

I have an array:

[(0, b'C', 5.8816, 6.0, 0.1184, 4.2631, 4.2631, 0.0)
 (1, b'H', 0.8495, 1.0, 0.1505, 0.951, 0.951, 0.0)
 (2, b'Br', 35.0064, 35.0, -0.0064, 1.2192, 1.2192, -0.0)
 (3, b'Cl', 17.0401, 17.0, -0.0401, 1.2405, 1.2405, -0.0)
 (4, b'F', 9.2225, 9.0, -0.2225, 1.0449, 1.0449, -0.0)]

from which I want to extract single columns of floating point numbers fro use further on in the program.

When I try the following:

QA = []
idx_IN_columns = [5]
QA = data[idx_IN_columns]

I get:

Traceback (most recent call last):

  File "<ipython-input-22-4e6a1b6a3f36>", line 1, in <module>
    runfile('C:/Users/Steve/Python/Testing/ReadFile_mpa_1.py', wdir='C:/Users/Steve/Python/Testing')

  File "C:\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
    execfile(filename, namespace)

  File "C:\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)
    enter code here

  File "C:/Users/Steve/Python/Testing/ReadFile_mpa_1.py", line 34, in <module>
    QA = data[idx_IN_columns]

IndexError: index 5 is out of bounds for axis 1 with size 5

Assistance will be much appreciated.

Thanks in advance

4
  • are you trying to get the element with index 5 from the array? Commented May 7, 2017 at 15:37
  • 2
    the array is confusing, is it a 2d array of just 1d array with tuple as its element. are you mising the commas? Commented May 7, 2017 at 15:38
  • @Steve Note that your full name shows in the paths of the error message you posted. Commented May 7, 2017 at 15:54
  • @Steve I edited that, but it still is in the history. Commented May 7, 2017 at 16:06

4 Answers 4

1

You cannot access a list element with a list as index (see Sequence Types), you need to iterate. Your data sets are Tuples; they are similar to Lists, but cannot be modified. By the way, your data is malformed (lacking commas):

#!python3
# coding=utf-8

l = [
    (0, b'C', 5.8816, 6.0, 0.1184, 4.2631, 4.2631, 0.0),
    (1, b'H', 0.8495, 1.0, 0.1505, 0.951, 0.951, 0.0),
    (2, b'Br', 35.0064, 35.0, -0.0064, 1.2192, 1.2192, -0.0),
    (3, b'Cl', 17.0401, 17.0, -0.0401, 1.2405, 1.2405, -0.0),
    (4, b'F', 9.2225, 9.0, -0.2225, 1.0449, 1.0449, -0.0)
    ]

print( [e[2] for e in l] ) # list comprehension
# [5.8816, 0.8495, 35.0064, 17.0401, 9.2225]

# equivalent loop
result = []
for e in l:
    result.append(e[2])
print(result)


A little more advanced, as generator function:

# iterator
def col(l, n=0):
    for e in l:
        yield e[n]

print(list(col(l,2)))

for data in col(l,2):
    print(data)
Sign up to request clarification or add additional context in comments.

Comments

0

Your data and what you are trying to do with it sounds like a good fit for Pandas, a library for working with data in a tabular format.

>>> import pandas as pd
>>> data = [(0, b'C', 5.8816, 6.0, 0.1184, 4.2631, 4.2631, 0.0),
 (1, b'H', 0.8495, 1.0, 0.1505, 0.951, 0.951, 0.0),
 (2, b'Br', 35.0064, 35.0, -0.0064, 1.2192, 1.2192, -0.0),
 (3, b'Cl', 17.0401, 17.0, -0.0401, 1.2405, 1.2405, -0.0),
 (4, b'F', 9.2225, 9.0, -0.2225, 1.0449, 1.0449, -0.0)]
>>> df = pd.DataFrame(data)
>>> df
   0      1        2     3       4       5       6    7
0  0   b'C'   5.8816   6.0  0.1184  4.2631  4.2631  0.0
1  1   b'H'   0.8495   1.0  0.1505  0.9510  0.9510  0.0
2  2  b'Br'  35.0064  35.0 -0.0064  1.2192  1.2192 -0.0
3  3  b'Cl'  17.0401  17.0 -0.0401  1.2405  1.2405 -0.0
4  4   b'F'   9.2225   9.0 -0.2225  1.0449  1.0449 -0.0

Then, to get column 5, just do df[5].

>>> df[5]
0    4.2631
1    0.9510
2    1.2192
3    1.2405
4    1.0449
Name: 5, dtype: float64

By using Pandas, you can also easily read from (pd.read_csv()) and write the data (df.to_csv()) to disk, for example.

1 Comment

I had to make a slight modification to you solution.
0
data = [(0, b'C', 5.8816, 6.0, 0.1184, 4.2631, 4.2631, 0.0),
 (1, b'H', 0.8495, 1.0, 0.1505, 0.951, 0.951, 0.0),
 (2, b'Br', 35.0064, 35.0, -0.0064, 1.2192, 1.2192, -0.0),
 (3, b'Cl', 17.0401, 17.0, -0.0401, 1.2405, 1.2405, -0.0),
 (4, b'F', 9.2225, 9.0, -0.2225, 1.0449, 1.0449, -0.0)]

numbers = [n[2:] for n in data]

Result

[(5.8816, 6.0, 0.1184, 4.2631, 4.2631, 0.0), (0.8495, 1.0, 0.1505, 0.951, 0.951, 0.0), (35.0064, 35.0, -0.0064, 1.2192, 1.2192, -0.0), (17.0401, 17.0, -0.0401, 1.2405, 1.2405, -0.0), (9.2225, 9.0, -0.2225, 1.0449, 1.0449, -0.0)]

Comments

0

Assuming your array is meant to have commas at the end, like

data = [(0, b'C', 5.8816, 6.0, 0.1184, 4.2631, 4.2631, 0.0),
 (1, b'H', 0.8495, 1.0, 0.1505, 0.951, 0.951, 0.0),
 (2, b'Br', 35.0064, 35.0, -0.0064, 1.2192, 1.2192, -0.0),
 (3, b'Cl', 17.0401, 17.0, -0.0401, 1.2405, 1.2405, -0.0),
 (4, b'F', 9.2225, 9.0, -0.2225, 1.0449, 1.0449, -0.0)]

Which is actually a list of tuples (not really an array), you could do

QA = [row[5] for row in data]

or

from operator import itemgetter

QA = map(itemgetter(5), data)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.