Error 'int' object is not subscriptable, How do i convert it to list?

Gal Israel

I'm a python newbie and still learning, I have this code:

# columns are [0]title [1]year [2]rating [3]length(min) [4]genre [5]budget($mil) [6]box_office_gross($mil)
oscar_data = [
    ["The Shape of Water", 2017, 6.914, 123, ['sci-fi', 'drama'], 19.4, 195.243464],
    ["Moonlight", 2016, 6.151, 110, ['drama'], 1.5, 65.046687],
    ["Spotlight", 2015, 7.489, 129, ['drama', 'crime', 'history'], 20.0, 88.346473],
    ["Birdman", 2014, 7.604, 119, ['drama', 'comedy'], 18.0, 103.215094]
]


def filter_by_genre(data, genre):
    result = []
    for row in data:
        genres = row[4]
        if genre in genres:
            result.append(row)
    return result


all_genres = [
    'sci-fi', 'drama', 'crime', 'history', 'comedy', 'biography',
    'thriller', 'war', 'melodrama', 'action', 'adventure', 'western',
    'mystery', 'horror'
]

genres_counts = []
for genre in all_genres:
    count = len(filter_by_genre(oscar_data, genre))
    genres_counts.append(genre)
    genres_counts.append(count)



print('Genre        | Number')
print('------------------------')
for row in genres_counts:
    genre = row[0]
    count = row[1]
    print('{: <11} | {: >10}'.format(genre, count))

I've made the list shorter for the sake of the post, It supposed to count each genre in oscar_data and print it with the genre and the count.

I thought I've done it correctly, but I can't print it because it returns the error

  File "temp.py", line 63, in <module>
    genre = row[0]

TypeError: 'int' object is not subscriptable

What am I supposed to do to make that int a list?

Thank you!

DYZ

Your error is introduced here:

genres_counts.append(genre)
genres_counts.append(count)

You append the genre and the count as separate values, not as a collection. Replace those two lines with:

genres_counts.append((genre, count))

Better yet, use list comprehension:

genres_counts = [(genre, len(filter_by_genre(oscar_data, genre))  
                 for genre in all_genres]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

I'm getting 'int' object is not subscriptable

Error: 'int' object is not subscriptable - Python

Error: 'int' object is not subscriptable - Python

How do I convert an object array to an object list

How do I convert a string to a list of Maybe Int

how do I convert an object to an array list in JS

How can I fix the Type Error: 'int' object is not subscriptable for 8-piece puzzle?

Why do I get TypeError: 'int' object is not subscriptable using for loop and not with list-comprehension in python

int object is not subscriptable error and list mutability error

How do i convert List into datetime.date object?

How to Fix 'int' object is not subscriptable

Why do I get 'int' is not subscriptable

I encountered an error after running the code: ( sumation = sum(var.copy()) TypeError: 'int' object is not subscriptable)

Error "'int' object is not subscriptable" though object is a list

I can not solve TypeError: 'int' object is not subscriptable

Python Error: int object is not subscriptable

How do I convert int(input) into a list

How do I define a `NewType` that's subscriptable, like `List[int]`

What can I do to get rid of the 'int' object is not subscriptable error in my main function def model:

How to fix "'int' object is not subscriptable" in this case?

Why do I get 'int is not subscriptable' error message?

How do I convert Pandas object to a list in python3

I'm recieving a TypeError because a NoneType object is not subscriptable. How do I make it subscriptable?

Using reduceByKey is throwing an int object is not subscriptable error

I have an error int object isn't subscriptable

How do I solve TypeError: 'set' object is not subscriptable?

dataframe index: 'int' object is not subscriptable error

error comes as :- 'int' object is not subscriptable when i type latte

How do I convert the numbers in my Vector<Object> into int?