0

How to check that tuple a contains all the elements of tuple B

11th Jan 2021, 9:23 AM
Vild
7 Respostas
+ 1
You do not specify clearly what "all elements" means. Does it include duplicates? If not, then Jan Markus has the right solution, if it does include duplicates then this will do the trick. print(all(i==j for i,j in zip(sorted(tA),sorted(tB)))) Edit: ^ forget this ^ Oma AKA Frogged Has a more elegant solution
11th Jan 2021, 11:19 AM
Louis
Louis - avatar
+ 2
print(all([True if i in tA else False for i in tB])) The above will print True if tupleA(tA) contains all elements of tB else False.
11th Jan 2021, 9:32 AM
Abhay
Abhay - avatar
+ 1
Louis nice!
11th Jan 2021, 11:21 AM
Oma Falk
Oma Falk - avatar
+ 1
sorted(ta) ==sorted(tb) i stood on the shoulders of Louis his answer is best.
11th Jan 2021, 11:26 AM
Oma Falk
Oma Falk - avatar
+ 1
What about the cases when TupleA has more elements than TupleB and still contains all the elements of TupleB? tA = (2,4,6,8,4,4,12,7,9) tB = (2,6,4,4,8) The working solution: print(all(tA.count(x)>=tB.count(x) for x in set(tB))) https://code.sololearn.com/cKuGc94bWpiU/?ref=app
14th Jan 2021, 9:25 PM
portpass
0
Can you write in code?
11th Jan 2021, 9:32 AM
Vild
0
Vild you mean?
11th Jan 2021, 9:38 AM
Abhay
Abhay - avatar