2

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?

1
  • Show your code where you convert your 'array' to a list please Commented Mar 12, 2014 at 7:03

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

0

enumerate will return you tuple with index and value. You can try

for index, value in enumerate(list):
    print index
    print value

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.