Posts: 51
Threads: 26
Joined: Nov 2020
Jul282021, 11:03 PM
(This post was last modified: Jul292021, 09:47 AM by quest.)
Hello,
I have this dict:
od = OrderedDict([(0, 1), (1, 1), (3, 1), (7, 1), (36, 1)]) And I am plotting this array with the following line:
plt.bar(range(len(od)), list(od.values()), align='center')
plt.xticks(range(len(od)), list(od.keys()),rotation='vertical') IN this example for x axis, I have 0,1,3,7,36. However I don't have 2 between 1 and 3 and I don't have any other numbers between 7 and 36. So I want to update my dict like that:
od=([(0, 1), (1, 1),(2,0), (3, 1), (7, 1), (8,0),(9,0),(10,0),(11,0),...(36, 1)]) How can I do that?
I tried this way:
finalarray=np.zeros(60) # or sth larger than max (input dict keys)
for k,v in od:
print(k,v)
finalarray[k]=v And I got this error:
Error: TypeError: cannot unpack noniterable int object
Posts: 10,668
Threads: 400
Joined: Sep 2016
Jul292021, 01:59 AM
(This post was last modified: Jul292021, 02:00 AM by Larz60+.)
I think what you wanted was (line 1):
od = OrderedDict([(0, 1), (1, 1), (3, 1), (7, 1), (36, 1)]) and assuming this code is in same script:
finalarray=np.zeros(60) # or sth larger than max (input dict keys)
for k,v in od:
print(k,v)
finalarray[k]=v
Posts: 51
Threads: 26
Joined: Nov 2020
(Jul292021, 01:59 AM)Larz60+ Wrote: I think what you wanted was (line 1):
od = OrderedDict([(0, 1), (1, 1), (3, 1), (7, 1), (36, 1)]) and assuming this code is in same script:
finalarray=np.zeros(60) # or sth larger than max (input dict keys)
for k,v in od:
print(k,v)
finalarray[k]=v
Thanks for corrections but I still could not solve my problem
Posts: 51
Threads: 26
Joined: Nov 2020
okay, problem has been solved:
for k,v in list(od.items()):
print(k,v)
finalarray[k]=v
Posts: 1,666
Threads: 6
Joined: May 2017
I guess there is a better solution for matplotlib to fill the gaps.
A function which looks for the gaps and fill them:
def continious_keys(mapping, fillvalue=0):
last = None
for key, value in mapping.items():
if last is None:
yield key, value
last = key
else:
while last + 1 < key:
last += 1
yield last, fillvalue
yield key, value
last = key It's a generator, so you have to consume it. For example you can use a OrderedDict again.
from collections import OrderedDict
od = OrderedDict([(0, 1), (1, 1), (3, 1), (7, 1), (36, 1)])
generator = continious_keys(od)
new_od = OrderedDict(generator) # < consumes the generator, creates a new OrderedDict To make it better visible, I used as fillvalue=math.nan, which is not a number.
Example output from repl:
Output: OrderedDict([(0, 1),
(1, 1),
(2, nan),
(3, 1),
(4, nan),
(5, nan),
(6, nan),
(7, 1),
(8, nan),
(9, nan),
(10, nan),
(11, nan),
(12, nan),
(13, nan),
(14, nan),
(15, nan),
(16, nan),
(17, nan),
(18, nan),
(19, nan),
(20, nan),
(21, nan),
(22, nan),
(23, nan),
(24, nan),
(25, nan),
(26, nan),
(27, nan),
(28, nan),
(29, nan),
(30, nan),
(31, nan),
(32, nan),
(33, nan),
(34, nan),
(35, nan),
(36, 1)])
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
