0

我正在尝试使用来自元组 [the values of] 的键来构建字典,这些键的值应该是由字典的键和初始化为0.

元组看起来像:

characters = ('Fred', 'Sam', 'Bob', 'Daisy', 'Gina', 'Rupert')

涉及的字典如下所示:

jobs = {
    'Pizzeria': 1,
    'Mall Kiosk': 2
    'Restaurant': 3
    'Burger Joint': 4
    'Department Store': 5
}

我希望最终结构看起来像:

jobsWorkedCounter = {
    'Fred': {
        'Pizzeria': 0,
        'Mall Kiosk': 0
        'Restaurant': 0
        'Burger Joint': 0
        'Department Store': 0
    },
    'Sam': {
        'Pizzeria': 0,
        'Mall Kiosk': 0
        'Restaurant': 0
        'Burger Joint': 0
        'Department Store': 0
    },

    ...

    'Rupert': {
        'Pizzeria': 0,
        'Mall Kiosk': 0
        'Restaurant': 0
        'Burger Joint': 0
        'Department Store': 0
    },
}

最终目标是有一个递增计数器的结构:

jobsWorkedCounter['Fred']['Burger Joint'] += 1

我尝试过使用各种嵌套理解:

jobsWorkedCounter = { char: dict((key, 0) for key in jobs.keys()) for char in characters }

# and

jobsWorkedCounter = { char: dict(jobs.keys(), 0) for char in characters }

# and

jobsWorkedCounterDict = { key: 0 for key in jobs.keys() }
jobsWorkedCounter = { char: jobsWorkedCounterDict for char in characters }

# and

jobsWorkedCounter = { char: { key: 0 for key in jobs.keys() } for char in characters }

和一个简单的for循环:

jobsWorkedCounter = { char: {} for char in characters }
    for char in characters:
        jobsWorkedCounter[char] = dict.fromkeys(jobs.keys(), 0)

但我能做到的最好的是单个子键而不是全套:

jobsWorkedCounter = {
    'Fred': {
        'Pizzeria': 0,
    },
    'Sam': {
        'Pizzeria': 0,
    },

    ...

    'Rupert': {
        'Pizzeria': 0,
    },
}

似乎无论我尝试什么,我都设法将新字典扁平化为单个键值对,这就是从元组中分配给键的内容。

我怎样才能完成我想做的事情?

另外,以防万一我做错,检查输出我这样做:

keys = jobsWorkedCounter['Fred'].keys()
raise Exception(keys)

这让我:

Exception: [u'Pizzeria']

我希望看到的地方:

Exception: [u'Pizzeria', u'Mall Kiosk', u'Restaurant', u'Burger Joint', u'Department Store']  

我相当确定这种查看密钥的方法应该有效,因为如果我将其更改为:

keys = jobsWorkedCounter.keys()
raise Exception(keys)

我得到:

Exception: [u'Fred', u'Sam', u'Bob', u'Daisy', u'Gina', u'Rupert']

附录

我在 Ren'Py 环境中使用 Python 2.7(因此引发异常以查看输出的原因)。

例如:

from pprint import pprint

给我:

Import Error: No module named pprint
4

2 回答 2

1

使用 dict 理解:

characters = ('Fred', 'Sam', 'Bob', 'Daisy', 'Gina', 'Rupert')

jobs = {
    'Pizzeria': 1,
    'Mall Kiosk': 2,
    'Restaurant': 3,
    'Burger Joint': 4,
    'Department Store': 5
}

jobsWorkedCounter = {c: {k: 0 for k in jobs} for c in characters}

# For pretty print:
#from pprint import pprint
#pprint(jobsWorkedCounter)

print(jobsWorkedCounter)

印刷:

{'Bob': {'Burger Joint': 0,
         'Department Store': 0,
         'Mall Kiosk': 0,
         'Pizzeria': 0,
         'Restaurant': 0},
 'Daisy': {'Burger Joint': 0,
           'Department Store': 0,
           'Mall Kiosk': 0,
           'Pizzeria': 0,
           'Restaurant': 0},
 'Fred': {'Burger Joint': 0,
          'Department Store': 0,
          'Mall Kiosk': 0,
          'Pizzeria': 0,
          'Restaurant': 0},
 'Gina': {'Burger Joint': 0,
          'Department Store': 0,
          'Mall Kiosk': 0,
          'Pizzeria': 0,
          'Restaurant': 0},
 'Rupert': {'Burger Joint': 0,
            'Department Store': 0,
            'Mall Kiosk': 0,
            'Pizzeria': 0,
            'Restaurant': 0},
 'Sam': {'Burger Joint': 0,
         'Department Store': 0,
         'Mall Kiosk': 0,
         'Pizzeria': 0,
         'Restaurant': 0}}

编辑:另一个明确的版本:

zeroed_jobs = dict((k, 0) for k in jobs)
jobsWorkedCounter = {c: dict(**zeroed_jobs) for c in characters}

print(jobsWorkedCounter)
于 2019-07-24T18:04:08.550 回答
1

我创建了一个新的 Ren'Py 项目(来自 Ubuntu 18.04)并在screens.rpy. 这基本上是您的试探之一:

init python:
    characters = ('Fred', 'Sam', 'Bob', 'Daisy', 'Gina', 'Rupert')

    jobs = {
        'Pizzeria': 1,
        'Mall Kiosk': 2,
        'Restaurant': 3,
        'Burger Joint': 4,
        'Department Store': 5
    }

    jobsWorkedCounter = {char: {key: 0 for key in jobs.keys()} for char in characters}
    keys = jobsWorkedCounter['Fred'].keys()
    raise Exception(keys)

我得到:

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/screens.rpy", line 5, in script
    init python:
  File "game/screens.rpy", line 19, in <module>
    raise Exception(keys)
Exception: [u'Department Store', u'Pizzeria', u'Restaurant', u'Mall Kiosk', u'Burger Joint']

-- Full Traceback ------------------------------------------------------------

Full traceback:
  File "game/screens.rpy", line 5, in script
    init python:
  File "/usr/share/games/renpy/renpy/ast.py", line 848, in execute
    renpy.python.py_exec_bytecode(self.code.bytecode, self.hide, store=self.store)
  File "/usr/share/games/renpy/renpy/python.py", line 1812, in py_exec_bytecode
    exec bytecode in globals, locals
  File "game/screens.rpy", line 19, in <module>
    raise Exception(keys)
Exception: [u'Department Store', u'Pizzeria', u'Restaurant', u'Mall Kiosk', u'Burger Joint']

Linux-4.15.0-55-generic-x86_64-with-Ubuntu-18.04-bionic
Ren'Py 6.99.14.1.3218
test_renpy 1.0
Wed Jul 24 21:03:28 2019

因此,我倾向于认为您的代码中的其他地方存在错误。

于 2019-07-24T18:35:47.490 回答