2016-11-20 200 views
1

我试图通过重新创建2048来教会自己一些基本的编码,但是我碰到了一堵墙。Python 3.5.2 - list.sort()不起作用

我想要做的是:

1)创建表示在电路板上的空间的基体,通过零值的空的空间

2占据)根据该矩阵的转置的轴于轴线玩家想要使用np.tolist()

4)使用listID.sort(键=布尔)进行排序,从各行中移动

3件)创建列表在矩阵按照它的真值把它列出来,把零置于一端没有重排具有的值的项目,并通过在处于值

6等于列表修改所述列表就地

5)合并相邻的项目节省空间列表)使用np.columnstack要结合分类列表成为一个矩阵,ovewriting以前的矩阵

7)转轴到他们最初

那里我得到绊倒在步骤4)。

该列表根本没有得到排序,没有任何变化。

例如,a0(第一行的列表)将为[0,2,0,0],但排序后,它仍然是[0,2,0,0]。

这里是我的可怕,nooby,WIP代码来看看,看看自己,如果你愿意的话:

import numpy as np 

a = np.matrix([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]) 

print (a) 

b = np.random.randint(low=0, high=3) 

c = b 
while c == b: 
     c = np.random.randint(low=0, high=3) 

d = np.random.randint(low=0, high=3) 

e = d 
while e == d: 
     e = np.random.randint(low=0, high=3) 

a.itemset((b, c), 2) 
a.itemset((d, e), 2) 

print (a) 

score = np.sum(a) 

print (score) 

#while 0 in a: 
     #direction = input("U, D, L, R?") 

     #if direction == "U": 
     # mode = 1 

     #if direction == "D": 
     # mode = 2 

     #if direction == "L": 
     # mode = 3 

     #if direction == "R": 
     # mode = 4 

#transpose axes according to mode 



#sort each array in matrix by boolean value 
#refuses to sort? 

a0 = a[0].tolist() 
a0.sort(key=bool) 

a1 = a[1].tolist() 
a1.sort(key=bool) 

a2 = a[2].tolist() 
a2.sort(key=bool) 

a3 = a[3].tolist() 
a3.sort(key=bool) 

#reverse each list depending on mode 


#combine back into matrix 

print (a0, a1, a2, a3) 

a = np.column_stack([[a0], [a1], [a2], [a3]]) 


print (a) 

我已经搜查,搜查,但没有我见过提供了一些深入了解我做错了,我认为我是。

编辑:求助!

import numpy as np 
import numpy.matlib 
import itertools 

a = np.matlib.zeros((4,4)) 

print (a) 

b = np.random.randint(low=0, high=3) 

c = b 
while c == b: 
     c = np.random.randint(low=0, high=3) 

d = np.random.randint(low=0, high=3) 

e = d 
while e == d: 
     e = np.random.randint(low=0, high=3) 

a.itemset((b, c), float(2)) 
a.itemset((d, e), float(2)) 

print (a) 

score = np.sum(a) 

print (score) 

#while 1 in a: 
     #direction = input("U, D, L, R?") 

     #if direction == "U": 
     # mode = 1 

     #if direction == "D": 
     # mode = 2 

     #if direction == "L": 
     # mode = 3 

     #if direction == "R": 
     # mode = 4 

#transpose axes according to mode 



#sort each array in matrix by boolean value 
#refuses to sort? 

a0 = a[0].tolist() 
a01 = [] 
a0_2 = list(itertools.chain.from_iterable(a0)) 
for item in a0_2: 
     a01.append(float(item)) 
a01.sort(key=bool) 

a1 = a[1].tolist() 
a11 = [] 
a1_2 = list(itertools.chain.from_iterable(a1)) 
for item in a1_2: 
     a11.append(float(item)) 
a11.sort(key=bool) 

a2 = a[2].tolist() 
a21 = [] 
a2_2 = list(itertools.chain.from_iterable(a2)) 
for item in a2_2: 
     a21.append(float(item)) 
a21.sort(key=bool) 

a3 = a[3].tolist() 
a31 = [] 
a3_2 = list(itertools.chain.from_iterable(a3)) 
for item in a3_2: 
     a31.append(float(item)) 
a31.sort(key=bool) 


#reverse each list depending on mode 


#combine back into matrix 

print (a01, a11, a21, a31) 

a = np.vstack([[a01], [a11], [a21], [a31]]) 


print (a) 
+1

摆脱那些无关紧要的东西(比如失败后的所有东西)以及所有冗余(同一破损代码的四个副本)。 –

+0

打印命令是这样我可以看到我对代码所做的更改是否允许它实际对列表进行排序。每个(数字)行是从矩阵的四行中的一行创建的单独列表。 –

+0

我认为复制数组的约定是c [:] = b而不是c = b,因为它只是创建了引用。 –

回答

1

你说你试图“按真值排序”,而NumPy没有这个选项。好吧,它确实有点!

a0 = a0[(~a0.astype(bool)).argsort(kind='mergesort')] 

这需要A0,并将其转换为True无论它是非零反,然后是(使假非零),然后排序,(使用归并为稳定排序)。使用argsort()可以让我们进行“间接”排序,即按照另一个排序。

通过将数组转换为列表然后再转回来,通过对比,效率相当低。但是,如果数据很小,您就不会注意到这一点,因为它们属于您的情况。