+ 3

Tuples comparison

This code is checking if tuple contains all items from another tuple. Is there any way to simplify this process? https://code.sololearn.com/cndpSiGVDEa4/?ref=app

23rd Jun 2022, 6:49 PM
Sator Arepo🌾🌌
Sator Arepo🌾🌌 - avatar
7 Respostas
+ 5
x = (1, 'hello', 'spam') y = ('foo', 'spam', 1, 42, 'hello') print(all( i in y for i in x)) # all() function except all values to be True edit: Sator Arepo🌾🌌 also I found other way , you can print( set(x).issubset(y) ) hope it helps...
23rd Jun 2022, 7:32 PM
Jayakrishna 🇮🇳
+ 5
Make each tuple into a set and intersect and count? x = (1, 'hello', 'spam') # or just x = {1, 'hello', 'spam'} y = ('foo', 'spam', 1, 42, 'hello') # likewise sx = set(x) sy = set(y) print(len(sx & sy)) # edit: rest of if block But ofc this won't count repetitions in both, should you have a tuple like that
23rd Jun 2022, 6:55 PM
Korkunç el Gato
Korkunç el Gato - avatar
+ 3
Jayakrishna🇮🇳 That(.issubset()) to me is the single right aka best way of doing this I've seen so far. Can't even imagine what us beginners would do here without people like you. Most people who install the app don't even have an idea where the actual learning is. TY. (I had already upvoted it before your edit, so I had to say this)
24th Jun 2022, 1:53 AM
Korkunç el Gato
Korkunç el Gato - avatar
+ 2
Thank you 🙏. I know about all() method previously but I searched in document for additional ways I found it. That's the beauty of community that we can share as well as gain knowledge.. So Honestly I have to say to thank you all. Korkunç the Terrible Frusciante Sator Arepo🌾🌌 But dont prefer predefined methods but for large program it's a best way to them.
24th Jun 2022, 8:58 AM
Jayakrishna 🇮🇳
+ 1
Jayakrishna🇮🇳 wow that's great, I didn't know about this function
23rd Jun 2022, 7:36 PM
Sator Arepo🌾🌌
Sator Arepo🌾🌌 - avatar
0
Thanks Korkunç the Terrible Frusciante set is a good idea so i could use set(x) <= set(y)
23rd Jun 2022, 7:10 PM
Sator Arepo🌾🌌
Sator Arepo🌾🌌 - avatar
0
GIad if it works out for you. (edit: & takes care of sets not being equal) But what if you had "hello" in both more than once? So set doesn't work in that case, at least not like this.
23rd Jun 2022, 7:14 PM
Korkunç el Gato
Korkunç el Gato - avatar