-2

I have created a defaultdict using a csv file. The defaultdict has a movie director as the key and a list of named tuples as the values. The named tuples have 3 elements: title, year and score. I need to isolate the year element, check whether the year is after a given year then check those results score element for each tuple is high than a given score.

Here is the output from the dictionary:

defaultdict(<class 'list'>, {'James Cameron': [Movie(title='Avatar', year=2009, score=7.9), Movie(title='Titanic', year=1997, score=7.7), Movie(title='Terminator 2: Judgment Day', year=1991, score=8.5), Movie(title='True Lies', year=1994, score=7.2), Movie(title='The Abyss', year=1989, score=7.6), Movie(title='Aliens', year=1986, score=8.4), Movie(title='The Terminator', year=1984, score=8.1)]
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
balter
  • 25
  • 1
  • 9
  • What have you tried, and what exactly is the problem with it? You can access the values of a namedtuple by name or index, per [the docs](https://docs.python.org/3/library/collections.html#collections.namedtuple). – jonrsharpe Jun 02 '19 at 07:33
  • If I wanted to find the score for every movie, what would the code look like? – balter Jun 02 '19 at 08:47
  • Which part of that *exactly* are you stuck on? SO is neither a code-writing nor tutorial service, provide a [mcve] to illustrate the specific issue with your current code. – jonrsharpe Jun 02 '19 at 08:49
  • I’ve read the docs and tried too many things to list here, I just need to access each score element in the named tuple and I am unsure how to go about this. – balter Jun 02 '19 at 09:15

1 Answers1

-1

Not sure if i understood correctly but maybe something like like below : Generate a defautdict and then access the score element

d = defaultdict(list)
ls1 = [('James Cameron' , Movie(title='Avatar', year=2009, score=7.9)),('James Cameron' , Movie(title='avs', year=2010, score=8.1))]
for k,v in ls1:
    d[k].append(v)
for item in d['James Cameron']:
    print(item.score))
dome
  • 820
  • 7
  • 20
Vishnu
  • 110
  • 2
  • 10