"::"操作符号
sequenceDiagramDiagram[::-1]
反转整个序列。
sequence[::2]
取序列中的偶数索引元素。
sequence[1::2]
取序列中的奇数索引元素
Counter
c = Counter('abracadabra')
print(c['a']) # 5
print(c['z']) # 0 (doesn't raise KeyError for missing items)
c1 = Counter('abracadabra')
c2 = Counter('alacazam')
print(c1 + c2) # add two counters together
print(c1 - c2) # subtract (keeping only positive counts)
print(c1 & c2) # intersection: min(c1[x], c2[x])
print(c1 | c2) # union: max(c1[x], c2[x])
python中哈希表怎么用
创建
hashmap = {}
加/更新
hashmap[index] = value
取
value = my_hashmap["key"]
value = my_hashmap.get("key", "default")
删除
value = my_hashmap.pop("key", "default") # Removes and returns the value, or "default" if key doesn't exist
得到所有keys, values
keys = my_hashmap.keys()
values = my_hashmap.values()
key是否存在
if key in hashmap
value是否存在
if value in hashmap.values()
set怎么用
创建
my_set = set() # Empty set
my_set = {1, 2, 3} # Set with initial elements
my_set = set([1, 2, 3]) # Creating a set from a list
加元素
my_set.add(4) # Add a single element
my_set.update([5, 6, 7]) # Add multiple elements
删除
my_set.remove(3) # Removes 3, raises KeyError if not found
my_set.discard(3) # Removes 3 if present, no error if not found
item = my_set.pop() # Removes and returns an arbitrary element
my_set.clear() # Removes all elements
集合操作
set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1 | set2 # or set1.union(set2)
intersection_set = set1 & set2 # or set1.intersection(set2)
difference_set = set1 - set2 # or set1.difference(set2)
symmetric_difference_set = set1 ^ set2 # or
is_subset = set1 <= set2 # or set1.issubset(set2)
is_superset = set2 >= set1set1.symmetric_difference(set2)