in Education by
I am new to python, 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 =I am new to python, 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 have made my 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. But I am getting the below error File "temp.py", line 63, in genre = row[0] TypeError: 'int' object is not subscriptable What am I supposed to do to make that int into a list? row[1] print('{: <11} | {: >10}'.format(genre, count)) I have made my 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. But I am getting the below error File "temp.py", line 63, in genre = row[0] TypeError: 'int' object is not subscriptable What am I supposed to do to make that int into a list? Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
Your error is been introduced here: genres_counts.append(genre) genres_counts.append(count) You have append genre and count as separate values, not as the 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] If you are a beginner and want to know more about Data Science the do check out the data science with python course

Related questions

0 votes
    The code works fine but the only problem I encountered an error is: bad operand type for unary +: 'str'. This is ... for unary +: 'str' Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
0 votes
    Suppose I have a Tensorflow tensor. How do I get the dimensions (shape) of the tensor as integer values? I ... 'Dimension' instead. Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    Here's the code: ''' file_path = (r'C:\Users\Luka\Desktop\Pyhton exercises\pi_digits.txt') with open( ... file_object: print(line) Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    please see my code below, example_list = [ ['a','b','c'], ['f','g','h'], ['i','j','k'], ] my_string = ''' ' ... g','h'), ('i','j','k'); Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    Hi, I am a relatively new programmer and my teacher gave us this problem to fix. Thing is, I have no idea ... wrong with this problem? Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I would like to clear the written email and write password grid when a del button is pressed, and then I ... mainloop() EmailPassword() Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I need to find a way to clear the Listbox for the refresh function which I am making. The function should ... clearing the Listbox) Select the correct answer from above options...
asked Jan 18, 2022 in Education by JackTerrance
0 votes
    Can anyone tell me how do I start learning Python for Data Science? Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
0 votes
    Do I need coding skills for Data Science using Python? Select the correct answer from above options...
asked Jan 17, 2022 in Education by JackTerrance
0 votes
    If I use a while loop for my below code, it is not giving the desired output, but when i use for loop i am anle ... x += 1 print(Comm) Select the correct answer from above options...
asked Jan 9, 2022 in Education by JackTerrance
0 votes
    I have a List : Old_list =['orders_ce_deduped.01.csv','orders_ce_deduped.02.csv'] I need to get the 01 and 02 part ... = ['01','02'] Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I am trying to alter the data in the panda's df. Using below, where X >=5, I want to change the corresponding Y row to 1. Where X...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I wrote the code for the counter in python, but I did not my appropriate result. I made the list by input numbers, ... like: [33,16,19] Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I have a dataframe with 2 columns and I want to add the new column; This new column should be updated based on ... +=1 is not working. Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
0 votes
    I have the list like this where items are separated by ":". x=['john:42:engineer', 'michael:29:doctor' ... engineer 1 michael 29 doctor Select the correct answer from above options...
asked Jan 10, 2022 in Education by JackTerrance
...