Get a running total from a list

前端 未结 4 1627
青春惊慌失措
青春惊慌失措 2021-01-28 02:51

I\'m reading in items:

for line in sys.stdin:
    line = line.strip()
    data = line.split(\"-\")

If I print data as it is read,

相关标签:
4条回答
  • 2021-01-28 02:56

    Use a collections.Counter() to count the occurrences:

    import collections
    
    lines = [['Adam', '5'],
             ['Peter', '7'],
             ['Adam', '8'],
             ['Lucy', '2'],
             ['Peter', '4']]
    
    counter = collections.Counter()
    for data in lines:
        counter[data[0]] += int(data[1])
    
    print(counter)
    

    You'll get:

    Counter({'Adam': 13, 'Peter': 11, 'Lucy': 2})
    
    0 讨论(0)
  • 2021-01-28 02:58

    Initialize a defaultdict with type int and use the name as the key

    from collections import defaultdict
    name_list = defaultdict(int)
    for line in sys.stdin:
        line = line.strip()
        data = line.split("-")
        name = data[0]
        value = int(data[1])
        name_list[name] += value
    for key, value in name_list.items(): print key, value
    
    0 讨论(0)
  • 2021-01-28 02:58

    I recommend creating a dictonary and updating that as you go. I have assumed your data format for data is a list of lists.

    finalList = {}
    
    for name, value in data:
        if name in finalList.keys():
            finalList[name] = finalList[name] + int(value)
        else:
            finalList[name] = int(value)
    
    print(finalList)
    
    0 讨论(0)
  • 2021-01-28 03:13

    Pandas does a very good job in handling this kind of situations

    import pandas as pd
    df_data=pd.read_csv(filepath_or_buffer=path,sep='_',names =['Name','value'])
    df=df_data.groupby(['Name'])['value'].sum()
    
    print df
    

    output

    'Adam'     13
    'Lucy'      2
    'Peter'    11
    

    Input file

    Adam_5
    Peter_7
    Adam_8
    Lucy_2
    Peter_4
    
    0 讨论(0)
提交回复
热议问题