-5

Now I understand the data in these data structures has to be of type integer for this to be viable but how would it work?

Suppose I had a list of lists or set with tuples in it; what would the result of that look like and what would it mean logically?

list_a = [[1,34,24],[12,727,2]]
list_b =[[12,727,2]]

some_list = list_a & list_b
# what would the above list look like?

set_1 = {(2,3),(3,4),(4,5)}

set_2 = {(1,3),(2,5),(6,7),(1,0)}

some_set = set_1 | set2
# what would the above set look like?

Could I use logical operators on the resulting data structures?

if some_value in set1 | set2:
    # do something
mojojojo47
  • 13
  • 6
  • You cannot use logical operators on lists because the result is arbitrary. Furthermore, `list_a` and `list_b` are different sizes. What do you "want" the output of `some_list` to be? – Derek O Aug 26 '20 at 06:35
  • 4
    For whether, why and how certain operations work on certain data structures, consult the documentation: https://docs.python.org/3/library/stdtypes.html#set, https://docs.python.org/3/library/stdtypes.html#common-sequence-operations – deceze Aug 26 '20 at 06:35
  • The data doesn't need to be integers for this "to work"; in the case of a set, the data merely needs to be hashable so it can be part of a set, and then the `|` operation on the set does whatever it does (hint: set union). – deceze Aug 26 '20 at 06:39
  • I would want some_list to be a new list of lists where there are only values in list_a AND list_b. – mojojojo47 Aug 26 '20 at 06:41
  • Your examples would be better and easier with single items, not lists of list or sets of tuples. – 9769953 Aug 26 '20 at 06:44
  • @mojojojo47 sets work that way, with `__or__` overloaded to `set.union`. Lists do not. – Adam Smith Aug 26 '20 at 06:45
  • 2
    Also: why not just try this? Drop into a repl and type your code -- see what happens. – Adam Smith Aug 26 '20 at 06:46
  • `&` isn't defined for lists and you'll get an error when trying to use it. Whatever you *want* it to do, it won't do anything in practice… – deceze Aug 26 '20 at 06:46
  • Ok so what I want doesn’t work for list but it does work for sets? So, set1 | set2 is equivalent to set1.union(set2) – mojojojo47 Aug 26 '20 at 06:48
  • You can easily test it. It would be faster than asking. – Asocia Aug 26 '20 at 06:52

1 Answers1

0

Those aren't bitwise operators per se. They're operators, and each type can define for itself what it will do with them. The & and | operators map to the __and__ and __or__ methods of an object respectively. Sets define operations for these (intersection and union respectively), while lists do not. Lists define an operation for + though (list concatenation).

Sooo… set_1 | set_2 results in:

{(2, 3), (6, 7), (4, 5), (3, 4), (1, 0), (2, 5), (1, 3)}

As for the rest of the question: Mu.

deceze
  • 510,633
  • 85
  • 743
  • 889