【问题标题】:How can I make the following python program(code) more efficient?如何使以下 python 程序(代码)更高效?
【发布时间】:2018-05-22 15:06:44
【问题描述】:

假设数据很大,解决以下问题的任何有效方法。我解决了这个问题,但我怎样才能改进代码,这将提高效率。有什么建议吗?

数据:

movie_sub_themes = {
'Epic': ['Ben Hur', 'Gone With the Wind', 'Lawrence of Arabia'],
'Spy': ['James Bond', 'Salt', 'Mission: Impossible'],
'Superhero': ['The Dark Knight Trilogy', 'Hancock, Superman'],
'Gangster': ['Gangs of New York', 'City of God', 'Reservoir Dogs'],
'Fairy Tale': ['Maleficent', 'Into the Woods', 'Jack the Giant Killer'],
'Romantic':['Casablanca', 'The English Patient', 'A Walk to Remember'],
'Epic Fantasy': ['Lord of the Rings', 'Chronicles of Narnia', 'Beowulf']}

movie_themes = {
'Action': ['Epic', 'Spy', 'Superhero'],
'Crime' : ['Gangster'],
'Fantasy' : ['Fairy Tale', 'Epic Fantasy'],
'Romance' : ['Romantic']}

themes_keys = movie_themes.keys()
theme_movies_keys = movie_sub_themes.keys()

#Iterate in movie_themes
#Check movie_themes keys in movie_sub_keys
#if yes append the movie_sub_keys into the newdict
newdict = {}
for i in range(len(themes_keys)):
   a = []
   for j in range(len(movie_themes[themes_keys[i]])):
     try:
         if movie_themes[themes_keys[i]][j] in theme_movies_keys:
            a.append(movie_sub_themes[movie_themes[themes_keys[i]][j]])
     except:
         pass
   newdict[themes_keys[i]] = a

# newdict contains nested lists
# Program to unpack the nested list into single list
# Storing the value into theme_movies_data 
theme_movies_data = {}
for k, v in newdict.iteritems():
    mylist_n = [j for i in v for j in i]
    theme_movies_data[k] = dict.fromkeys(mylist_n).keys()

print (theme_movies_data)

输出:

{'Action': ['Gone With the Wind', 'Ben Hur','Hancock, Superman','Mission: Impossible','James Bond','Lawrence of Arabia','Salt','The Dark Knight Trilogy'],
 'Crime': ['City of God', 'Reservoir Dogs', 'Gangs of New York'],
 'Fantasy': ['Jack the Giant Killer','Beowulf','Into the Woods','Maleficent','Lord of the Rings','Chronicles of Narnia'],
 'Romance': ['The English Patient', 'A Walk to Remember', 'Casablanca']}

抱歉没有正确注释代码。

我更关心运行时间。

谢谢你..

【问题讨论】:

  • 尝试codereview.stackexchange.com 以获取有关工作代码的帮助。
  • 为了让您的代码在 Python 3 上运行 - 将两个 keys()(现在是 View Objects)传递给 list 函数,并将 .iteritems() 替换为 .items()。

标签: python performance list dictionary


【解决方案1】:

您可以使用关系数据库来存储两个表,一个是电影及其子主题,另一个是与电影主题相关的子主题。然后,您可以使用 SQL 查询数据库,选择所有电影及其相关电影主题的列表。

这种方法会更有效,因为 SQL 命令往往会被编译以提高处理速度。关系数据库模型具有很强的可扩展性,因此可以以最小的开销处理非常大的数据集。

有关在 Python 中创建和使用简单数据库的示例,请参阅here。如果您对 SQL 操作不熟悉,请参阅here 获取有关有用操作的简单教程。

【讨论】:

    【解决方案2】:

    这是我的解决方案(使用 defaultdict):

    movie_sub_themes = {
    'Epic': ['Ben Hur', 'Gone With the Wind', 'Lawrence of Arabia'],
    'Spy': ['James Bond', 'Salt', 'Mission: Impossible'],
    'Superhero': ['The Dark Knight Trilogy', 'Hancock, Superman'],
    'Gangster': ['Gangs of New York', 'City of God', 'Reservoir Dogs'],
    'Fairy Tale': ['Maleficent', 'Into the Woods', 'Jack the Giant Killer'],
    'Romantic':['Casablanca', 'The English Patient', 'A Walk to Remember'],
    'Epic Fantasy': ['Lord of the Rings', 'Chronicles of Narnia', 'Beowulf']}
    
    movie_themes = {
    'Action': ['Epic', 'Spy', 'Superhero'],
    'Crime' : ['Gangster'],
    'Fantasy' : ['Fairy Tale', 'Epic Fantasy'],
    'Romance' : ['Romantic']}
    
    from collections import defaultdict
    newdict = defaultdict(list)
    
    for theme, sub_themes_list in movie_themes.items():
        for sub_theme in sub_themes_list:
            newdict[theme] += movie_sub_themes.get(sub_theme, [])       
    
    dict(newdict)
    
    >> {'Action': ['Ben Hur',
      'Gone With the Wind',
      'Lawrence of Arabia',
      'James Bond',
      'Salt',
      'Mission: Impossible',
      'The Dark Knight Trilogy',
      'Hancock, Superman'],
     'Crime': ['Gangs of New York', 'City of God', 'Reservoir Dogs'],
     'Fantasy': ['Maleficent',
      'Into the Woods',
      'Jack the Giant Killer',
      'Lord of the Rings',
      'Chronicles of Narnia',
      'Beowulf'],
     'Romance': ['Casablanca', 'The English Patient', 'A Walk to Remember']}
    

    时序:4.84 µs 对比 14.6 µs

    【讨论】:

    • 这一行出错:newdict = defaultdict(list) -> TypeError: first argument must be callable or None...
    • 你使用的是哪个版本的 python - 我的是 3.6.5。
    • 我怀疑您之前一定重新定义了列表函数。查找开头的行:list = ...
    • python 2.7版
    猜你喜欢
    • 2021-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-28
    • 2017-06-25
    相关资源
    最近更新 更多