0

I have two text files, in each file there are 10 values. Now I want to include these 10 values as two lists in one list and access index of the lists of list. But the problem is I get an error saying, "list indices must be integers, not tuple" . Any suggestion would be greatly appreciated.

in the first file I have 0.001 0.017 0.07 0.09 0.05 0.02 0.014 0.014 0.021 0.033

In the second file I have

0.001 0.01 0.0788 0.09 0.0599 0.0222 0.014 0.01422 0.0222 0.033

import numpy as np

d=[]
one = np.loadtxt('one.txt')
two=np.loadtxt('two.txt')

d.append(one)
d.append(two)


#I get this error "list indices must be integers, not tuple ", when 
# I try to access the index of my lists inside the list

print (d[0,:])
hima Fernando
  • 137
  • 2
  • 8
  • 1
    Possible duplicate of [What does a colon and comma stand in a python list?](https://stackoverflow.com/questions/21165751/what-does-a-colon-and-comma-stand-in-a-python-list) – mkrieger1 Nov 27 '17 at 01:06
  • 1
    try `d = np.array([one, two])` lists do not allow multidimensional indexing, but numpy arrays do. – Paul Panzer Nov 27 '17 at 01:08

2 Answers2

3

You start out with numpy, so stick with numpy! You don't want one and two in a list, you want them as the rows of a 2d numpy array:

d = np.array([one, two])

d
# array([[ 0.001  ,  0.017  ,  0.07   ,  0.09   ,  0.05   ,  0.02   ,
#          0.014  ,  0.014  ,  0.021  ,  0.033  ],
#        [ 0.001  ,  0.01   ,  0.0788 ,  0.09   ,  0.0599 ,  0.0222 ,
#          0.014  ,  0.01422,  0.0222 ,  0.033  ]])
type(d)
# <class 'numpy.ndarray'>
d.shape
# (2, 10)
d[0, :]
# array([ 0.001,  0.017,  0.07 ,  0.09 ,  0.05 ,  0.02 ,  0.014,  0.014,
#         0.021,  0.033])
d[:, 4]
# array([ 0.05  ,  0.0599])

etc.

Paul Panzer
  • 51,835
  • 3
  • 54
  • 99
0

d[0,:] is not a thing. Not sure what you're trying to do here. Try d[0].

Some additional info:

d is a 2-dimensional list. (It is a list of lists.) Here's an example to show how to access certain parts:

>>> d = [[1, 2, 3], [4, 5, 6]]
>>> d[0]
[1, 2, 3]
>>> d[1]
[4, 5, 6]
>>> d[0][2]
3
>>> d[0][1:]
[2, 3]
>>> d[1][:2]
[4, 5]
>>> e = [d[0][1], d[1][1]]
>>> e
[2, 5]

Make sense?

AAM111
  • 1,178
  • 3
  • 19
  • 39