New to programming and tried to find solution but keep on not getting it right. Trying to make a list with the count of Oscar wins for the directors in the list.
**Input**
winners = {1931: ['Norman Taurog'], 1932: ['Frank Borzage'], 1933: ['Frank Lloyd'], 1934: ['Frank Capra'], 1935: ['John Ford'], 1936: ['Frank Capra'], 1937: ['Leo McCarey'], 1938: ['Frank Capra'], 1939: ['Victor Fleming'], 1940: ['John Ford'], 1941: ['John Ford'], 1942: ['William Wyler'], 1943: ['Michael Curtiz'], 1944: ['Leo McCarey'], 1945: ['Billy Wilder'], 1946: ['William Wyler'], 1947: ['Elia Kazan'], 1948: ['John Huston'], 1949: ['Joseph L. Mankiewicz'], 1950: ['Joseph L. Mankiewicz'], 1951: ['George Stevens'], 1952: ['John Ford'], 1953: ['Fred Zinnemann'], 1954: ['Elia Kazan'], 1955: ['Delbert Mann'], 1956: ['George Stevens'], 1957: ['David Lean'], 1958: ['Vincente Minnelli'], 1959: ['William Wyler'], 1960: ['Billy Wilder'], 1961: ['Jerome Robbins', 'Robert Wise'], 1962: ['David Lean'], 1963: ['Tony Richardson'], 1964: ['George Cukor'], 1965: ['Robert Wise'], 1966: ['Fred Zinnemann'], 1967: ['Mike Nichols'], 1968: ['Carol Reed'], 1969: ['John Schlesinger'], 1970: ['Franklin J. Schaffner'], 1971: ['William Friedkin'], 1972: ['Bob Fosse'], 1973: ['George Roy Hill'], 1974: ['Francis Ford Coppola'], 1975: ['Milos Forman'], 1976: ['John G. Avildsen'], 1977: ['Woody Allen'], 1978: ['Michael Cimino'], 1979: ['Robert Benton'], 1980: ['Robert Redford'], 1981: ['Warren Beatty'], 1982: ['Richard Attenborough'], 1983: ['James L. Brooks'], 1984: ['Milos Forman'], 1985: ['Sydney Pollack'], 1986: ['Oliver Stone'], 1987: ['Bernardo Bertolucci'], 1988: ['Barry Levinson'], 1989: ['Oliver Stone'], 1990: ['Kevin Costner'], 1991: ['Jonathan Demme'], 1992: ['Clint Eastwood'], 1993: ['Steven Spielberg'], 1994: ['Robert Zemeckis'], 1995: ['Mel Gibson'], 1996: ['Anthony Minghella'], 1997: ['James Cameron'], 1998: ['Steven Spielberg'], 1999: ['Sam Mendes'], 2000: ['Steven Soderbergh'], 2001: ['Ron Howard'], 2002: ['Roman Polanski'], 2003: ['Peter Jackson'], 2004: ['Clint Eastwood'], 2005: ['Ang Lee'], 2006: ['Martin Scorsese'], 2007: ['Ethan Coen', 'Joel Coen'], 2008: ['Danny Boyle'], 2009: ['Kathryn Bigelow'], 2010: ['Tom Hooper']}
best_director = []
for year, winnerlist in best_director:
for winner in winnerlist:
best_director[winner] = 1
else:
best_director[winner] = 1
print("Top winning director = {}".format(best_director))
**Output**
Top winning director = []
CodePudding user response:
You can use collections.Counter
along with itertools.chain
will get all the names together:
result = Counter(chain(*winners.values()))
Counter({'John Ford': 4, 'Frank Capra': 3, 'William Wyler': 3, 'Leo McCarey': 2, 'Billy Wilder': 2, 'Elia Kazan': 2, 'Joseph L. Mankiewicz': 2, 'George Stevens': 2, 'Fred Zinnemann': 2, 'David Lean': 2, 'Robert Wise': 2, 'Milos Forman': 2, 'Oliver Stone': 2, 'Clint Eastwood': 2, 'Steven Spielberg': 2, 'Norman Taurog': 1, 'Frank Borzage': 1, 'Frank Lloyd': 1, 'Victor Fleming': 1, 'Michael Curtiz': 1, 'John Huston': 1, 'Delbert Mann': 1, 'Vincente Minnelli': 1, 'Jerome Robbins': 1, 'Tony Richardson': 1, 'George Cukor': 1, 'Mike Nichols': 1, 'Carol Reed': 1, 'John Schlesinger': 1, 'Franklin J. Schaffner': 1, 'William Friedkin': 1, 'Bob Fosse': 1, 'George Roy Hill': 1, 'Francis Ford Coppola': 1, 'John G. Avildsen': 1, 'Woody Allen': 1, 'Michael Cimino': 1, 'Robert Benton': 1, 'Robert Redford': 1, 'Warren Beatty': 1, 'Richard Attenborough': 1, 'James L. Brooks': 1, 'Sydney Pollack': 1, 'Bernardo Bertolucci': 1, 'Barry Levinson': 1, 'Kevin Costner': 1, 'Jonathan Demme': 1, 'Robert Zemeckis': 1, 'Mel Gibson': 1, 'Anthony Minghella': 1, 'James Cameron': 1, 'Sam Mendes': 1, 'Steven Soderbergh': 1, 'Ron Howard': 1, 'Roman Polanski': 1, 'Peter Jackson': 1, 'Ang Lee': 1, 'Martin Scorsese': 1, 'Ethan Coen': 1, 'Joel Coen': 1, 'Danny Boyle': 1, 'Kathryn Bigelow': 1, 'Tom Hooper': 1})
And to get the "best" director just use Counter.most_common
:
result.most_common(1)[0][0]
John Ford
Edit: You can also just use statistics.mode
along with itertools.chain
if you only want the most common name:
mode(chain(*winners.values()))
John Ford
CodePudding user response:
You need to iterate over the items of winners:
for year, winnerlist in winners.items():
You'll also need to make best_director
a dictionary instead of a list. You will also need to check if a director already exists in best_director
and if not set the initial value otherwise increment the count. Alternatively, you could use a Counter.
CodePudding user response:
Alternatively you can use DefaultDict from collections module
from typing import DefaultDict
winners = {1931: ['Norman Taurog'], 1932: ['Frank Borzage'], 1933: ['Frank Lloyd'], 1934: ['Frank Capra'], 1935: ['John Ford'], 1936: ['Frank Capra'], 1937: ['Leo McCarey'], 1938: ['Frank Capra'], 1939: ['Victor Fleming'], 1940: ['John Ford'], 1941: ['John Ford'], 1942: ['William Wyler'], 1943: ['Michael Curtiz'], 1944: ['Leo McCarey'], 1945: ['Billy Wilder'], 1946: ['William Wyler'], 1947: ['Elia Kazan'], 1948: ['John Huston'], 1949: ['Joseph L. Mankiewicz'], 1950: ['Joseph L. Mankiewicz'], 1951: ['George Stevens'], 1952: ['John Ford'], 1953: ['Fred Zinnemann'], 1954: ['Elia Kazan'], 1955: ['Delbert Mann'], 1956: ['George Stevens'], 1957: ['David Lean'], 1958: ['Vincente Minnelli'], 1959: ['William Wyler'], 1960: ['Billy Wilder'], 1961: ['Jerome Robbins', 'Robert Wise'], 1962: ['David Lean'], 1963: ['Tony Richardson'], 1964: ['George Cukor'], 1965: ['Robert Wise'], 1966: ['Fred Zinnemann'], 1967: ['Mike Nichols'], 1968: ['Carol Reed'], 1969: ['John Schlesinger'], 1970: ['Franklin J. Schaffner'], 1971: ['William Friedkin'], 1972: ['Bob Fosse'], 1973: ['George Roy Hill'], 1974: ['Francis Ford Coppola'], 1975: ['Milos Forman'], 1976: ['John G. Avildsen'], 1977: ['Woody Allen'], 1978: ['Michael Cimino'], 1979: ['Robert Benton'], 1980: ['Robert Redford'], 1981: ['Warren Beatty'], 1982: ['Richard Attenborough'], 1983: ['James L. Brooks'], 1984: ['Milos Forman'], 1985: ['Sydney Pollack'], 1986: ['Oliver Stone'], 1987: ['Bernardo Bertolucci'], 1988: ['Barry Levinson'], 1989: ['Oliver Stone'], 1990: ['Kevin Costner'], 1991: ['Jonathan Demme'], 1992: ['Clint Eastwood'], 1993: ['Steven Spielberg'], 1994: ['Robert Zemeckis'], 1995: ['Mel Gibson'], 1996: ['Anthony Minghella'], 1997: ['James Cameron'], 1998: ['Steven Spielberg'], 1999: ['Sam Mendes'], 2000: ['Steven Soderbergh'], 2001: ['Ron Howard'], 2002: ['Roman Polanski'], 2003: ['Peter Jackson'], 2004: ['Clint Eastwood'], 2005: ['Ang Lee'], 2006: ['Martin Scorsese'], 2007: ['Ethan Coen', 'Joel Coen'], 2008: ['Danny Boyle'], 2009: ['Kathryn Bigelow'], 2010: ['Tom Hooper']}
director_count = DefaultDict(int)
for year, winnerlist in winners.items():
for winner in winnerlist:
director_count[winner] = 1
count_map = DefaultDict(list)
for director, count in director_count.items():
count_map[count].append(director)
print("Top winning director = {}".format(count_map[max(count_map)]))
Output:
Top winning director = ['John Ford']
Lets get the values for the variables
print(director_count)
print(count_map)
Output:
defaultdict(<class 'int'>, {'Norman Taurog': 1, 'Frank Borzage': 1, 'Frank Lloyd': 1, 'Frank Capra': 3, 'John Ford': 4, 'Leo McCarey': 2, 'Victor Fleming': 1, 'William Wyler': 3, 'Michael Curtiz': 1, 'Billy Wilder': 2, 'Elia Kazan': 2, 'John Huston': 1, 'Joseph L. Mankiewicz': 2, 'George Stevens': 2, 'Fred Zinnemann': 2, 'Delbert Mann': 1, 'David Lean': 2, 'Vincente Minnelli': 1, 'Jerome Robbins': 1, 'Robert Wise': 2, 'Tony Richardson': 1, 'George Cukor': 1, 'Mike Nichols': 1, 'Carol Reed': 1, 'John Schlesinger': 1, 'Franklin J. Schaffner': 1, 'William Friedkin': 1, 'Bob Fosse': 1, 'George Roy Hill': 1, 'Francis Ford Coppola': 1, 'Milos Forman': 2, 'John G. Avildsen': 1, 'Woody Allen': 1, 'Michael Cimino': 1, 'Robert Benton': 1, 'Robert Redford': 1, 'Warren Beatty': 1, 'Richard Attenborough': 1, 'James L. Brooks': 1, 'Sydney Pollack': 1, 'Oliver Stone': 2, 'Bernardo Bertolucci': 1, 'Barry Levinson': 1, 'Kevin Costner': 1, 'Jonathan Demme': 1, 'Clint Eastwood': 2, 'Steven Spielberg': 2, 'Robert Zemeckis': 1, 'Mel Gibson': 1, 'Anthony Minghella': 1, 'James Cameron': 1, 'Sam Mendes': 1, 'Steven Soderbergh': 1, 'Ron Howard': 1, 'Roman Polanski': 1, 'Peter Jackson': 1, 'Ang Lee': 1, 'Martin Scorsese': 1, 'Ethan Coen': 1, 'Joel Coen': 1, 'Danny Boyle': 1, 'Kathryn Bigelow': 1, 'Tom Hooper': 1})
defaultdict(<class 'list'>, {1: ['Norman Taurog', 'Frank Borzage', 'Frank Lloyd', 'Victor Fleming', 'Michael Curtiz', 'John Huston', 'Delbert Mann', 'Vincente Minnelli', 'Jerome Robbins', 'Tony Richardson', 'George Cukor', 'Mike Nichols', 'Carol Reed', 'John Schlesinger', 'Franklin J. Schaffner', 'William Friedkin', 'Bob Fosse', 'George Roy Hill', 'Francis Ford Coppola', 'John G. Avildsen', 'Woody Allen', 'Michael Cimino', 'Robert Benton', 'Robert Redford', 'Warren Beatty', 'Richard Attenborough', 'James L. Brooks', 'Sydney Pollack', 'Bernardo Bertolucci', 'Barry Levinson', 'Kevin Costner', 'Jonathan Demme', 'Robert Zemeckis', 'Mel Gibson', 'Anthony Minghella', 'James Cameron', 'Sam Mendes', 'Steven Soderbergh', 'Ron Howard', 'Roman Polanski', 'Peter Jackson', 'Ang Lee', 'Martin Scorsese', 'Ethan Coen', 'Joel Coen', 'Danny Boyle', 'Kathryn Bigelow', 'Tom Hooper'], 3: ['Frank Capra', 'William Wyler'], 4: ['John Ford'], 2: ['Leo McCarey', 'Billy Wilder', 'Elia Kazan', 'Joseph L. Mankiewicz', 'George Stevens', 'Fred Zinnemann', 'David Lean', 'Robert Wise', 'Milos Forman', 'Oliver Stone', 'Clint Eastwood', 'Steven Spielberg']})
CodePudding user response:
Try get
:
best_director = {}
for year, winnerlist in winners.items():
for winner in winnerlist:
best_director[winner] = best_director.get(winner, 0) 1
print("Top winning director = {}".format(max(best_director, key=lambda x: best_director[x])))