我有一个名为movies的列表,其中包含包括诸如标题、描述、流派等键:值对的字典.我希望能够在该列表中找到给定电影描述的字符串长度

以下是我try 过的方法

def get_movie_description_length(dictionary_entry):
  print(len(dictionary_entry['description']))

get_movie_description_length('The Godfather')

它抛出了

TypeError: list indices must be integers or slices, not str

我如何调整代码才能使其正常工作?

我原以为它会返回整数149,因为这是字符串的长度,也就是键'description'的值

推荐答案

# toy dataset
movies = [
    {
        'title': 'The Shawshank Redemption',
        'description': 'Two imprisoned men bond over several years, finding solace and eventual redemption through acts of common decency.',
        'genre': 'Drama'
    },
    {
        'title': 'Inception',
        'description': 'A thief who steals corporate secrets through the use of dream-sharing technology is given the inverse task of planting an idea into the mind of a CEO.',
        'genre': 'Action, Adventure, Sci-Fi'
    },
    {
        'title': 'Pulp Fiction',
        'description': 'The lives of two mob hitmen, a boxer, a gangster and his wife, and a pair of diner bandits intertwine in four tales of violence and redemption.',
        'genre': 'Crime, Drama'
    },
    {
        'title': 'The Dark Knight',
        'description': 'When the menace known as the Joker wreaks havoc and chaos on the people of Gotham, Batman must accept one of the greatest psychological and physical tests of his ability to fight injustice.',
        'genre': 'Action, Crime, Drama'
    },
    {
        'title': 'The Matrix',
        'description': 'A computer hacker learns from mysterious rebels about the true nature of his reality and his role in the war against its controllers.',
        'genre': 'Action, Sci-Fi'
    }
]


def get_description_length(movie_title):
    for movie in movies:
        if movie['title'] == movie_title:
            return len(movie['description'])
    return 0  # Return 0 if the movie title is not found in the dataset

print(get_description_length('The Matrix'))

如果数据是字典:

movies = {
    'The Shawshank Redemption': {
        'description': 'Two imprisoned men bond over several years, finding solace and eventual redemption through acts of common decency.',
        'genre': 'Drama'
    },
    'Inception': {
        'description': 'A thief who steals corporate secrets through the use of dream-sharing technology is given the inverse task of planting an idea into the mind of a CEO.',
        'genre': 'Action, Adventure, Sci-Fi'
    },
    'Pulp Fiction': {
        'description': 'The lives of two mob hitmen, a boxer, a gangster and his wife, and a pair of diner bandits intertwine in four tales of violence and redemption.',
        'genre': 'Crime, Drama'
    },
    'The Dark Knight': {
        'description': 'When the menace known as the Joker wreaks havoc and chaos on the people of Gotham, Batman must accept one of the greatest psychological and physical tests of his ability to fight injustice.',
        'genre': 'Action, Crime, Drama'
    },
    'The Matrix': {
        'description': 'A computer hacker learns from mysterious rebels about the true nature of his reality and his role in the war against its controllers.',
        'genre': 'Action, Sci-Fi'
    }
}

那么这样的事情就会奏效:

def get_movie_description_length(dictionary_entry):
  print(len(movies[dictionary_entry]['description']))

get_movie_description_length('The Matrix')

Python相关问答推荐

为什么自定义pytree aux_data对于jnp.数组来说在.jit()之后跟踪,而对于np.数组来说则不是?

日程优化问题不知道更好的方式来呈现解决方案- Python / Gekko

当pip为学校作业(job)安装sourcefender时,我没有收到匹配的分发错误.我已经try 过Python 3.8.10和3.10.11

在Python中,如何才能/应该使用decorator 来实现函数多态性?

使用Python OpenCV的文本检测分割

当密钥是复合且唯一时,Pandas合并抱怨标签不唯一

比较两个二元组列表,NP.isin

将numpy数组存储在原始二进制文件中

我在使用fill_between()将最大和最小带应用到我的图表中时遇到问题

Python库:可选地支持numpy类型,而不依赖于numpy

为什么sys.exit()不能与subproccess.run()或subprocess.call()一起使用

在Wayland上使用setCellWidget时,try 编辑QTable Widget中的单元格时,PyQt 6崩溃

Telethon加入私有频道

* 动态地 * 修饰Python中的递归函数

下三角形掩码与seaborn clustermap bug

如何在两列上groupBy,并使用pyspark计算每个分组列的平均总价值

在方法中设置属性值时,如何处理语句不可达[Unreacable]";的问题?

搜索按钮不工作,Python tkinter

Python避免mypy在相互引用中从另一个类重定义类时失败

并行编程:同步进程