Page 1 of 1

Unterschied zwischen Wörterbuch und geordnetemDict

Posted: 22 May 2025, 22:40
by Anonymous
Ich versuche ein sortiertes Wörterbuch zu bekommen. Die Reihenfolge der Elemente zwischen MyDict und orddict scheint sich jedoch nicht zu ändern.

Code: Select all

from collections import OrderedDict

mydict = {'a': 1, 'b': 2, 'c': 3, 'd': 4}

orddict = OrderedDict(mydict)

print(mydict, orddict)

# print items in mydict:
print('mydict')
for k, v in mydict.items():
print(k, v)

print('ordereddict')
# print items in ordered dictionary
for k, v in orddict.items():
print(k, v)

# print the dictionary keys
# for key in mydict.keys():
#     print(key)

#  print the dictionary values
# for value in mydict.values():
#     print(value)