Python Forum
len(row) does not count rows correctly
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
len(row) does not count rows correctly
#1
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 "[]"
Reply
#2
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])
quest_ likes this post
Reply
#3
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
buran write Jan-23-2021, 06:55 AM:
Link removed
Reply
#4
(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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Row Count and coloumn count Yegor123 4 1,320 Oct-18-2022, 03:52 AM
Last Post: Yegor123
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 1,625 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  Pandas DataFrame combine rows by column value, where Date Rows are NULL rhat398 0 2,105 May-04-2021, 10:51 PM
Last Post: rhat398
  Indexing [::-1] to Reverse ALL 2D Array Rows, ALL 3D, 4D Array Columns & Rows Python Jeremy7 8 7,097 Mar-02-2021, 01:54 AM
Last Post: Jeremy7
  Comparing the count of rows from the tables that are present in two different databas krt5 6 3,993 Feb-15-2019, 03:20 PM
Last Post: krt5
  Count the rows to 10 and store in the list chris0147 0 2,272 Apr-08-2018, 06:52 PM
Last Post: chris0147

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020