+ 9

Why does this code not invert a dictionary?

https://code.sololearn.com/cA9A231A147a

22nd May 2021, 2:33 PM
Oma Falk
Oma Falk - avatar
16 Antworten
+ 2
Frogged I used my 2nd way to work with lambda inversedict = lambda x : dict([(v, [k for k, v1 in x.items() if v1 == v]) for v in set(x.values())]) print(inversedict({"a" : 1, "b" : 2, "c" : 2}))
22nd May 2021, 6:43 PM
A͢J
A͢J - avatar
22nd May 2021, 2:49 PM
Sebastian Keßler
Sebastian Keßler - avatar
+ 4
dict1 = {"a":1,"b":2,"c":2} wanted result {1:["a"],2:["b","c"]}
22nd May 2021, 2:57 PM
Oma Falk
Oma Falk - avatar
+ 4
Frogged I think I found shortest way: dict2 = {} for i in dict1: dict2.setdefault(dict1[i], []).append(i)
22nd May 2021, 5:03 PM
A͢J
A͢J - avatar
+ 4
Frogged I tried every possible way but I think append and extend doens't work in lambda expression that's why it is displaying None. I tried this way too but didn't work --------------- from collections import defaultdict inversedict = lambda x, y : {y[v].append(k) for k, v in x.items()} print(inversedict({"a":1,"b":2}, defaultdict(list))) ----------------------- But when I tried to add key and value of set then it worked. from collections import defaultdict inversedict = lambda x, y : {k + str(v) for k, v in x.items()} print(inversedict({"a":1,"b":2}, defaultdict(list))) It worked.
22nd May 2021, 7:01 PM
A͢J
A͢J - avatar
+ 3
I have try like this using loop https://code.sololearn.com/cyW0S7hPc9eV/?ref=app
22nd May 2021, 4:39 PM
A͢J
A͢J - avatar
+ 3
Zen Coding In real world actually 70% of football player had to answer in court. But on the lane there are different rules. My sports is oneliner. Anyway I appreciate the discussion about lambdas, especially when not to use. Readability is surely the most important dimension. Be sure...in a project i would not use that way unless I want to leave it.
14th Jul 2021, 5:16 AM
Oma Falk
Oma Falk - avatar
+ 2
🅰🅹 🅐🅝🅐🅝🅣 i need a lambda....you have an idea?
22nd May 2021, 5:57 PM
Oma Falk
Oma Falk - avatar
+ 1
Frogged Here's a simpler solution: k = lambda x: {a: [b for b in x if x[b] == a] for a in set(x.values())} # Hope this helps
24th May 2021, 12:19 PM
Calvin Thomas
Calvin Thomas - avatar
+ 1
Oma Falk May I ask, why you need a lambda expression? The solution you marked is nearly unreadable. Your future self will be delighted, when you find a readable solution. I can't think if a situation where it is impossible using a regular function declaration instead of a lambda.
14th Jul 2021, 4:05 AM
Zen Coding
Zen Coding - avatar
+ 1
Calvin Thomas the question was for Oma Falk. But I will reply to your comment: In my eyes this is very constructed and surely you can do it without lambda by just putting the logic before the print statement. Calling a lambda in the same line you define it would probably not go through the code QA in any serious Project. Probably you have other priorities for what is 'simple' (simple to read, simple to execute, simple in a sense of less to write). From my perspective the first is in nearly every case the most important. Only if I find that the performance of the code will not meet the target performance I would start to optimize in a direction where it doesn't get obvious to see what happens on first sight. But that nearly never ended in a lambda expression. The reason I ask is, that sometimes we code stuff because we are in the illusion of some fictional constraints which in reality do not exists and by taking a step back, we find that we can create an 'simpler' solution by letting go of the constraint.
14th Jul 2021, 4:45 AM
Zen Coding
Zen Coding - avatar
0
Frogged I am trying using lambda.
22nd May 2021, 6:11 PM
A͢J
A͢J - avatar
0
Zen Coding Assuming that your comment is based on my comment, I don't have any problem in reading it. In my opinion, it is pretty much self-explanatory. I could've defined a function like this: def k(x): {a: [b for b in x if x[b] == a] for a in set(x.values())} But the lambda version looks a bit more appealing to me.
14th Jul 2021, 4:16 AM
Calvin Thomas
Calvin Thomas - avatar
0
Zen Coding There are times when you'd need to use a lambda expression (yeah, expression) instead of a regular function. Suppose that you're asked to make a program that prints the factorial of the input number without the help of any external modules. How would you do it? Here's a possible solution using lambda: print((k:=lambda x: x * k(x - 1) if x else 1)(int(input()))) I don't think that this can be done without a lambda.
14th Jul 2021, 4:18 AM
Calvin Thomas
Calvin Thomas - avatar
0
Zen Coding The thing is that I like to make one-liners. It's a bit challenging, but fun too. Regarding the efficiency, I'm pretty sure that creating a function takes the same amount of time in either case. I don't care much about readability. Other answers had been posted here to help Oma Falk, so I had decided to try and make a one-liner with the list comprehension and all. Still, I find my code pretty self-explanatory.
14th Jul 2021, 6:22 AM
Calvin Thomas
Calvin Thomas - avatar
0
Zen Coding I don't think that it's possible to make a one-liner code that prints the factorial of the input number without using a lambda function. It's worth a try though.
14th Jul 2021, 6:28 AM
Calvin Thomas
Calvin Thomas - avatar