Python 以key1.key2.key3格式打印字典中的所有键

Python 以key1.key2.key3格式打印字典中的所有键,python,json,dictionary,key,Python,Json,Dictionary,Key,这是我的字典(或JSON) 我想用这种格式打印所有钥匙key1.key2.key3。这是我的代码: def myprint(d, keys = ''): for k, v in d.items(): temp = keys keys += k if isinstance(v,dict): keys += '.' myprint(v,keys) else:

这是我的字典(或JSON)

我想用这种格式打印所有钥匙
key1.key2.key3
。这是我的代码:

def myprint(d, keys = ''):
    for k, v in d.items():
        temp = keys
        keys += k
        if isinstance(v,dict):
            keys += '.'
            myprint(v,keys)
        else:
            print(keys)
            keys = temp
不幸的是,此操作失败,返回结果如下:

$schema
type
name
properties.stock.type
properties.stock.properties.warehouse.type
properties.stock.properties.warehouse.retail.type
properties.stock.price.minimum
properties.stock.price.type
properties.stock.price.required
properties.stock.price.tags.items.type
properties.stock.price.tags.items.type
properties.stock.price.tags.id.required
properties.stock.price.tags.id.type
properties.stock.price.tags.id.description
properties.stock.price.tags.id.name.required
properties.stock.price.tags.id.name.type
properties.stock.price.tags.id.name.description
如你所见,最后几行是错误的


有人有什么建议吗?不仅受此脚本的限制,还欢迎使用其他方法,而且不使用任何模块。

我认为更新
键会使它变得复杂。Python中的字符串是不可变的。我们每次都可以将扩展密钥传递到下一个递归级别,因此:

def myprint(d, keys = ''):
    for k, v in d.items():
        if isinstance(v, dict):
            myprint(v, '{}{}.'.format(keys, k))
        else:
            print('{}{}'.format(keys, k))
这将产生:

>>> myprint(d)
$schema
name
type
properties.id.type
properties.id.description
properties.id.required
properties.name.type
properties.name.description
properties.name.required
properties.price.type
properties.price.minimum
properties.price.required
properties.tags.type
properties.tags.items.type
properties.stock.type
properties.stock.properties.warehouse.type
properties.stock.properties.retail.type
代码的问题在于,如果不是字典,您只能还原旧的
值。因此,如果存在多个子字典,则开始将键连接在一起。

可以使用递归:

d = {'$schema': 'http://json-schema.org/draft-03/schema#', 'name': 'Product', 'type': 'object', 'properties': {'id': {'type': 'number', 'description': 'Product identifier', 'required': True}, 'name': {'type': 'string', 'description': 'Name of the product', 'required': True}, 'price': {'type': 'number', 'minimum': 0, 'required': True}, 'tags': {'type': 'array', 'items': {'type': 'string'}}, 'stock': {'type': 'object', 'properties': {'warehouse': {'type': 'number'}, 'retail': {'type': 'number'}}}}}
def display_keys(s, last=None):
   for a, b in s.items():
      if not isinstance(b, dict):
          yield "{}.{}".format(last, a) if last else str(a)
      else:
         for h in display_keys(b, str(a) if not last else '{}.{}'.format(last, a)):
           yield h

 print(list(display_keys(d)))
输出:

['$schema', 'name', 'type', 'properties.id.type', 'properties.id.description', 'properties.id.required', 'properties.name.type', 'properties.name.description', 'properties.name.required', 'properties.price.type', 'properties.price.minimum', 'properties.price.required', 'properties.tags.type', 'properties.tags.items.type', 'properties.stock.type', 'properties.stock.properties.warehouse.type', 'properties.stock.properties.retail.type']

“没有使用模块”是什么意思?@wim与没有导入集合(或其他模块)一样,
collections
内置于任何Python安装中。那么,你为什么不能用它呢?@wim:因为这可能是homework@WillemVanOnsem作业如果不使用导入模块,我从未收到过此级别的h/w。古怪的
['$schema', 'name', 'type', 'properties.id.type', 'properties.id.description', 'properties.id.required', 'properties.name.type', 'properties.name.description', 'properties.name.required', 'properties.price.type', 'properties.price.minimum', 'properties.price.required', 'properties.tags.type', 'properties.tags.items.type', 'properties.stock.type', 'properties.stock.properties.warehouse.type', 'properties.stock.properties.retail.type']