dictが関数呼び出しであると述べています。それは3つの構文形式を有する。
形式:(ここで使用される、またはname
)
dict(**kwargs) -> new dictionary initialized with the name=value pairs
in the keyword argument list. For example: dict(one=1, two=2)
キーは、Python identifiers有効でなければならず、int値は有効ではありません。
>>> def f(**kw): pass
...
>>> f(one=1) # this is OK
>>> f(1=one) # this is not
File "<stdin>", line 1
SyntaxError: keyword can't be an expression
はしかし、あなたの他の二つの構文形式があります使用することができます。
制限は、あなたがそうのようにそれを示すことができる機能dict
だけではありません。
あり:
dict(iterable) -> new dictionary initialized as if via:
d = {}
for k, v in iterable:
d[k] = v
例:
dict(mapping) -> new dictionary initialized from a mapping object's
(key, value) pairs
例:
>>> dict({1:'one',2:2})
{1: 'one', 2: 2}
>>> dict([(1,'one'),(2,2)])
{1: 'one', 2: 2}
とマッピングから
>>> from collections import Counter
>>> Counter('aaaaabbbcdeffff')
Counter({'a': 5, 'f': 4, 'b': 3, 'c': 1, 'e': 1, 'd': 1})
>>> dict(Counter('aaaaabbbcdeffff'))
{'a': 5, 'c': 1, 'b': 3, 'e': 1, 'd': 1, 'f': 4}
:それは非常に(文字通りの辞書から辞書)のように見える
Counterと
defaultdictがマッピングされていることを念頭に置いておくと、これはあなたが辞書にそれらのいずれかをひそかだろうかあるないかもしれませんが