+ 5

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.

12th Jul 2020, 10:04 AM
Oma Falk
Oma Falk - avatar
12 Respuestas
+ 6
Oma 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')
12th Jul 2020, 1:42 PM
Lothar
Lothar - avatar
+ 6
Sven_m Auch nicht schlecht...
14th Jul 2020, 11:01 AM
Oma Falk
Oma Falk - avatar
+ 5
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)
12th Jul 2020, 10:07 AM
Oma Falk
Oma Falk - avatar
+ 4
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 ???
12th Jul 2020, 11:11 AM
Lothar
Lothar - avatar
+ 4
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})
12th Jul 2020, 12:38 PM
Lothar
Lothar - avatar
+ 3
Lothar looks gooooood!
12th Jul 2020, 3:01 PM
Oma Falk
Oma Falk - avatar
+ 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.
14th Jul 2020, 10:35 AM
Sven_m
Sven_m - avatar
+ 2
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])
12th Jul 2020, 11:01 AM
rodwynnejones
rodwynnejones - avatar
+ 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.
12th Jul 2020, 11:25 AM
Oma Falk
Oma Falk - avatar
+ 2
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}
12th Jul 2020, 11:35 AM
Oma Falk
Oma Falk - avatar
+ 2
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?)
12th Jul 2020, 12:30 PM
HonFu
HonFu - avatar
0
الوووو
31st Jul 2021, 5:52 AM
محمد الحميدي
محمد الحميدي - avatar