Python 不推荐使用numpy fromstring,请改用frombuffer
在使用numpy 1.18.1的Python代码中 `def打印板(自): 当前=self.player 其他=自玩家%2+1Python 不推荐使用numpy fromstring,请改用frombuffer,python,string,numpy,typeerror,deprecation-warning,Python,String,Numpy,Typeerror,Deprecation Warning,在使用numpy 1.18.1的Python代码中 `def打印板(自): 当前=self.player 其他=自玩家%2+1 currBin = '{:049b}'.format(self.current_position) currRev = currBin[::-1] cArr = (np.fromstring(currRev,'u1') - ord('0'))*current other_position = self.current_positi
currBin = '{:049b}'.format(self.current_position)
currRev = currBin[::-1]
cArr = (np.fromstring(currRev,'u1') - ord('0'))*current
other_position = self.current_position^self.mask
othBin = '{:049b}'.format(other_position)
othRev = othBin[::-1]
oArr = (np.fromstring(othRev,'u1') - ord('0'))*other
tArr = oArr+cArr
brd = np.reshape(tArr,(7,7),order = 'F')
for y in range(bitBoard.HEIGHT,-1,-1):
for x in range(bitBoard.WIDTH):
print(brd[y,x],end = ' ')
print()
print()
`
该行:
cArr = (np.fromstring(currRev,'u1') - ord('0'))*current
给出以下警告:
DeprecationWarning: The binary mode of fromstring is deprecated, as it behaves surprisingly on unicode inputs. Use frombuffer instead
cArr = (np.fromstring(currRev,'u1') - ord('0'))*current
将“fromstring”替换为“frombuffer”会出现以下错误:
cArr = (np.frombuffer(currRev,'u1') - ord('0'))*current
TypeError: a bytes-like object is required, not 'str'
尽管在谷歌上搜索了一下,我还是找不到我应该用的东西。有人能帮忙吗
多谢各位
Alan代码的相关部分是产生
currev
的部分。由此,我可以构建这个示例:
In [751]: astr = '{:049b}'.format(123)[::-1]
In [752]: astr
Out[752]: '1101111000000000000000000000000000000000000000000'
你的警告:
In [753]: np.fromstring(astr, 'u1')
/usr/local/bin/ipython3:1: DeprecationWarning: The binary mode of fromstring is deprecated, as it behaves surprisingly on unicode inputs. Use frombuffer instead
#!/usr/bin/python3
Out[753]:
array([49, 49, 48, 49, 49, 49, 49, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48,
48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48,
48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48],
dtype=uint8)
frombuffer
需要一个bytestring,所以让我们创建一个:
In [754]: astr.encode()
Out[754]: b'1101111000000000000000000000000000000000000000000'
In [755]: np.frombuffer(astr.encode(),'u1')
Out[755]:
array([49, 49, 48, 49, 49, 49, 49, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48,
48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48,
48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48],
dtype=uint8)
其余部分:
In [756]: _-ord('0')
Out[756]:
array([1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0], dtype=uint8)
获取相同数组的另一种方法:
In [758]: np.array(list(astr),'uint8')
Out[758]:
array([1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0], dtype=uint8)
代码的相关部分是生成
currRev
的部分。由此,我可以构建这个示例:
In [751]: astr = '{:049b}'.format(123)[::-1]
In [752]: astr
Out[752]: '1101111000000000000000000000000000000000000000000'
你的警告:
In [753]: np.fromstring(astr, 'u1')
/usr/local/bin/ipython3:1: DeprecationWarning: The binary mode of fromstring is deprecated, as it behaves surprisingly on unicode inputs. Use frombuffer instead
#!/usr/bin/python3
Out[753]:
array([49, 49, 48, 49, 49, 49, 49, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48,
48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48,
48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48],
dtype=uint8)
frombuffer
需要一个bytestring,所以让我们创建一个:
In [754]: astr.encode()
Out[754]: b'1101111000000000000000000000000000000000000000000'
In [755]: np.frombuffer(astr.encode(),'u1')
Out[755]:
array([49, 49, 48, 49, 49, 49, 49, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48,
48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48,
48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48],
dtype=uint8)
其余部分:
In [756]: _-ord('0')
Out[756]:
array([1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0], dtype=uint8)
获取相同数组的另一种方法:
In [758]: np.array(list(astr),'uint8')
Out[758]:
array([1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0], dtype=uint8)
从概念上解释这些行的作用。你的例子并不简单,你发布的90%的代码与问题无关。我知道我给出的太多了,但当我问问题时,我经常会被要求提供整个代码!这一次,我给出了这个函数,希望这样就足够了。这些行采用二进制编码的connect4列中的一列,并根据播放机将其转换为0和1或0和2。它将生成的数组相加,并输出该列中各部分的位置,解释这些行在概念上的作用。你的例子并不简单,你发布的90%的代码与问题无关。我知道我给出的太多了,但当我问问题时,我经常会被要求提供整个代码!这一次,我给出了这个函数,希望这样就足够了。这些行采用二进制编码的connect4列中的一列,并根据播放机将其转换为0和1或0和2。它将生成的数组相加,并输出该列中各个片段的位置。非常感谢。成功了。我不经常玩位,所以我不确定该怎么做。在py2中,默认字符串是字节大小。在py3中,默认为unicode,ByTestRing用
b'…'
标记,感谢您的解释非常感谢。成功了。我不经常玩位,所以我不确定该怎么做。在py2中,默认字符串是字节大小。在py3中,默认使用unicode,ByTestRing用b'…'
标记,感谢您的解释