2016-08-04 23 views
0

Ich möchte meinen Listenwert als Liste machen.Python-Listenwert eine Liste machen

Beispiel:

abc = [1,2,3,4] 

Ergebnis:

abc = [[1], [2], [3], [4]] 

Ich fand, dass numpy Bibliothek benötigt wird. aber ich weiß nicht, wie ich das ändern soll. Wer kennt die Lösung?

+3

'[[v] für v in abc]'? – idjaw

Antwort

1

Mit numpy, können Sie eine neue Achse hinzufügen:

import numpy as np 
np.array(abc)[:, np.newaxis] 
Out: 
array([[1], 
     [2], 
     [3], 
     [4]]) 
+0

Danke. Ich fand, dass die Antwort am besten ist. – spritecodej

8

Ich glaube nicht, dass Sie brauchen, numpy. Ein Listenverständnis sollte genügen.

abc = [[x] for x in abc] 
2

Try This: -

abc = map(lambda x:[x], abc) 
1

Eine nicht list comprehension oder lambda function Version

abc = [1,2,3] 
arr = [] 
for x in abc: 
    arr.append([x]) 

print arr 

Ich glaube, das ist mehr intuitiv für Anfänger, aber weniger pythonic sein würde.

0
import numpy as np 
arr = np.array([[x] for x in abc])