“Python Standard Dictionary” Code-Antworten

Aus den Sammlungen importieren Sie Standarddict

>>> s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
>>> d = defaultdict(list)
>>> for k, v in s:
...     d[k].append(v)
...
>>> d.items()
[('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])]
Unusual Unicorn

Standard -Wörterbuchwert

>>> from collections import defaultdict
>>> d = {'foo': 123, 'bar': 456}
>>> d['baz']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'baz'
>>> d = defaultdict(lambda: -1, d)
>>> d['baz']
-1
Rich Rat

Python Standard Dictionary

# sample code
monthConversions = {
    1: "January",
    2: "February",
    3: "March",
    4: "April",
    5: "May",
    6: "June",
    7: "July",
    8: "August",
    9: "September",
    10: "October",
    11: "November",
    12: "December"
}
num_month = int(input("Enter number of month: "))
# with default prompt if keys not found (keys,default value)
print(monthConversions.get(num_month,"Invalid input")) 

>>Enter number of month: 13
>>Invalid Input
CARTAÑO, Kirk Cedrick S.

Ähnliche Antworten wie “Python Standard Dictionary”

Fragen ähnlich wie “Python Standard Dictionary”

Weitere verwandte Antworten zu “Python Standard Dictionary” auf Python

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen