biogeek - 6 months ago 22

Python Question

The following is my sample that consists of binary values 0 and 1

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

For any number of elements in the list and any number of elements in the list of lists I need to do the following:

A. Convert it into a list of lists such that the corresponding elements of each list are strung together in a list of lists

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

B. Create a counter(nn) to count length of each element and divide by (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. Need to calculate a parameter for pi. Here is a link showing how this can be done for a list https://eval.in/672980.

I have tried to write the code for the same. I hit the following errors:

`A. l += seq_list[j][i]`

IndexError: list index out of range.

I am certain that i,j and k are all in the correct range.

`B. counters = [Counter(sub_list) for sub_list in column]`

^

SyntaxError: invalid syntax

Why is it invalid syntax?

Any ideas on how to correct the errors? I tried different ways to do the same, but I am unable to do so.

`#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)]

Answer

I think this should work for you:

```
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)]
```