Python list not getting passed? -
def main(): winners = [] winnersnd = [] readdata() noduplicates(winners) def readdata(): winners_file = open('worldserieswinners.txt', 'r') winners = winners_file.readlines() winners_file.close() index = 0 while index < len(winners): winners[index] = winners[index].rstrip('\n') index += 1 print(winners) print() print() homecoming winners def noduplicates(winners): winnersnd = [] x in winners: if x not in winnersnd: winnersnd.append(x) print (winnersnd) winnersnd = tuple(winnersnd) homecoming winnersnd main()
so i've been looking everywhere , i've gone through 7 hours worth of iterations of code trying create work, reason, winners list gets printed in readdata()
, winnersnd
list, list of unique , ordered winners list elements, not printed, , instead displays output: []
. i'd love clarification on i'm messing here. give thanks you!
your issue here:
winners = readdata() # need homecoming list here... noduplicates(winners) # otherwise it's empty here.
this done this.
def main(): winners = [x.rstrip() x in open('worldserieswinners.txt').readlines()] winnersnd = list(set(winners))
since asked in comments, how can check how many times each team has won.
from collections import counter winner, count in counter(winners).iteritems(): print '{0} has won {1} times'.format(winner, count)
python list tuples unique
No comments:
Post a Comment