Python Forum
what is wrong. Why python doesn't ad my values ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
what is wrong. Why python doesn't ad my values ?
#8
from collections import Counter

r1 = {'value 1': 13, 'value 2': 15, 'value 3': 9, 'value 4': 5}
r2 = {'value 3': 1, 'value 5': 12}

r = dict(sorted((Counter(r1) + Counter(r2)).most_common()))
print("Counter", r)

r = r1.copy()
for key, value in r2.items():
    r[key] = r.get(key, 0) + value
r = dict(sorted(r.items()))
print("Loop", r)
Output:
Counter {'value 1': 13, 'value 2': 15, 'value 3': 10, 'value 4': 5, 'value 5': 12} Loop {'value 1': 13, 'value 2': 15, 'value 3': 10, 'value 4': 5, 'value 5': 12}
Reply


Messages In This Thread
RE: what is wrong. Why python doesn't ad my values ? - by deanhystad - Apr-28-2022, 06:45 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,923 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,690 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  why doesn't python look in two directions Kakha 21 6,707 Jan-01-2021, 11:24 PM
Last Post: jefsummers
  Python IDE doesn't see opencv-python package on my Jetson Nano sadhaonnisa 1 3,377 Oct-11-2020, 01:04 AM
Last Post: Larz60+
  python gives wrong string length and wrong character thienson30 2 3,067 Oct-15-2019, 08:54 PM
Last Post: Gribouillis
  Python: command “python -m pip install --upgrade pip” doesn't work apollo 2 13,481 Sep-16-2019, 03:11 PM
Last Post: snippsat
  wrong values cybervulcan 3 2,948 Jun-17-2018, 08:32 PM
Last Post: cybervulcan

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020