Hi All,
This is my first cry for help
I'm a Python Noob and I have been progressing fairly well (I started learning Python about a month ago). I have come to the point in my code that I simply cannot figure out. I am trying to take a simple .csv file and tally duplicates and combine values. I have tried dictionaries, tuples, sets, etc etc but I am simply missing something.
My production .csv is about 150k lines long and is a log for commodities market state change messages... so I created a basic example of what I am dealing with. I have pretty good luck with regex and could probably solve this problem via regex.. but I know that is slow.
Here is my example .csv... lets say I'm a car dealer that owns several car lots.. so I have duplicates in the list.
ford, 3
ford, 3
honda, 1
dodge, 8
dodge, 2
dodge, 5
ford, 6
dodge, 2
chevy, 1
dodge, 5
My goal is to have a combined list that would look like this... the order in the result does not matter.. I just need totals
chevy, 1
dodge, 22
ford, 9
honda, 1
So far I have coded the following that parses the .csv and creates a list of car makes, and another list that has the car make and quantity.
If anyone can advise on how I continue the code to get the total I would be greatly appreciative! If there is a more efficient method then mine please do let me know.
I have a hunch that a dictionary may be the way to go but I could not figure out how to append the key values
Thanks all!
This is my first cry for help

I'm a Python Noob and I have been progressing fairly well (I started learning Python about a month ago). I have come to the point in my code that I simply cannot figure out. I am trying to take a simple .csv file and tally duplicates and combine values. I have tried dictionaries, tuples, sets, etc etc but I am simply missing something.
My production .csv is about 150k lines long and is a log for commodities market state change messages... so I created a basic example of what I am dealing with. I have pretty good luck with regex and could probably solve this problem via regex.. but I know that is slow.
Here is my example .csv... lets say I'm a car dealer that owns several car lots.. so I have duplicates in the list.
ford, 3
ford, 3
honda, 1
dodge, 8
dodge, 2
dodge, 5
ford, 6
dodge, 2
chevy, 1
dodge, 5
My goal is to have a combined list that would look like this... the order in the result does not matter.. I just need totals
chevy, 1
dodge, 22
ford, 9
honda, 1
So far I have coded the following that parses the .csv and creates a list of car makes, and another list that has the car make and quantity.
import csv
log = ('C:\\Users\\' + 'jcastleb' + '\\Desktop\\' + 'cars.csv')
f = open(log)
csv_f = csv.reader(f)
#Create empty lists
carlist = []
car_count = []
result = []
for row in csv_f:
#Create list of Car Makes and Numbers
car_count.append(row)
#Create the list of cars
if row[0] not in carlist:
carlist.append(row[0])
if row[0] not in result:
result.append(row[0])
print(carlist)
print(car_count)I get the following when I run the script:Output:['ford', 'honda', 'dodge', 'chevy']
[['ford', ' 3'], ['ford', ' 3'], ['honda', ' 1'], ['dodge', ' 8'], ['dodge', ' 2'], ['dodge', ' 5'], ['ford', ' 6'], ['dodge', ' 2'], ['chevy', ' 1'], ['dodge', ' 5']] If anyone can advise on how I continue the code to get the total I would be greatly appreciative! If there is a more efficient method then mine please do let me know.
I have a hunch that a dictionary may be the way to go but I could not figure out how to append the key values

Thanks all!
