# Find the ONE key in a dict with a certain property

{1: [], 2: [5], 3: [4], 4: [], 5: []} This is an example for the occurency of numbers after rolling 5 dices One can read it following: 2 ocurencies of 5 3 occurencies of 4 I am looking for an (again) ELEGANT way to find the one key > 2 with an not empty list and there the value. In this example I want go get the 4 because it is the only number, which occurs at least 3 times.

7/12/2020 10:04:44 AM

Oma Falk13 Answers

New AnswerOma Falk , here is code to demonstrate how this could be done. Instead of randint(), choices is used to get 5 numbers in a range. To count the generated dice values, a counter from collections module is used. The code is not put to maximum compactness, but for better readability. If necessary, this can be shortened. I hope i understood it right. from random import choices as ch from collections import Counter as co dices = ch(range(1,6),k=5) # using choices from random: range 1-6 (= 1-5) k= number of values print(dices) # for demo only res = co(dices) # uses a counter from collections out = [i for i in res.items() if i[1] >= 3] if out: print(f'{dict(out)} - dice {out[0][0]} occurs {out[0][1]} times') else: print('no value count >= 3 found')

Oma Falk I'm very junior to you & I should not ask such stupid question to you Will you show your attempt ?

After a very little not offical and cheating criticism of my last post: Edit: and now to answer @KIIbo Here is my attempt: v = None for i in range(3,6): if dicdice[i]: v=dicdice[i][0] break print(v)

Oma Falk, just a a short question. You mentioned in your post: "... the one key > 2 with ...." This could be read as "key greater then 2", or you just want to mention that "key 2 has the greatest value". As in the dict are 2 values that are greater than 2: 2:[5] and 3:[4]. From my understanding you are looking for the key, that has the greatest value ???

Oma Falk , using your latest code, i did the last line, that shows the same result as your code: # find one value with occurency 3+ dicdice= {i:j for i in range(3,6) for j in range(1,7) if dice.count(j) == i or None} print(dicdice) # code lothar: print({dice.count(i):i for i in set(dice) if dice.count(i) >= 3})

If I would want to get the values of the key, which should be greater than 3 of a dict, where the keys are the number of ocurencies, I would: for k,v in d.items(): if k > 2 and len(v) >0: print(v) But I am not sure, if it is elegant. Its more a kind of "solid". A for iteration with the keys and values of a dict. Its good readable (I guess), its possible to turn it in a function fast, if neeeded. Its the best crafted solid and maybe not 100% ugly codesolution, I can offer for this problem atm. But I guess, I know what you mean with the "elegant" codestyle. Its not often. But sometimes I rework some of my old code to more elegant statements. Good luck with your project.

Play about with this:- mydict = {1: [], 2: [5], 3: [4], 4: [], 5: []} print(*[b[0] for a, b in mydict.items() if b and a > 2])

Lothar i have 5 dice. So 1 or 0 value can occur 3 times or more. I am looking for that special value if existing. so key is 3 or greater (occurence) and value is the value I am looking for.

I found a better dictionary it now surely has 1/0 entries. dice =[ri(1,6) for i in range(5)] # find one value with occurency 3+ dicdice= {i:j for i in range(3,6) for j in range(1,7) if dice.count(j) == i or None}

A boring version: d = {1: [], 2: [5], 3: [4], 4: [], 5: []} res = None for i in d: if i < 3: continue if not res and d[i]: res = i elif res and d[i]: res = None break print(res) (Now will I find something more interesting?)

Oma Falk Is it satisfactory to you ? dicdice={1: [], 2: [5], 3: [4], 4: [], 5: []} for i in range(3,6): if not dicdice[i]==False and dicdice[i][0]>=3: print(dicdice[i]) break #you can use len(dicdice[i])==0 or not len(dicdice[i]) or dicdice[i]==0 at not dicdice[i]==False