Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
+ 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🌾🌌
7 Antworten
+ 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
+ 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
+ 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