So I'm tasked with finding the most popular bid from a list of csv files like this
1,8dac2b,ewmzr,jewelry,phone0,9759243157894736,us,69.166.231.58,vasstdc27m7nks3
2,668d39,aeqok,furniture,phone1,9759243157894736,jp,50.201.125.84,jmqlhflrzwuay9c
3,622r49,arqek,vehicle,phone2,9759544365415694736,az,53.001.135.54,weqlhrerreuert6f
4,6444t43,rrdwk,vehicle,phone9,9759543263245434353,au,54.241.234.64,weqqyqtqwrtert6f
and I have to determine the most popular bid, which is found in every 4th element within the csv file...
so the thing that I'm gonna be looking for are the ones with (** **) around them, as indicated don below from the same example
1,8dac2b,ewmzr,**jewelry**,phone0,9759243157894736,us,69.166.231.58,vasstdc27m7nks3
2,668d39,aeqok,**furniture**,phone1,9759243157894736,jp,50.201.125.84,jmqlhflrzwuay9c
3,622r49,arqek,**vehicle**,phone2,9759544365415694736,az,53.001.135.54,weqlhrerreuert6f
4,6444t43,rrdwk,**vehicle**,phone9,9759543263245434353,au,54.241.234.64,weqqyqtqwrtert6f
So from my example, the output should be vehicle
Look at my previous question here where I asked the same thing: How can I return the most reoccuring value in the given csv list?
Now with the help of a kind StackOverflow user https://stackoverflow.com/users/5387738/nikita-almakov , I was able to get this code...
import csv
value_to_count = {}
with open("tmp.csv", "r") as f:
reader = csv.reader(f)
for row in reader:
category = row[3]
if category in value_to_count:
value_to_count[category] = 1
else:
value_to_count[category] = 1
count_to_value = sorted((v, k) for k, v in value_to_count.items())
However, when I tried to put this code under a function popvote(filename)
, I encounter an issue which is that I don't know where to include the filename
in the piece of code in order to get the right output.
Here is what I have so far:
import csv
def popvote(filename):
value_to_count = {}
with open("tmp.csv", "r") as f:
reader = csv.reader(f)
for row in reader:
category = row[3]
if category in value_to_count:
value_to_count[category] = 1
else:
value_to_count[category] = 1
count_to_value = sorted((v, k) for k, v in value_to_count.items())
return count_to_value
However, the output, which as expected is not being printed correctly. I know that I haven't implemented filename
into the function, and the reason is I don't know where to do so, can someone please help me figure this out?
CodePudding user response:
Change
with open("tmp.csv", "r") as f:
to
with open(filename, "r") as f:
CodePudding user response:
If the file filename
ist the CSV file with the Values above, then you should replace the "tmp.csv"
with filename
. Then this function should return an tuple with the most popular bids on the front.