Posts: 6
Threads: 1
Joined: Nov 2019
Nov-27-2019, 07:06 AM
(This post was last modified: Nov-27-2019, 07:53 AM by buran.)
Hi all
just joined
I have a very annoying problem
I have a dictionary containing key value pairs that are all integers.
I want to check if some values equal a number, and if so, the value of each of these items will be changed with relation to its key.
and if no value matches the current number a new item is created
relevant piece of code:
for key, value in mydict.items():
if value == current:
value = value + 3 * key
if current not in mydict:
mydict[current:current * 2]
thank you all
this does not work
I need the correct synthax...
Posts: 1,950
Threads: 8
Joined: Jun 2018
If you want update value of the key then you should tell Python which key in which dictionary you would like to do so:
>>> d = dict(zip(range(1, 5), range(11, 15)))
>>> d
{1: 11, 2: 12, 3: 13, 4: 14}
>>> for k, v in d.items():
... if v == 13:
... d[k] = v + 3 * k
...
>>> d
{1: 11, 2: 12, 3: 22, 4: 14}
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 6
Threads: 1
Joined: Nov 2019
thank you for the fast reply ,
.but this is exactly my problem
how can I access the item through its value
or do I have to create a new list of keys holding any value
there must be a better way
Posts: 1,950
Threads: 8
Joined: Jun 2018
(Nov-27-2019, 09:38 AM)malevy Wrote: .but this is exactly my problem
how can I access the item through its value
Nope, I don't understand what exactly is your problem. Can you specify?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 8,155
Threads: 160
Joined: Sep 2016
Nov-27-2019, 09:50 AM
(This post was last modified: Nov-27-2019, 09:50 AM by buran.)
if you need to access by value - you can create a new dict where unique values are keys and value is a list of respective keys from original dict.
For one time you can just loop over the items (i.e. key, values), check the value and take actions you want.
If you are more specific - it could help to show sample original dict and expected output, with transformation explained
Posts: 8,155
Threads: 160
Joined: Sep 2016
looking at the first post
spam = {1:3, 4:6, 7:6, 9:10}
current = 6
if current in spam.values():
for key, value in spam.items():
spam[key] = value + 3 * key
else:
current *= 2
print(spam)
print(current) Output: {1: 6, 4: 18, 7: 27, 9: 37}
6
Posts: 6
Threads: 1
Joined: Nov 2019
Nov-27-2019, 10:05 AM
(This post was last modified: Nov-27-2019, 10:05 AM by malevy.)
maybe my question is actually this :
how can i get which keys hold this value
I need to fill the square brackets with the actual key
otherwise it will preform the function on all keys in d
am i right or do i not understand ?
Posts: 1,950
Threads: 8
Joined: Jun 2018
(Nov-27-2019, 10:05 AM)malevy Wrote: maybe my question is actually this :
how can i get which keys hold this value
This is to answer your question about 'how can I get which keys holds this value(s)':
>>> d = dict(zip(range(1, 5), range(11, 15)))
>>> values = [12, 14]
>>> [k for k, v in d.items() if v in values]
[2, 4]
>>> d.update({5: 12})
>>> d
{1: 11, 2: 12, 3: 13, 4: 14, 5: 12}
>>> value = 12
>>> [k for k, v in d.items() if v == value]
[2, 5]
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 8,155
Threads: 160
Joined: Sep 2016
looking at the first post
spam = {1:3, 4:6, 7:6, 9:10}
current = 6
if current in spam.values():
for key, value in spam.items():
if value = current:
spam[key] = value + 3 * key
else:
current *= 2
print(spam)
print(current)
Posts: 6
Threads: 1
Joined: Nov 2019
Nov-27-2019, 10:23 AM
(This post was last modified: Nov-27-2019, 10:25 AM by malevy.)
the keys are not ordered or following/
more spam like mess  **
|