hug2wisdom / learnpython

note the points of learn python
0 stars 0 forks source link

tuple #8

Open hug2wisdom opened 4 years ago

hug2wisdom commented 4 years ago

a tuple with one item is constructed by following a value with a comma (it is not sufficient to enclose a single value in parentheses). Ugly, but effective.

当元祖中只有一个元素的时候,元素后面必须加 。比如:('have a comma', )

hug2wisdom commented 4 years ago
a = set('abracadabra')
b = set('alacazam')
print(a) # {'b', 'c', 'r', 'd', 'a'}
print(b) # {'l', 'c', 'm', 'z', 'a'} 

# letters in a but not in b
print(a - b) # {'b', 'r', 'd'} a 中有而 b 中没有 差集

# letters in a or b or both
print(a | b) # {'b', 'l', 'c', 'm', 'r', 'z', 'd', 'a'} 并集

# letters in both a and b
print(a & b) # {'c', 'a'} 交集

# letters in a or b but not both
print(a ^ b) # {'l', 'b', 'z', 'r', 'd', 'm'} a,b 都有的去掉,留下两者中不同的放置在一起

# result
{'b', 'c', 'r', 'd', 'a'}
{'l', 'c', 'm', 'z', 'a'}
{'b', 'r', 'd'}
{'b', 'l', 'c', 'm', 'r', 'z', 'd', 'a'}
{'c', 'a'}
{'l', 'b', 'z', 'r', 'd', 'm'}

image