由于某种原因,
evalRow(list(array([0, 1, 0, 0, 0])))
和evalRow([0, 1, 0, 0, 0])
的结果不同。然而,如果我使用magicConvert
(在这里用于调试)代替list
将numpy数组转换为列表,则结果与预期相同:def magicConvert(a):
ss = str(list(a))[1:-1]
return map(int, ss.split(","))
# You don't actually need to read these functions, just here to reproduce the error:
from itertools import *
def evalRow(r):
grouped = map(
lambda (v, l): (v, len(tuple(l))),
groupby(chain([2], r, [2])))
result = 0
for player in (1, -1):
for (pre, mid, post) in allTuples(grouped, 3):
if mid[0] == player:
result += player * streakScore(mid[1], (pre[0] == 0) + (post[0] == 0))
return result
def streakScore(size, blanks):
return 0 if blanks == 0 else (
100 ** (size - 1) * (1 if blanks == 1 else 10))
def allTuples(l, size):
return map(lambda i: l[i : i + size], xrange(len(l) - size + 1))
0
... - Saullo G. P. Castrotolist()
在numpy
数组和列表之间进行转换。使用list(some_array)
必须遍历 numpy 数组,然后相应地创建列表,而tolist()
可能可以避免一些通用转换的开销。 - Bakuriulist(array([0, 1, 2]))
ж—¶пјҢдјҡеҫ—еҲ°дёҖдёӘеҲ—иЎЁ[0, 1, 2]
пјҢе…¶дёӯе…ғзҙ жҳҜnumpy
зҡ„int64
иҖҢдёҚжҳҜ Python зҡ„int
гҖӮдҪҝз”Ёtolist()
ж–№жі•еҸҜд»Ҙиҝ”еӣһ Python зҡ„int
гҖӮиҝҷеҸҜиғҪдјҡеҪұе“ҚжӮЁжү§иЎҢзҡ„ж“ҚдҪңе’Ңз»“жһңгҖӮ - Bakuriu