Milind Sahu
Milind Sahu

Reputation: 27

Comparing contents in the given tuples

I've two python class 'tuples' and and i want to compare contents on both of them and get only which is only in 'deactivated' tuple for eg.,

deactivated = ((34, 'abcd'), (250, 'def'), (350, 'xyz'))
schedules = ((34, 'abcd'), (250, 'def'))
to_deactivate = ()

in here, i want to push (350, 'xyz') which is not in schedules to another variable to_deactivate.

I've looked into some of the solutions online but most of them are just comparing whether the tuples are same or not. Please help me out on this.

Upvotes: -1

Views: 48

Answers (3)

chrslg
chrslg

Reputation: 13346

Are you sure you those variables should be tuples?

Because tuples are immutable. In their essence, you are not supposed to add or remove things from them. It looks like those variables are intended to be collections to which you add or remove things. Which is not possible with tuples.

For example, sets, since you don't seem to bother about order, but do seem to be interested about what is or is not in them. That is what sets are for. Very quick to check whether something is in it or not (with the same easy in operator. But in is O(n) for lists or tuples, and is O(log(n)), and in practice even O(1) with sets).

So, that is just a remark, but I feel that those variables should have been sets. In which case, answer to your question would simply have been

to_deactivate =  deactivated - schedules

Even if you insist on keeping them as tuples, one compact answer is still to use sets

to_deactivate=tuple(set(deactivated)-set(schedules))

Note that this is faster than the compound tuple solution. (1.38 μs vs 1.59 μs on my computer. And the timing difference grows when the size of the input tuples grows)

Timings

Just to make my point about timing clear, here is how much times it takes to compute is with the "compound tuple" strategy (Andrej's answer)

enter image description here

Of course, curve fitting is not a complexity method evaluation, but, well, since instinct says that we can expect either O(n), O(nlog(n)) or O(n²) complexity, we can conclude that we are clearly on the O(n²) side here.

Where as with conversion to sets : enter image description here

Which looks as a straight line as it can get. So, clearly, on the O(n) side.

And, of course, put together, we have a match clearly in favor of sets.

enter image description here

And to reply to SUTerliakov's comment: not just for big values. Even at the very start, sets win. Just a zoom on previous graph.

enter image description here

And that is comparing set in their most defavorable usage: when we keep every thing in tuples, and then convert to set only for the operation. It would be even faster if variables were sets from the beginning. And in the question, there is really no indication that there is a reason from them not to be sets.

Upvotes: 2

Karthik
Karthik

Reputation: 1

You could use iteration to compare every element in one tuple to every element of the other tuple and get the the value of index for which there exist value only in the first tuple

deactivated = ((34, 'abcd'), (250, 'def'), (350, 'xyz'))
schedules = ((34, 'abcd'), (250, 'def'))
to_deactivate = ()
for i in range(len(deactivated)):
    if deactivated[i] not in schedules:
        to_deactivate += deactivated[i]
print(to_deactivate)

This should work fine.

Edit: Iterating only once.

Upvotes: -1

Andrej Kesely
Andrej Kesely

Reputation: 195468

Try:

deactivated = ((34, "abcd"), (250, "def"), (350, "xyz"))
schedules = ((34, "abcd"), (250, "def"))

to_deactivate = tuple(tpl for tpl in deactivated if tpl not in schedules)
print(to_deactivate)

Prints:

((350, 'xyz'),)

Upvotes: 0

Related Questions