2つの同じキーが異なる要素を持つPython辞書を使用できますか?Python Dictionary with 2 Keys?
答えて
いいえ、1つのキーに2つの要素を追加できます。辞書= {'a'、[b、c]}。リストオブジェクトを使用して、辞書に複数の値を設定します。
私はそれを2回設定すると元のファイルに置き換わるものと思われます –
@AramKocharyan:yesですが、 'd ['a']。append( 'c')'を設定するのではなく、項目を追加/追加できます。 'dict.setdefault()'メソッドは便利です。また、[collections.defaultdict](http://docs.python.org/library/collections.html#collections.defaultdict)を見て、例えば 'defaultdict(list)'や 'defaultdict(set)'を使ってください。 (そして、グーグル - または "SO"を検索する - "python multidict"にも興味深い例があります) – Steven
必要であれば、あなたは同じように、「ペア」タプルのリストを作成できます。
thelist = [('foo', 'first_foo'), ('foo', 'second_foo'), ('bar', 'not_foo')]
その後、あなたはこの醜いハックでthelist['foo']
と同等のものを取得することができます。
for pair in thelist:
if pair[0] == 'foo':
print pair[1]
と取得
first_foo
second_foo
号
これを行うには通常の方法はdefaultdictである:
dd = defaultdict(list)
dd['Your mother\'s key'].append('A')
dd['Your mother\'s key'].append('B')
dd['Your mother\'s key'] #=> ['A', 'B']
これは非常に十分でない場合は、独自のクラスを作成することができます。
@eumiro:修正されました。 – Marcin
- 1. PHP Foreach with keys with Foreach with keys
- 2. AndroidアクセスFirebase with keys with spaces
- 3. ASP.Net OData with string keys
- 4. VBA Vlookup with Scripting Dictionary
- 5. python return dictionary from function
- 6. Django Sum Annotation with Foreign keys
- 7. AngularJS ng-repeat dictionary with array value
- 8. Python == Dictionary
- 9. French Dictionary on Python
- 10. Python Dictionary Function
- 11. Python dictionary to csv excel
- 12. python multithreading save dictionary result
- 13. Python Dictionary Comprehension
- 14. Python pickling dictionary EOFError
- 15. Dictionary in Python
- 16. python append dictionary to list
- 17. Python Send Keys関数Selenium
- 18. Gnuplot python with 2 list
- 19. Python 2 with Spark 2.0
- 20. python delete dict keys in list comprehension
- 21. python dictionary key error excel
- 22. Python write dictionary into excel
- 23. Pythonのdictionaryとordereddictの違い
- 24. VBA-excel dictionary
- 25. Hangman 2 with Inventor for Python
- 26. Python Errno 2 with Windows 10
- 27. Psycopg2 insert python dictionary in postgres database
- 28. yaml multi nestedとpython dictionary
- 29. Python Dictionary JSON Key、Val Extraction
- 30. python 2対python 3クラスwith __iter__
鍵を要求したらどうなりますか? –
さて、でも...なぜ? – MMM