字典帮助!提取值和制作表格 [英] Dictionary Help! Extracting values and making a table

查看:119
本文介绍了字典帮助!提取值和制作表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我应该编码的问题:

Here's the question that I'm supposed to code for:


编写一个函数showCast的合同,docstring和实现电影标题,并以字母顺序从给定的电影中输出与相应演员/演员相关的角色。列必须对齐(演员/女演员姓名前20个字符(包括字符的名称))。如果没有找到电影,则打印出错误消息。

Write the contract, docstring and implementation for a function showCast that takes a movie title and prints out the characters with corresponding actors/actresses from the given movie in an alphabetical order of characters. The columns must be aligned (20 characters (including the character's name) before the name of the actor/actress.) If the movie is not found, it prints out an error message.

它给出了这里应该发生的一个例子。

It gives an example of what's supposed to happen here

>>> showCast("Harry Potter and the Sorcerer's Stone")

Character           Actor/Actress

----------------------------------------

Albus Dumbledore    Richard Harris

Harry Potter        Daniel Radcliffe

Hermione Granger    Emma Watson

Ron Weasley         Rupert Grint



>>> showCast('Hairy Potter')

No such movie found

我为同一个项目撰写的其他功能,可能有助于回答这个问题。我迄今为止所做的一个总结是,我正在使用电影标题的关键字创建一个名为myIMDb的字典,另外还有一个字典。在那个字典中,该键是电影的一个角色,值是演员。我已经做了一些事情。 myIMDb是记录的全局变量。

Here are other functions that I've written for the same project that will probably be of assistance in answering the question. A summary of what I've had to do so far is that I'm creating a dictionary, called myIMDb, with a key of the title of the movie, and the value another dictionary. In that dictionary that key is a character of a movie, and the value is the actor. And I've done stuff with it. myIMDb is a global variable for the record.

其他功能,他们所做的是docString

Other functions, what they do is the docString

def addMovie (title, charList, actList):
    """The function addMovie takes a title of the movie, a list of characters,
    and a list of actors. (The order of characters and actors match one
    another.) The function addMovie adds a pair to myIMDb. The key is the title
    of the movie while the value is a dictionary that matches characters to
    actors"""

    dict2 = {}
    for i in range (0, len(charList)):
        dict2 [charList[i]] = actList[i]
    myIMDb[title] = dict2
    return myIMDb

我已经添加了三部电影,

I've added three movies,

addMovie("Shutter Island", ["Teddy Daniels", "Chuck Aule"],["Leonardo DiCaprio, ","Mark Ruffalo"])

addMovie("Zombieland", ["Columbus", "Wichita"],["Jesse Eisenberg, ","Emma Stone"])

addMovie("O Brother, Where Art Thou", ["Everett McGill", "Pete Hogwallop"],["George Clooney, ","John Turturro"])



def listMovies():
    """returns a list of titles of all the movies in the global variable myIMDb"""

    return (list(myIMDb.keys()))


def findActor(title, name):
    """ takes a movie title and a character's name and returns the
    actor/actress that played the given character in the given movie. If the
    given movie or the given character is notfound, it prints out an error
    message"""
    if title in myIMDb:
        if name in myIMDb[title]:
            return myIMDb[title][name]
        else:
            return "Error:  Character not in Movie"
    else:
        return "Error: No movie found"






现在我遇到麻烦


Now where I'm having trouble

我应该编写showCast函数,但是我有很多麻烦,我一直在修补它一段时间,但是当我调用myIMDb.values()一切都返回,我似乎无法循环通过它来排序它们来创建表。

I'm supposed to write the showCast function, but I'm having a lot of trouble. I've been tinkering with it for a while but when I call myIMDb.values() everything returns. And I can't seem to loop through it to sort them to create the table.

这是我想出来的到目前为止,但它并没有做我所希望的,只是希望你们中的一个可以引导我走向正确的方向(被注释的区域是我以前做过的,只是这样可以看到我的火车的想法[打印( alist)和print(alist [0])只是为了确认它是一个列表中的一个大条目,不分离])

Here's what I've come up with so far, but it doesn't do what I was hoping. I'm just hoping that one of you can steer me in the right direction. (The commented out region is what I was doing before, just so you can see my train of thought. [the print(alist) and print(alist[0]) was just to confirm that it's one big entry in a list, not separated at all])

def showCast(title):

    if title in myIMDb:
        actList=[]
        chList=[]
        aList = myIMDb[title]
        print (aList)

          """"for i in range (len(aList)):
              if i%2==0:
                  chList.append(aList[i])
              else:
                  actList.append(aList[i])
          print(chList)
          print(actList)""""

else:
    return "Movie not Found"


推荐答案

p>首先,我不认为你应该在 addMovie 函数中返回任何东西。只需将其添加到全局变量中:

First, i don't think you should return anything in the addMovie function. Just simply add it to the global variable:

myIMDb = {}

def addMovie (title, charList, actList):
    global myIMDb

    """The function addMovie takes a title of the movie, a list of characters,
    and a list of actors. (The order of characters and actors match one
    another.) The function addMovie adds a pair to myIMDb. The key is the title
    of the movie while the value is a dictionary that matches characters to
    actors"""

    dict2 = {}
    for i in range (0, len(charList)):
        dict2 [charList[i]] = actList[i]
    myIMDb[title] = dict2

虽然我不建议经常使用全局变量,在这种情况下可以原谅:D

Although i don't recommend to use global variables often, i think it's forgivable in this case :D

之后,在您的 showCast 函数中,我会使用: / p>

After that, in your showCast function, i'd use this:

def showCast(title):
    if title in myIMDb:
        actList=[]
        chList=[]
        movie = myIMDb[title]
        for character, cast in movie.keys(), movie.values(): #grab the character from
        #the keys, and cast from the values. 
              chList.append(character)
              actList.append(cast)

        print (chList, actList)
    else:
        return "Movie not Found"

这是我的输出:

['Columbus', 'Jesse Eisenberg, '] ['Wichita', 'Emma Stone']

它按预期工作,希望这有帮助!

It's working as expected, hope this helps!

这篇关于字典帮助!提取值和制作表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆