从一个元组和另一个字典的键初始化一个新字典

Initialize a new dictionary from a tuple and the keys of another dictionary

我正在尝试使用来自元组 [的值] 的键来构建字典,这些键的值应该是由字典的键和子的值组成的新字典-key 初始化为 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

使用字典理解:

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)

我创建了一个新的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

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