Tuples comparison | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 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