Demo entry 6683905

asdf

   

Submitted by anonymous on Dec 14, 2017 at 02:40
Language: Python. Code size: 1.5 kB.

import csv
import re
data = []

#Open CSV file and create reader object
with open('C:\\Users\\Fontenot\\Documents\\IT-140 - Python\customerData.csv') as csvfile:
    reader = csv.DictReader(csvfile)
    #iterate through rows in reader object and fill list 'data' with the Favorite Radio Station Column Values
    for row in reader:
        data.append(row['Favorite Radio Station'][:2])
    #Conver the data list into a string separated by '.'
    stations = '.'.join(map(str,data)) + '.'

#Create empty dictionary to store radio station counts
count_list = []
counted = []
#Function to search the string of stations and count the number of items in the list
def count_stations(item):
    search_string = item
    #search for station in the stations list
    list_found = re.findall(item + '.', stations)
    #return the number of items found
    count = len(list_found)
    return count

#Iterate through list of stations
for item in data:
    #if station is not in already count list
    if not item in counted:
        #call count_stations function
        count = count_stations(item)
        #append count_list with the station and the number of entries
        count_list.append((item,count))
        #append a list to track whether we have counted that station or not
        counted.append(item)

#sort the count_list highest to lowest
count_list.sort(key=lambda x: x[1], reverse = True)

#print the top three stations
for item in range(3):
    print(count_list[item])

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).