【问题标题】:Python 3.5.2 - list.sort() not functioningPython 3.5.2 - list.sort() 不起作用
【发布时间】:2017-04-03 16:43:24
【问题描述】:

我正试图通过重新创建 2048 来自学一些基本的编码,但我碰壁了。

我想做的是:

1) 创建一个矩阵,表示棋盘上的空格,空格由零值占据

2) 根据玩家想要移动棋子的轴来转置矩阵的轴

3) 使用 np.tolist() 从矩阵中的每一行创建一个列表

4) 使用 listID.sort(key=bool) 按其真值对列表进行排序,将零放在列表的一端,而不重新排列具有值的项目,并通过修改列表来节省空间-地点

5) 合并列表中值相等的相邻项

6) 使用 np.columnstack 将排序后的列表组合成一个矩阵,覆盖之前的矩阵

7) 将轴转置到原来的位置

我被绊倒的地方是第 4 步)。

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

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

如果您愿意,这是我可怕的、笨拙的 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)

【问题讨论】:

  • 去掉无关紧要的东西(比如失败部分之后的所有东西)和所有冗余(相同损坏代码的四个副本)。
  • 打印命令让我可以看到我对代码所做的更改是否允许它实际对列表进行排序。 a(number) 行中的每一行都是从矩阵的四行之一创建的单独列表。
  • 我认为复制数组的惯例是 c[:] = b 而不是 c = b 因为这只是创建引用。
  • c = b 等与数组无关,这是为游戏生成坐标以将前两个图块插入矩阵的代码。那部分是功能性的。这里唯一不起作用的部分是对列表进行排序(a0 到 a3)
  • 啊,是的,我的错,我因为某种原因感到困惑。

标签: list python-3.x sorting numpy matrix


【解决方案1】:

您说您正在尝试“按真值排序”,而 NumPy 对此没有选择。嗯,确实如此!

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

这需要 a0 并将其转换为 True,只要它是非零的,然后将其反转(因此 False 表示非零),然后对其进行排序(使用 mergesort 进行稳定排序)。 argsort() 的使用让我们可以进行“间接”排序,即按另一件事排序。

相比之下,您通过将数组转换为列表然后再返回的方式非常低效。但是,如果数据很小,您就不会注意到这一点,就像您的情况一样。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-13
    • 2018-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-01
    • 2018-08-10
    • 1970-01-01
    相关资源
    最近更新 更多