I am very new to python and I have a line of code
print docClassifier.predict(temp)
which basically prints an array of this format [0,0,1,1,1,0....]
. Now I want to store this array in a list for some other processing and the output of list should be same as array. I tried doing list.append() but when I print this list using for item in enumerate(list)
the output is not the same as of array.It is like this (0,array([0,0,1,1...])
. How can I correct this?
Asked
Active
Viewed 381 times
2

thefourtheye
- 233,700
- 52
- 457
- 497

user2916886
- 847
- 2
- 16
- 35
-
Show your code where you convert your 'array' to a list please – Tim Mar 12 '14 at 07:03
2 Answers
3
You can convert numpy arrays to lists, like this
my_list = np.array([0,0,1,1,1,0]).tolist()
You can check their types to confirm it like this
>>> type(np.array([0,0,1,1,1,0]))
<type 'numpy.ndarray'>
>>> type(np.array([0,0,1,1,1,0]).tolist())
<type 'list'>
If you want to enumerate
the list, you can do it like this
for index, current_number in enumerate(np.array([0,0,1,1,1,0]).tolist()):
print index, current_number
In fact, if you are just going to iterate, you don't even have to convert that to a list,
>>> for index, current_number in enumerate(np.array([0,0,1,1,1,0])):
... print index, current_number
...
...
0 0
1 0
2 1
3 1
4 1
5 0

thefourtheye
- 233,700
- 52
- 457
- 497
0
enumerate
will return you tuple
with index and value. You can try
for index, value in enumerate(list):
print index
print value

Nilesh
- 20,521
- 16
- 92
- 148