【问题标题】:I have taken following code from the book but not understand from numerous research我从书中获取了以下代码,但从大量研究中无法理解
【发布时间】:2020-06-16 09:40:27
【问题描述】:

我从一本名为 Automate The Boring Stuff With Python 的教科书中得到它。我做了很多研究,但仍然无法理解。任何人都可以逐步提供澄清吗?

    allGuests = {'Alice': {'apples': 5, 'pretzels': 12},
                'Bob': {'ham sandwiches': 3, 'apples': 2},
                'Carol': {'cups': 3, 'apple pies': 1}}

        def totalBrought(guests, item):
            numBrought = 0
            for k, v in guests.items():
                numBrought = numBrought + v.get(item, 0)
           return numBrought
    print('Number of things being brought:')
    print(' - Apples' + str(totalBrought(allGuests, 'apples')))
    print(' - Cups' + str(totalBrought(allGuests, 'cups')))
    print(' - Cakes ' + str(totalBrought(allGuests,'cakes')))
    print(' - Ham Sandwiches ' + str(totalBrought(allGuests, 'ham sandwiches')))
    print(' - Apple Pies' + str(totalBrought(allGuests,'apple pies')))

输出:

Number of things being brought:
- Apples 7
- Cups 3
- Cakes 0
- Ham Sandwiches 3
- Apple Pies
1

【问题讨论】:

  • 由于您还没有做很多研究,请尝试研究 Python 数据结构,尤其是字典,以了解这里发生了什么。例如这里:docs.python.org/2.7/tutorial/datastructures.html#dictionaries Stackoverflow 上还有一个很好的 Python 资源列表,可以帮助您入门,请参阅:stackoverflow.com/tags/python/info
  • 你是在 Python 2.x 还是 3.x 下运行这个?
  • 我在 python 3.x 中运行这段代码
  • 请询问您不理解的单个特定部分。例如,如果是 numBrought = 0 行让您感到困惑:它会将值 0 分配给名为 numBrought 的变量。如果您需要对每一行进行这样的解释,那么最好从头开始学习 Python。
  • 我在打印命令中怀疑它是如何从顶部检索数据的。

标签: python python-3.x python-2.7


【解决方案1】:
# this is dictionary. format is {<key>: <value>},
# in this code it is nested dictionary {<dict1>: {<innerkey>: <value>}}
allGuests = {'Alice': {'apples': 5, 'pretzels': 12},
             'Bob': {'ham sandwiches': 3, 'apples': 2},
             'Carol': {'cups': 3, 'apple pies': 1}}


# in this function you will receive nested dictionary (guests)
# and the value (item) which will be searched as key in dictionary
def totalBrought(guests, item):
    numBrought = 0  # this is return value and set as zero initially
    for k, v in guests.items():  # for loop to iterate each dictionary element and its nested elements, items() will return elements of dictionary
        numBrought = numBrought + v.get(item, 0)  # here you check whether your value is exist in inner dictionary.
                                                  # if so, you will get its value (integer in here) with get() function and sum them continuously
    return numBrought  # and return result


print('Number of things being brought:')
print(' - Apples' + str(totalBrought(allGuests, 'apples')))  # in total you have 7 apples ('apples': 5 + 'apples': 2)
print(' - Cups' + str(totalBrought(allGuests, 'cups')))
print(' - Cakes ' + str(totalBrought(allGuests, 'cakes')))
print(' - Ham Sandwiches ' + str(totalBrought(allGuests, 'ham sandwiches')))
print(' - Apple Pies' + str(totalBrought(allGuests, 'apple pies')))

【讨论】:

    猜你喜欢
    • 2020-08-07
    • 2011-09-06
    • 2020-04-02
    • 1970-01-01
    • 1970-01-01
    • 2022-06-11
    • 1970-01-01
    • 2021-06-30
    • 2017-02-03
    相关资源
    最近更新 更多