Post Ahl1K7SiuIOFSOxKqm by IngaLovinde@embracing.space
 (DIR) More posts by IngaLovinde@embracing.space
 (DIR) Post #AhkiLhLj4CJFv4iqbw by futurebird@sauropods.win
       2024-05-10T15:36:13Z
       
       0 likes, 0 repeats
       
       I have a list of 3 python objects w/ 4 attributes. If exactly 2 are identical (not all 3, not none) I want to return False. mylist=[ob1, obj2, obj3]if len(set([x.attribute1 for x in mylist]))==2:    return Falseif len(set([x.attribute2 for x in mylist]))==2:    return Falseif len(set([x.attribute3 for x in mylist]))==2:    return Falseif len(set([x.attribute4 for x in mylist]))==2:    return Falsereturn Truefeels clunky. what am i missing? (can you guess what card game?)
       
 (DIR) Post #Ahkil6YGVDB3K3hbJw by sgillies@mastodon.social
       2024-05-10T15:40:45Z
       
       0 likes, 0 repeats
       
       @futurebird the name of the game is right there in your code isn't it?
       
 (DIR) Post #AhkisG8lFaar5XBVD6 by jmjm@mstdn.social
       2024-05-10T15:42:18Z
       
       0 likes, 0 repeats
       
       @futurebird if your object is hashable, len(set(objs)) == 2If it is not hashable, add your own hash or repr methods.len(set([repr(x) for x in objs])) == 2Or len(set([x.hash() for x in objs])) == 2
       
 (DIR) Post #AhkjAb20Jhm35plf28 by not2b@sfba.social
       2024-05-10T15:45:13Z
       
       0 likes, 0 repeats
       
       @futurebird It's Set, right? For every attribute (color, shape, shading, something else), the three cards either have to have the same value (so len(set) is 1) or different values (so len(set) is 3).I think your solution is reasonable.
       
 (DIR) Post #AhkjDIZRPSkUhthDpw by pkw@mastodon.sdf.org
       2024-05-10T15:45:47Z
       
       0 likes, 0 repeats
       
       @futurebird Does your code work?
       
 (DIR) Post #AhkjGGWHUXYDDNdISO by Alon@mastodon.social
       2024-05-10T15:46:12Z
       
       0 likes, 1 repeats
       
       @futurebird Can you transform the attributes from strings to numbers? Then three cards form a set iff they sum to 0 in (Z/3Z)^4.
       
 (DIR) Post #AhkjIJNpobFufxeqQK by faassen@fosstodon.org
       2024-05-10T15:47:16Z
       
       0 likes, 0 repeats
       
       @futurebirdYou could write a function "two_match" where you pass the list and an attribute name to do the check, and use getattr. So two_match(mylist, "attribute1")
       
 (DIR) Post #AhkjT9KXX5uWgLHhaK by errg@ioc.exchange
       2024-05-10T15:48:50Z
       
       0 likes, 0 repeats
       
       @futurebird How about:attributes = ['attribute1','attribute2','attribute3','attribute4']for attribute in attributes:        if len(set(getattr(x,attribute) for x in mylist)) == 2:            return False    return True
       
 (DIR) Post #AhkjYeSpCq9cgmmdLE by zkamvar@hachyderm.io
       2024-05-10T15:50:11Z
       
       0 likes, 0 repeats
       
       @futurebird I think you might be able to use `obj.__dict__.values()` to iterate over each of the attributes instead of calling them one by one.https://bobbyhadz.com/blog/python-iterate-over-object-attributes
       
 (DIR) Post #Ahkk8l7HwuIlkMvY7k by galchwyn@mastodon.uy
       2024-05-10T15:56:32Z
       
       0 likes, 0 repeats
       
       @futurebird a way to simplify is to store the attributes as strings in a list, and iterate over it with getattr.  Not very elegant, but should work
       
 (DIR) Post #AhkkWvlpzOp3jvQ5Im by UtilityNerd@dice.camp
       2024-05-10T16:00:30Z
       
       0 likes, 0 repeats
       
       @futurebird Assuming these objects are dicts:max([mylist.count(x) for x in mylist]) == 2list.count(x) returns the number of times x appears in the list, and it's perfectly happy to work with lists of dicts and compare their values. Without the max() call you get list of the number of times each dict appears in your list. The max == 2 gets you exactly 2 matches.
       
 (DIR) Post #Ahko3RYTeO7VRlMiPY by barrygoldman1@sauropods.win
       2024-05-10T16:40:29Z
       
       0 likes, 0 repeats
       
       @futurebird won't len(set([x.attribute1 for x in mylist]))==2tell you that exacty 2 objects are DIFFERENT for that attribute? (hence the set will have 2 items)
       
 (DIR) Post #AhksnNaj7SIkI6Zq2i by futurebird@sauropods.win
       2024-05-10T17:33:42Z
       
       0 likes, 0 repeats
       
       @Alon  I can do this. And it gives and excuse to talk about mods (even better) It’s possible there could be more attributes in a future variant of this game and this could be extendable.
       
 (DIR) Post #AhkvUQhvbp0gJebKgC by Alon@mastodon.social
       2024-05-10T18:03:50Z
       
       0 likes, 0 repeats
       
       @futurebird The cool thing is that this property is invariant under S_3 actions on the underlying set of alternatives, even if it is expressed in Z/3Z language. Now I'm trying to think if there's a reasonable 4-dimensional extension, in which four cards with four alternatives per attribute form a set iff they are coplanar in (Z/4Z)^n and no three are collinear.
       
 (DIR) Post #Ahl1K7SiuIOFSOxKqm by IngaLovinde@embracing.space
       2024-05-10T19:09:02Z
       
       0 likes, 0 repeats
       
       @futurebird consider this: (a^b)|(b^c)|(c^a) is zero iff all three numbers are equal. (a^b)*(b^c)*(c^a) is zero iff at least two numbers are equal (assuming there is no integer overflow).
       
 (DIR) Post #Ahl2hmSgjp47EGmneC by kechpaja@social.kechpaja.com
       2024-05-10T19:24:43Z
       
       0 likes, 0 repeats
       
       @futurebird "Clunky" isn't necessarily a bad thing, unless you want to be able to change the set of attributes.There's a saying among professional software developers: in order to debug code, you have to be twice as clever as the person who wrote it. Thus, if you write the cleverest code you can (let alone the cleverest code that you _and_ an entire thread's worth of geeks can come up with), you are by definition unqualified to debug it and will likely regret the decision.IMO if it works after thorough testing, you should keep it the way it is.
       
 (DIR) Post #AhlhvxbcCL1zxyqF16 by perlscript@mastodon.social
       2024-05-11T03:06:42Z
       
       0 likes, 0 repeats
       
       @futurebird You have some solutions above, but are the classes of the objects such that you can either compare id(obj1) or add your own  __eq__ /__ne__ /etc. methods?I will admit, I was checking for which card game it actually was, didn't see answer a quick scan of the replies.
       
 (DIR) Post #Ahm5ijm4i14MCkxgoq by algebraicyclist@mathstodon.xyz
       2024-05-11T07:33:12Z
       
       0 likes, 0 repeats
       
       @futurebird you’re playing Set, right? sets are equivalent to lines in affine 4-space over \(\mathbb{F}_3\)I think last time I thought about generalizations, I concluded that adding more attributes would preserve this formalism but trying to increase the number of values of each attribute (like, 5 colors instead of 3) wouldn’t. but I don’t remember why I thought that, sorry!