Leetcode python基础语法

"::"操作符号

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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值