Python Forum

Full Version: len(row) does not count rows correctly
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I have this code script:
triplets = tuple(itertools.product((0, 1), repeat = 2)) 
freq = []
sums = {}
for row in newlist2:
    print("rows",len(row))
    for triple in triplets:
        sums[triple] = sum(np.all((row-np.array(triple))==0, axis=1))
        print("sum", sums[triple])
        freq.append(sums[triple]/(len(row)))
    print(sums)
In this code len(row) sometimes is correct but sometimes is not correct(it comes as len(row)-1 or len(row)+1)
Here is the result of code:
newlist2 [[[0, 0], [1, 1], [1, 1], [0, 0], [1, 1], [0, 0], [0, 0], (0.7853981633974483, 0.7853981633974483)], [[0, 0], [0, 0], [0, 0], (0, 0)], [[0, 0], [1, 1], [0, 0], [0, 0], [1, 1], [0, 0], [0, 0], (0.7853981633974483, 0)], [[0, 0], [0, 0], [1, 1], (0, 0.7853981633974483)]]
rows 8
sum 4
sum 0
sum 0
sum 3
{(0, 0): 4, (0, 1): 0, (1, 0): 0, (1, 1): 3} #For instance here 4+3 = 7 but it comes as 8
rows 4
sum 4
sum 0
sum 0
sum 0
{(0, 0): 4, (0, 1): 0, (1, 0): 0, (1, 1): 0} #Here is ok
rows 8
sum 5
sum 0
sum 0
sum 2
{(0, 0): 5, (0, 1): 0, (1, 0): 0, (1, 1): 2} #Here is 5+2=7 but it comes as 8
rows 4
sum 2
sum 0
sum 0
sum 1
{(0, 0): 2, (0, 1): 0, (1, 0): 0, (1, 1): 1} #Here 2+1=3 but it comes as 4 
freq:  [0.5, 0.0, 0.0, 0.375, 1.0, 0.0, 0.0, 0.0, 0.625, 0.0, 0.0, 0.25, 0.5, 0.0, 0.0, 0.25]
I did not understand why it is happening. Can you please help me?

Edit: I just noticed that:
My list is here:
newlist2 [[[0, 0], [1, 1], [1, 1], [0, 0], [1, 1], [0, 0], [0, 0], (0.7853981633974483, 0.7853981633974483)], [[0, 0], [0, 0], [0, 0], (0, 0)], [[0, 0], [1, 1], [0, 0], [0, 0], [1, 1], [0, 0], [0, 0], (0.7853981633974483, 0)], [[0, 0], [0, 0], [1, 1], (0, 0.7853981633974483)]]
When I am in the for loop in newlist2 I am also counting last element of sublist and if last element of sublist includes "(0,0)" then the number of (0,0) increases. How can I prevent it? I should not count last element of sublist of newlist2. I actually should not count inside "()", I should just count "[]"
import itertools
import numpy as np

newlist2 = [[[0, 0], [1, 1], [1, 1], [0, 0], [1, 1], [0, 0], [0, 0], (0.7853981633974483, 0.7853981633974483)],
            [[0, 0], [0, 0], [0, 0], (0, 0)],
            [[0, 0], [1, 1], [0, 0], [0, 0], [1, 1], [0, 0], [0, 0], (0.7853981633974483, 0)],
            [[0, 1], [1, 0], [1, 1], (0, 0.7853981633974483)]]

triplets = tuple(itertools.product((0, 1), repeat = 2))
freq = []
sums = {}
for row in newlist2:
    row = [x for x in row if isinstance(x, list)]
    count = len(row)
    for triple in triplets:
        sums[triple] = sum(np.all((row-np.array(triple))==0, axis=1))
        freq.append(sums[triple]/count)

for i in range(0, len(freq), 4):
    print(freq[i:i+4])
Refer to a python tutorial for any type of these queries cause they will cover these topics in a more better way, I mean the topics when you have a frequency of altered error. Smile
(Jan-23-2021, 01:49 AM)deanhystad Wrote: [ -> ]
import itertools
import numpy as np

newlist2 = [[[0, 0], [1, 1], [1, 1], [0, 0], [1, 1], [0, 0], [0, 0], (0.7853981633974483, 0.7853981633974483)],
            [[0, 0], [0, 0], [0, 0], (0, 0)],
            [[0, 0], [1, 1], [0, 0], [0, 0], [1, 1], [0, 0], [0, 0], (0.7853981633974483, 0)],
            [[0, 1], [1, 0], [1, 1], (0, 0.7853981633974483)]]

triplets = tuple(itertools.product((0, 1), repeat = 2))
freq = []
sums = {}
for row in newlist2:
    row = [x for x in row if isinstance(x, list)]
    count = len(row)
    for triple in triplets:
        sums[triple] = sum(np.all((row-np.array(triple))==0, axis=1))
        freq.append(sums[triple]/count)

for i in range(0, len(freq), 4):
    print(freq[i:i+4])
Thank you very much! Smile
And sometimes list does not have one of the combinations like that:
newlist2 = [[[0, 0], [1, 1], [1, 1], [0, 0], [1, 1], [0, 0], [0, 0], (0.7853981633974483, 0.7853981633974483)],
            [[0, 0], [1, 1], [0, 0], [0, 0], [1, 1], [0, 0], [0, 0], (0.7853981633974483, 0)],
            [[0, 1], [1, 0], [1, 1], (0, 0.7853981633974483)]]
For instance (0,0) is not exist and in this situation I have to add zero to frequency table for [0,0] [1, 1], [1, 0],[0, 1]. How can I do that?