Advertisement
ponczuTM

Untitled

Mar 19th, 2023 (edited)
597
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.54 KB | None | 0 0
  1. from collections import defaultdict
  2.  
  3. # stwórz słownik z domyślną wartością 0 dla nowych kluczy
  4. result = defaultdict(int)
  5.  
  6. # wczytaj dane z pliku "dane.txt"
  7. with open("dane.txt") as f:
  8.     for line in f:
  9.         # usuń białe znaki na początku i końcu linii, a następnie podziel linie na klucz i wartość
  10.         key, value = line.strip().strip("()").split(", ")
  11.         # dodaj wartość do słownika pod kluczem
  12.         result[key] += int(value)
  13.  
  14. # wyświetl wyniki
  15. for key, value in result.items():
  16.     print(f"({key}, {value})")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement