目录
31. 计算字符串中每个字符的频率
from collections import Counter
s = "hello world"
counter = Counter(s)
print(counter)
32. 检查两个字符串是否是字母异位词
from collections import Counter
s1 = "listen"
s2 = "silent"
if Counter(s1) == Counter(s2):
print("Anagrams")
else:
print("Not anagrams")
33. 移除列表中的重复元素
lst = [1, 2, 2, 3, 4, 4, 5]
unique_lst = list(set(lst))
print(unique_lst)
34. 列表排序
lst = [3, 1, 4, 2, 5]
lst.sort()
print(lst)
35. 创建一个简单的类
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person("John", 30)
print(p1.name, p1.age)