python - Renaming countries in list -
I have a text file, which is formatted with the file name, rating score, and country of origin, which by tab Location on each line is separated: <3p>
"3: 0RRBB RTE" (1971) 6.8 West Germany "3K check in" (2002) 4.3 Federal Republic Yugoslavia "3 Mw: Rivers (2008) 7.9 UK "3y" (2008) 8.2 USA "Sun of Three Rock" (1996) 7.8 United States "Third and Bird" (2008) 7.8 UK "3satfestival" (2000) 6.7 Germany
My goal is to calculate the average rating score of each country, which works with the code given below, however, I want to change the names of some countries, such as' West Germany 'to' Germany 'so that their rating scores can be combined together, but I have a code that does not work. Rating score for' West Germany 'and' Germany 'are still calculated separately Can i change MovieRating = collections.namedtuple ('MovieRating', ['countryorigin', 'Ratingcore']) Ratings = {} movie = open ("movieRatingscore.txt", "R") ") # Open the country rating data file for the line in the movie. Lines (): line.rstrip () (moviename, ratingcore, countrymakers) = line.split ('\ t') if countryorigin == 'West Germany' Rating: CountryGuin = 'Germany' Ratingcoreia = Float (Rating Core) Rating Country Rating: Ratings [Countrywide] .append (Ratings) Other: Rating [CountryGermanya] = [Ratingscore] Average = Lambda Goodbye: Zodiac (Alast) / Lane (Alist) average_ratings = [MovieRating (Countrymen, average (Rating [Countrywomen ]) For the country in the rating) Print "\ n Highest Average Movie Rating Country \ n ---------------- -------------- "sorted_ratings = sorted (average_rating, key = lambda countrywide: countrymen.recentingcore, reverse = True) i, for j (sorted_rating): print '% i. % S \ t% g '% (i + 1, j.countryorigin, j.ratingscore)
The easiest way is to use a dictionary to change the words See sample code:
dt = {'West Germany': 'Germany', 'other': 'replaced '} For the line in the movie. Roadline (): DT for item: line = line.Raylay (item, dt [item])
Comments
Post a Comment