假设我想将一些数据拆分为 60 个字符的部分并将它们存储在哈希中。我有以下解决方案,但对我来说似乎有点脏:(因为迭代和不断重新分配)
i = 0
while signature != '':
header_hash['Some-Authorization-' + i] = signature[:60]
signature = signature[60:]
i += 1
你能想出更好的方法来处理这个问题吗?
最佳答案
虽然与 how to evenly split a list into chunks 非常相似我相信这仍然是一个有效的问题,但将包含上一个问题的部分答案:
def hashing(header_hash, signature, hash_size):
for index, i in enumerate(xrange(len(signature), hash_size)):
header_hash['Some-Authorization-%s' % index] = signature[i:i+hash_size]
这就是我的答案,查看 itertools
函数,我怀疑在分组后重新加入它们会抵消使用 itertools 的任何好处。
关于python - 优雅的数据分块方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8409782/