我正在使用一个函数来构建一个字符串数组(恰好只有 0 和 1),该数组相当大。当我构建较小的字符串时,该函数可以工作,但不知何故,数据类型似乎将字符串的大小限制为 32 个字符长 (U32),而我没有要求。我错过了一些简单的事情吗?
当我构建字符串时,我首先将它们转换为列表,以便在再次将它们连接到字符串中之前更轻松地操作单个字符。我是否以某种方式限制了我通过我的方法使用“更大”数据类型的能力?在本例中,np.max(CM1) 的值约为 300(最近一次运行产生了 253),但字符串仅显示 32 个字符长...
''' Function to derive genome and count mutations in provided list of cells '''
def derive_genome_biopsy(biopsy_list, family_dict, CM1):
derived_genomes_inBx = np.zeros(len(biopsy_list)).astype(str)
for position, cell in np.ndenumerate(biopsy_list):
if cell == 0: continue
temp_parent = 2
bitstring = list('1')
bitstring += (np.max(CM1)-1)*'0'
if cell == 1:
derived_genomes_inBx[position] = ''.join(bitstring)
continue
else:
while temp_parent > 1:
temp_parent = family_dict[cell]
bitstring[cell-1] = '1'
if temp_parent == 1: break
cell = family_dict[cell]
derived_genomes_inBx[position] = ''.join(bitstring)
return derived_genomes_inBx
我收到的具体错误消息是:
Traceback (most recent call last):
File "biopsyCA.py", line 77, in <module>
if genome[site] == '1':
IndexError: string index out of range
family_dict 是一个字典,其中包含 parent 和 child 的列表,上述算法通过该列表从分支家谱中重建个体的“基因组”。它基本上将位串中的位置设置为“1”(如果您的 parent 拥有它,那么如果您的祖 parent 等)...直到您到达第一位(始终为“1”),然后应该完成。
最佳答案
32 个字符的限制来自于这一行中 float64 数组到字符串数组的转换:
derived_genomes_inBx = np.zeros(len(biopsy_list)).astype(str)
生成的数组包含数据类型 S32 值,该值将内容限制为 32 个字符。
要更改此限制,请使用“S300”或更大的值代替 str。
您还可以使用map(str, np.zeros(len(biopsy_list))
来获取更灵活的字符串列表,并使用numpy.array()<将其转换回numpy数组
填充后。
关于python - 在 python 3.5 中构建位字符串时出错 : the datatype is being set to U32 without my control,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34939606/