Ich habe ein kurzes Testprogramm zum "collections" verstehen:
from collections import Counter
from collections import defaultdict
led_pins_dict = {5: "red", 6: "yellow", 13: "green", 19: "White", 26: "red"}
led_pins_dict_added = [(1,"orange"), (2, "pink"), (16, "vioolet")]
led_pins = led_pins_dict.keys()
color = led_pins_dict.values()
print(color)
print(led_pins)
print(led_pins_dict_added)
def log_missing():
print("Key added")
return 0
def main():
try:
result = defaultdict(log_missing, led_pins_dict)
print("before" ,dict(result))
for pin, color in led_pins_dict_added :
result[pin] = color
print("after", dict(result))
print(Counter(color))
except KeyboardInterrupt:
pass
finally:
print("\nclose")
if __name__ == "__main__":
main()
Display More
out:
%Run collections_example.py
dict_values(['red', 'yellow', 'green', 'White', 'red'])
dict_keys([5, 6, 13, 19, 26])
[(1, 'orange'), (2, 'pink'), (16, 'vioolet')]
before {5: 'red', 6: 'yellow', 13: 'green', 19: 'White', 26: 'red'}
after {5: 'red', 6: 'yellow', 13: 'green', 19: 'White', 26: 'red', 1: 'orange', 2: 'pink', 16: 'vioolet'}
Counter({'o': 2, 'v': 1, 'i': 1, 'l': 1, 'e': 1, 't': 1})
close
after result o.k., aber kein log_missing
from collections import Counter
from collections import defaultdict
led_pins_dict = {5: "red", 6: "yellow", 13: "green", 19: "White", 26: "red"}
led_pins_dict_added = [(1,"orange"), (2, "pink"), (16, "vioolet")]
led_pins = led_pins_dict.keys()
color = led_pins_dict.values()
print(color)
print(led_pins)
print(led_pins_dict_added)
def log_missing():
print("Key added")
return 0
def main():
try:
result = defaultdict(log_missing, led_pins_dict)
print("before" ,dict(result))
for pin, color in led_pins_dict_added :
result[pin]
print("after", dict(result))
print(Counter(color))
except KeyboardInterrupt:
pass
finally:
print("\nclose")
if __name__ == "__main__":
main()
Display More
out:
dict_values(['red', 'yellow', 'green', 'White', 'red'])
dict_keys([5, 6, 13, 19, 26])
[(1, 'orange'), (2, 'pink'), (16, 'vioolet')]
before {5: 'red', 6: 'yellow', 13: 'green', 19: 'White', 26: 'red'}
Key added
Key added
Key added
after {5: 'red', 6: 'yellow', 13: 'green', 19: 'White', 26: 'red', 1: 0, 2: 0, 16: 0}
Counter({'o': 2, 'v': 1, 'i': 1, 'l': 1, 'e': 1, 't': 1})
close
after nicht o.k. aber log_missing
Was mache ich hier falsch ??
Dank und Gruß DP