- set():Python内置函数,用于创建一个空的集合(set)或将可迭代对象(如列表、元组、字符串等)转换为集合。集合是一种无序的、包含唯一元素的数据结构。
(1)用add()向集合添加元素。
(2)常用于创建哈希表。s = set() print(s) # output:set() s.add(1) print(s) # output:{1} s.add(2) print(s) # output:{1, 2} s.add(1) print(s) # output:{1, 2} l = [0, 1, 2, 3, 3] sl = set(l) print(sl) # output:{0, 1, 2, 3}
- Counter():collections模块中的一个类,用于统计可迭代对象中各元素的出现次数,并将结果存储在一个字典中。这个字典以元素作为键,以出现次数作为值。
from collections import Counter str1 = 'apple' str2 = 'banana' list1 = list(str1) list2 = list(str2) print(list1) print(list2) print(Counter(list1)) print(Counter(list2)) # output: # ['a', 'p', 'p', 'l', 'e'] # ['b', 'a', 'n', 'a', 'n', 'a'] # Counter({'p': 2, 'a': 1, 'l': 1, 'e': 1}) # Counter({'a': 3, 'n': 2, 'b': 1})
- sorted():Python内置函数,用于对可迭代对象进行排序并返回一个新的已排序的列表。该函数可以按升序(默认)或降序对元素进行排序,并且适用于各种可迭代对象,如列表、元组、字符串等。
str1 = 'apple' str2 = 'banana' list1 = list(str1) list2 = list(str2) print(list1) print(list2) print(sorted(list1)) print(sorted(list2)) print(sorted(list1,reverse=True)) print(sorted(list2,reverse=True)) # output: # ['a', 'p', 'p', 'l', 'e'] # ['b', 'a', 'n', 'a', 'n', 'a'] # ['a', 'e', 'l', 'p', 'p'] # ['a', 'a', 'a', 'b', 'n', 'n'] # ['p', 'p', 'l', 'e', 'a'] # ['n', 'n', 'b', 'a', 'a', 'a']
- get():字典(dictionary)对象的方法之一,用于获取字典中指定键(key)的值。该方法可以安全地访问字典的键值对,即使键不存在也不会引发错误。
# dictionary.get(key, default) my_dict = {'apple': 3, 'banana': 5, 'cherry': 2} # 获取存在的键的值 apple_count = my_dict.get('apple') print(apple_count) # output: 3 # 获取不存在的键时,返回默认值 orange_count = my_dict.get('orange', 0) print(orange_count) # output: 0 # 不提供默认值,返回 None grape_count = my_dict.get('grape') print(grape_count) # output: None
- enumerate(): Python内置函数,用于在迭代中同时获取元素的索引(index)和元素的值(value)。这个函数返回一个枚举对象,其中包含了迭代对象的元素以及它们对应的索引。通常在for循环中使用。字典的items()方法类似
my_list = {'apple', 'banana', 'cherry'} for index, value in enumerate(my_list): print(index, value) # output: # 0 apple # 1 banana # 2 cherry my_dict = {'apple': 3, 'banana': 5, 'cherry': 2} for key, value in my_dict.items(): print(key, value) # output: # apple 3 # banana 5 # cherry 2
-
defaultdict():collections模块中的一个类,它是字典的一个变种,用于创建具有默认值的字典。与普通的字典不同,defaultdict允许在访问不存在的键时,自动创建该键并设置一个默认值,而不会引发KeyError异常。
from collections import defaultdict # 创建一个 defaultdict,指定默认值为 int 类型的 0 my_dict = defaultdict(int) # 访问不存在的键 'key1',将其创建并设置默认值为 0 my_dict['key1'] += 1 # 访问不存在的键 'key2',将其创建并设置默认值为 0 my_dict['key2'] += 1 # 访问已存在的键 'key1',增加其值 my_dict['key1'] += 1 print(my_dict) # output: defaultdict(<class 'int'>, {'key1': 2, 'key2': 1})
-
ord():Python内置函数,用于返回给定字符的Unicode码点(Unicode code point)。Unicode是一种国际字符编码标准,它为世界上几乎所有的字符分配了唯一的数字标识。
unicode_value = ord('A') print(unicode_value) # ouput: 65 unicode_value = ord('中') print(unicode_value) # ouput: 20013 unicode_value = ord('1') print(unicode_value) # ouput: 49
-
l = [ [] for i in range(5) ]:创建一个包含5个空列表的列表。
my_list1 = [ [] for i in range(5) ] print('list 1:', my_list1) # ouput: # list 1: [[], [], [], [], []] my_list2 = [ 0 for i in range(5) ] print('list 2:', my_list2) # ouput: # list 2: [0, 0, 0, 0, 0]
-
for i in range(5, 0, -1):反向迭代循环,从5开始递减到1。
(1)起始值:5,循环的起始值是 5。
(2)结束值:0,循环在到达或超过结束值时停止。
(3)步长:-1,循环在每次迭代时递减 1。for i in range(5, 0, -1): print(i) # output: # 5 # 4 # 3 # 2 # 1
-
//:运算符,通常用于执行整数除法操作,它返回除法结果的整数部分(去掉小数部分)。
print('4/3 =',4/3) print('4//3 =',4//3) # output: # 4/3 = 1.3333333333333333 # 4//3 = 1
【Python3 NoteBook】001 Arrays & Hashing
于 2023-09-20 13:45:16 首次发布