+ 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
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...
+ 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
+ 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)
+ 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.
+ 1
Jayakrishna🇮🇳 wow that's great, I didn't know about this function
0
Thanks Korkunç the Terrible Frusciante
set is a good idea so i could use
set(x) <= set(y)
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.