Python Set Trick? #code #programming #python

preview_player
Показать описание
Python set trick? #code #programming #python
Рекомендации по теме
Комментарии
Автор


set_a^set_b

set_a.union(set_b)
set_a|set_b

set_a.intersection(set_b)
set_a&set_b

set_a.difference(set_b)
set_a-set_b

prithviraje
Автор

Want to have more fun? Copy and paste these into your Python script:

print(f'{set_a - set_b = }') # Exclusive to set a
print(f'{set_b - set_a = }') # Exclusive to set b
print(f'{set_a ^ set_b = }') # The difference in sets
print(f'{set_a & set_b = }') # Where the sets intersect
print(f'{set_a | set_b = }') # The sets combined

Don't forget to create:
set_a = {1, 2, 3}
set_b = {2, 3, 4}

Indently
Автор

That's called a symmetric difference operation that allows you to get the elements that are either in first_set or in second_set but not in both.

abdelmajidbadreddine
Автор

Nice tip. Actually I think it would be beneficial if you also highlight the 'symetric_difference' method of sets. The ^ operator is nice for fast scripting, but if you see that in production code, it might be hard to understand. Especially if you are new to python.

lukaswieg
Автор

This is why you should learn math prior to any programming. This is set theory XOR.

Infe
Автор

"Even after 3 years of working with Python I'm still learning new things"
3 years is not a lot. At all.

lepe
Автор

the ^ works like XOR for example here if you put it in a variable like c and xor c and a you will get b and if you c^b you will get a

parhamm
Автор

That's a symmetric difference and it uses the ^ because it's the XOR operator

AkivaB
Автор

Thanks for ur awesome tips!
i love ur videos specifically ur short videos, cuz u tell some useful tips that no one told us before ❤️
and what is ur pycharm theme?? did u download it?? or its new pycharm theme??

ItzPouriya
Автор

this is bitwise xor operator and depending on the object ( here sets / example of polymorphism) means what is in one that is not in the other : difference if it was 10 ^ 4 i think it must be 14 for x being 00001001 ^ = 0000 1110

xzex
Автор

Kindly explain this
A = [ [], [] ] * 2
A[1].append(1)

then A = [ [], [1], [], [1] ]

I don't understand how this works !!

ventacode
Автор

Wait you started 3 years ago? Same! And yet your so much better than me...

WedgeDev
Автор

Its like bitwise operators. Didn't know you can use it with objects, but it makes sense. Those are still bits. I would say, that it works like this: All numbers inside the obj gets combined to a big bit. With each number it gets longer by 32 bits. Then the operator checks and gives you the numbers back. I dont know, but you could check it by just simply printing the set var as a bit 😊

hedgehogfriend
Автор

set_a - set_b you will get elements appear in set_a but not appear in set_b

lamkhatinh
Автор

Amazing, can we also do the same for list, dict?

abdultheseekerofknowledge
Автор

Took me 2 rewatch before I figured out that you mean "unique' values rather than "difference"

JhourladEstrella
Автор

It only works when there are even number of occurances of an element like. This is because a^a=0 and this will not work if there are odd occurences eg.if there 2 occurs 3 times then it will still be included in the result

manishreddy
Автор

the ^ symbol is the XOR operator in python...

quitethecontrary
Автор

No offense but those are basics how can u learn it after 3 years

_a_
Автор

Hungarian notation is not pythonic, so replace set_a with a, and so on.

DrDeuteron
welcome to shbcf.ru