gpt4 book ai didi

python - 如何处理 Pytjon 列表列表中具有二进制值的样本的索引超出范围和无效语法?

转载 作者:太空宇宙 更新时间:2023-11-03 15:59:39 24 4
gpt4 key购买 nike

以下是我的示例,由二进制值 0 和 1 组成

 sample = [['0100','0101','0101''],['011','100','001','001'],['001','001','001']]

对于列表中的任意数量的元素以及列表列表中的任意数量的元素,我需要执行以下操作:

A.将其转换为列表的列表,使得每个列表对应的元素都串在列表的列表中

column = [['000','111','000','0111'],['0100','1000','1011'],['000','000','111']

B.创建一个计数器(nn)来计算每个元素的长度并除以(nn-1)

nn = [[3,3,3,3],[4,4,4],[3,3,3]]
nn - 1 = [[2,2,2,2],[3,3,3,3],[2,2,2]]
d = nn-1
div = nn/d

C.需要计算 pi 的参数。以下链接显示了如何对列表 https://eval.in/672980 执行此操作。

我尝试编写相同的代码。我遇到了以下错误:

 A. l += seq_list[j][i]
IndexError: list index out of range.

我确信 i、j 和 k 都在正确的范围内。

B. counters = [Counter(sub_list) for sub_list in column]
^
SyntaxError: invalid syntax

为什么语法无效?

关于如何纠正错误有什么想法吗?我尝试了不同的方法来做到这一点,但我无法做到这一点。

#Tranposing. Moving along the columns only
column = []
for k in range(len(seq_list)):
for i in range(len(seq_list[k][0])): #Length of the row
l = ""
for j in range(len(seq_list[k])): #Length of the column
l += seq_list[j][i]
column.append(l)
print "\n Making the columns as a list: " + str(column)


#Creating a separate list where -/? will not be part of the sequence
tt = ["".join(y for y in x if y in {'0','1'}) for x in column]

#Creating a counter that stores n/n-1 values
counters = [Counter(sub_list) for sub_list in tt]
nn =[]
d = []
for counter in counters:
binary_count = sum((val for key, val in counter.items() if key in "01"))
nn.append(binary_count)
d = [i - 1 for i in nn]
div = [int(b) / int(m) for b,m in zip(nn, d)]

最佳答案

我认为这应该适合你:

seq_list = [['0100','0101','0101'],['011','100','001','001'],['001','001','001']]

results = []
for k in range(len(seq_list)):
column_list = [[] for i in range(len(seq_list[k][0]))]
for seq in seq_list[k]:
for i, nuc in enumerate(seq):
column_list[i].append(nuc)
tt = ["".join(y for y in x if y in {'0','1'}) for x in column_list]
results.append(tt)


### Creating a counter that stores n/n-1 values
BINARY = {'0','1'}
counts = [[sum(c in BASES for c in s) for s in pair] for pair in results]
countsminusone1 = [[(sum(c in BINARY for c in s)-1) for s in pair] for pair in results]
countsminusone = [[1 if x <= 0 else x for x in pair] for pair in countsminusone1]
bananasplit = [[n/d for n, d in zip(subq, subr)] for subq, subr in zip(counts, countsminusone)]

关于python - 如何处理 Pytjon 列表列表中具有二进制值的样本的索引超出范围和无效语法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40455883/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com