Python 以不断变化的间隔插入列表
我不知道该怎么称呼这个问题,如果某个mod可以更改标题以更好地反映问题,请尝试。如果你不是mod,请随意评论姓名建议。谢谢:) 我正在尝试创建一个RAID 5模拟,使用Python列表作为HDD。我已经成功地模拟了RAID 4,其中所有奇偶校验都在一个磁盘上(请参阅)。现在我正试图将奇偶校验分布到所有磁盘上 RAID 4:奇偶校验在一个磁盘上,RAID 5:奇偶校验分布在一个磁盘上 我不知道如何正确地将奇偶校验插入列表 给定字节列表:Python 以不断变化的间隔插入列表,python,python-3.x,Python,Python 3.x,我不知道该怎么称呼这个问题,如果某个mod可以更改标题以更好地反映问题,请尝试。如果你不是mod,请随意评论姓名建议。谢谢:) 我正在尝试创建一个RAID 5模拟,使用Python列表作为HDD。我已经成功地模拟了RAID 4,其中所有奇偶校验都在一个磁盘上(请参阅)。现在我正试图将奇偶校验分布到所有磁盘上 RAID 4:奇偶校验在一个磁盘上,RAID 5:奇偶校验分布在一个磁盘上 我不知道如何正确地将奇偶校验插入列表 给定字节列表: b=[104、101、121、32、116、104、10
b=[104、101、121、32、116、104、101、114、101、32、66、111、98、98、121、33]
我需要它在两个硬盘之间平均分配(hdd[0]
-hdd[3]
),最后用0填充hdd[0]=[104,32,101,“p”,98,33]
hdd[1]=[101,116,“p”,32,98,0]
hdd[2]=[121,“p”,114,66,121,“p”]
hdd[3]=“p”,104,101,111,“p”,0]
我认为这样做的方法是在将列表拆分为HDD之前将“p”
插入列表
我不知道怎么做,因为在插入一个后,列表会改变,在插入第四个“p”
后,它会重置回第一个位置
我已尝试在运行时插入“p”
s,使用此(不工作)代码:
在本例中,hdd_num=4
(它是hdd的数量)
我的方法是将代码分成可管理的部分,这些部分可以单独测试和推理。这里有一个建议
def grabChunkOfBytes(byteArray, noChunks):
chunks = []
for byte in byteArray:
chunks.append(byte)
if len(chunks) == noChunks:
yield chunks
chunks = []
# If the total number of bytes is not divisible by number of disks, 0-fill
while len(chunks) < noChunks:
chunks.append(0)
yield chunks
def computeChecksum(chunks):
return 'p' # Your function
def writeChunkToHDDs(chunks, HDDs):
[hdd.append(part) for hdd, part in zip(HDDs, chunks)]
b = [104, 101, 121, 32, 116, 104, 101, 114, 101, 32, 66, 111, 98, 98, 121, 33, ]
hdds = [[], [], [], []]
totalHDDs = len(hdds)
for i, chunk in enumerate(grabChunkOfBytes(b, totalHDDs - 1)):
checksum = computeChecksum(chunk)
chunk.insert(i % totalHDDs, checksum)
writeChunkToHDDs(chunk, hdds)
from pprint import pprint
pprint(hdds)
def-grabbytes块(byteArray,noChunks):
块=[]
对于字节数组中的字节:
chunks.append(字节)
如果len(chunks)=noChunks:
产量块
块=[]
#如果总字节数不能被磁盘数整除,则0-fill
而len(chunks)
谢谢你让我走上正确的道路@Andrei。我最终得到了以下代码:
# make blank hdds (with their parity index)
hdds = [[i] for i in range(hdd_num)]
i = 0
# while there are still bytes to store
while len(input_bytes):
# pop the row from the list
row = input_bytes[:hdd_num - 1]
del input_bytes[:hdd_num - 1]
# add 0s if there aren't enough elements
while len(row) < hdd_num - 1:
row.append(0)
# add the XOR result in the right place
row.insert(i % hdd_num, xor(row))
# insert the values into the HDDs
for j, x in enumerate(row):
hdds[j].append(x)
i += 1
# make blank hdds (with their parity index)
hdds = [[i] for i in range(hdd_num)]
i = 0
# while there are still bytes to store
while len(input_bytes):
# pop the row from the list
row = input_bytes[:hdd_num - 1]
del input_bytes[:hdd_num - 1]
# add 0s if there aren't enough elements
while len(row) < hdd_num - 1:
row.append(0)
# add the XOR result in the right place
row.insert(i % hdd_num, xor(row))
# insert the values into the HDDs
for j, x in enumerate(row):
hdds[j].append(x)
i += 1
def xor(self, *to_xor):
"""Performs XOR on parameters, from left to right."""
# if passed a list as it's only argument, xor everything in it
if len(to_xor) == 1 and \
isinstance(to_xor[0], (list, tuple, types.GeneratorType)):
to_xor = to_xor[0]
x = 0
for i in to_xor:
x ^= i
return x