简体   繁体   中英

Find sum and average from list?

1) Sum of all the 3rd element from those row which start from 1 and contain letter M , same for 507 and so on.
2) Find min and max number from all the 3rd element from those rows which start from 1 and contain Letter W , same for 507 and so on.From this list

rows = [['507', 'W', '1000', '1'],
['1', 'M', '6', '2'],
['1', 'W', '1400', '3'],
['1', 'M', '8', '8'],
['1', 'T', '101', '10'],
['507', 'M', '4', '12'],
['1', 'W', '1700', '15'],
['1', 'M', '7', '16'],
['507', 'M', '8', '20']]

Expected output: The Format is (id, min number,max number,sum of number) for id

1 1400 1700 27 507 1000 1000 12

my code

    import itertools


lst = []
fileToProcess = open("/home/salman/Desktop/input.txt", "r")
for line in fileToProcess:
    lst.append(line.strip().split(' '))
lst = sorted(lst, key=lambda x: x[0])
groups = itertools.groupby(lst, key=lambda x: x[0])
groups = [[*group] for _, group in groups]

# 3rd element
grp_3rd = [[entry[2] for entry in group] for group in groups]


grp_3rd = [sum(float(entry[2]) for entry in group) for group in groups]


grp_3rd = {group[0][0]: sum(float(entry[2]) for entry in group) for group in groups}
print(grp_3rd)
flatten = lambda list_: [sublist for l in list_ for sublist in l]
groups = [flatten(group) for group in groups]

print(groups)

Your code does not make sense but based on your initial problem statement, try this:

rows = [['507', 'W', '1000', '1'],
['1', 'M', '6', '2'],
['1', 'W', '1400', '3'],
['1', 'M', '8', '8'],
['1', 'T', '101', '10'],
['507', 'M', '4', '12'],
['1', 'W', '1700', '15'],
['1', 'M', '7', '16'],
['507', 'M', '8', '20']]
dd = {}
for row in rows:
    key = "{}_{}".format(row[0],row[1])
    if key in dd:
        dd[key]["col_3"].append(int(row[2]))
        dd[key]["col_4"].append(int(row[3]))
    else:
        dd.update({key: {"col_3":[int(row[2])],"col_4":[int(row[3])]}})

for key, value in dd.items():
    print("id: {}, min number: {},max number: {},sum of numbers: {}".format(key,min(value['col_3']),max(value['col_3']),sum(value['col_4'])))

output

id: 507_W, min number: 1000,max number: 1000,sum of numbers: 1
id: 1_M, min number: 6,max number: 8,sum of numbers: 26
id: 1_W, min number: 1400,max number: 1700,sum of numbers: 18
id: 1_T, min number: 101,max number: 101,sum of numbers: 10
id: 507_M, min number: 4,max number: 8,sum of numbers: 32

i just found 21 instead of 27 check if it's correct

rows = [['507', 'W', '1000', '1'],
['1', 'M', '6', '2'],
['1', 'W', '1400', '3'],
['1', 'M', '8', '8'],
['1', 'T', '101', '10'],
['507', 'M', '4', '12'],
['1', 'W', '1700', '15'],
['1', 'M', '7', '16'],
['507', 'M', '8', '20']]

sum_1_M=0
sum_1_W=0
sum_507_M=0
sum_507_W=0
list_1=[]
list_507=[]
for x in rows:
    if x[0]== '1' and x[1]=='M' :
        sum_1_M += int(x[2])
    elif  x[0]== '1' and x[1]=='W':
           sum_1_W += int(x[2])
           list_1.append(int(x[2]))
    elif  x[0]== '507' and x[1]=='M':
        sum_507_M += int(x[2])
    elif  x[0]== '507' and x[1]=='W':
           sum_507_W += int(x[2])
           list_507.append(int(x[2]))

list_1.sort()
list_507.sort()
print('{} {} {} {}'.format('1',list_1[0],list_1[len(list_1)-1],sum_1_M))
print('{} {} {} {}'.format('507',list_507[0],list_507[len(list_507)-1],sum_507_M))

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