简体   繁体   中英

compare total of each row with the rest of rows

what is the best way to compare the value of each row below to the rest. suppose i want to find that 2 or more rows have the same total (other than if statements).

each index below is originated from a random function.

import random main_list = [random.randint(1,9) for iter in range(25)]

row_1_total = main_list[0] + main_list[1] + main_list[2] + main_list[3] + main_list[4]
row_2_total = main_list[5] + main_list[6] + main_list[7] + main_list[8] + main_list[9]
row_3_total = main_list[10] + main_list[11] + main_list[12] + main_list[13] + main_list[14]
row_4_total = main_list[15] + main_list[16] + main_list[17] + main_list[18] + main_list[19]
row_5_total = main_list[20] + main_list[21] + main_list[22] + main_list[23] + main_list[24]

print("Total of row 1 >>>", row_1_total)
print("Total of row 2 >>>", row_2_total)
print("Total of row 3 >>>", row_3_total)
print("Total of row 4 >>>", row_4_total)
print("Total of row 5 >>>", row_5_total)

Add the rows to data frame and use df.diff().eq(0) or any comparison

import pandas as pd
row_1_total=3
row_2_total=6
row_3_total=7
row_4_total=5
row_5_total=5
df= pd.DataFrame([row_1_total,row_2_total,row_3_total ,row_4_total ,row_5_total  ])
df.diff().eq(0)
print(df.diff().eq(0))
duplicat = df[df.duplicated()]
print(duplicat)

Instead of 5 variables keep the sums in a list. If you loos loop you can take slice of the list in each iteration and reduce your code to

main_list = [random.randint(1, 9) for _ in range(25)]
sums = [sum(main_list[i:i+4]) for i in range(0, len(main_list), 5)]
for i in range(len(sums)):
    print(f"Total of row {i + 1} >>>", sums[i])

Now you can put sums in set() and compare the size

sums_set = set(sums)
print(len(sums_set) == len(sums))

First, I would suggest collecting the totals in a slightly less unwieldy data structure, like a list:

row_total = []
row_total.append(main_list[0] + main_list[1] + main_list[2] + main_list[3] + main_list[4])
row_total.append(main_list[5] + main_list[6] + main_list[7] + main_list[8] + main_list[9])
row_total.append(main_list[10] + main_list[11] + main_list[12] + main_list[13] + main_list[14])
row_total.append(main_list[15] + main_list[16] + main_list[17] + main_list[18] + main_list[19])
row_total.append(main_list[20] + main_list[21] + main_list[22] + main_list[23] + main_list[24])
print(row_total[3])

Secondly, you could avoid a lot of repetition by computing the totals in one line like this (assuming the main_list stops at 24):

row_total = [sum(x[0+n:3+n]) for n in range(0, len(x), 3)]]

The answer to your question would then be something like:

from collections import Counter
from random import randint

main_list = [randint(0, 10) for _ in range(25)]

row_total = [sum(main_list[0+n:3+n]) for n in range(0, len(main_list), 3)]

duplicate_totals = {t: c for t, c in Counter(row_total).items() if c > 1}

print(main_list)
# a dictionary of totals that show up twice or more often
print(duplicate_totals)

The result would look something like:

[4, 9, 1, 2, 0, 6, 8, 6, 4, 3, 0, 10, 0, 9, 1, 0, 10, 7, 10, 7, 0, 6, 2, 1, 7]
{17: 2}

I think you are looking for something like this:

main_list = [[5,4,6], [1, 2, 3, 4], [4,5,6]]
total = []
for row in main_list:
    total.append(sum(row))


for t in total:
    print("value : ",t, " is at indices : ", total.indices(t))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM