目錄
Ⅰ.in方法的使用
Ⅱ.字典的使用
Ⅲ.1MB 、KB、 B、 b(即bit)的轉換(必學)
Ⅳ.閏年or平年
Ⅴ.count和counter方法
1. count() 方法的使用場景
2. Counter 類的介紹
3. count() 與 Counter 的區別
4. Counter 的高級應用
5.Counter的另一種使用
Ⅵ.max和min的使用---可以直接連用max(max(lst))
Ⅶ.格式化字符串format
Ⅷ.itertools迭代器
應用實例:使用itertools求前綴和S(n)
Ⅸ.笛卡爾積
Ⅹ.無窮小和無窮大的表示方法
Ⅰ.in方法的使用
在 Python 中,in 是一個用于檢查某個元素是否存在于某個容器(如列表、字符串、元組、字典、集合等)中的關鍵字。
它的使用非常簡單且靈活,以下是 in 的常見用法和示例。
1. 在列表中使用 in
檢查某個元素是否在列表中:
PYTHON
my_list = [1, 2, 3, 4, 5]
print(3 in my_list) ?# 輸出:True
print(6 in my_list) ?# 輸出:False
2. 在字符串中使用 in
檢查某個子字符串是否在字符串中:
PYTHON
my_string = "hello world"
print("hello" in my_string) ?# 輸出:True
print("python" in my_string) ?# 輸出:False
3. 在元組中使用 in
檢查某個元素是否在元組中:
PYTHON
my_tuple = (1, 2, 3, 4, 5)
print(3 in my_tuple) ?# 輸出:True
print(6 in my_tuple) ?# 輸出:False
4. 在字典中使用 in
檢查某個鍵是否在字典中:
PYTHON
my_dict = {"name": "Alice", "age": 25}
print("name" in my_dict) ?# 輸出:True
print("city" in my_dict) ?# 輸出:False
注意:in 默認檢查字典的鍵,而不是值。如果需要檢查值,可以使用 in my_dict.values():PYTHON
print("Alice" in my_dict.values()) ?# 輸出:True
5. 在集合中使用 in
檢查某個元素是否在集合中:
PYTHON
my_set = {1, 2, 3, 4, 5}
print(3 in my_set) ?# 輸出:True
print(6 in my_set) ?# 輸出:False
6. 在范圍(range)中使用 in
檢查某個數字是否在 range 中:
PYTHON
my_range = range(1, 10)
print(5 in my_range) ?# 輸出:True
print(10 in my_range) ?# 輸出:False
7. in 的否定形式:not in
檢查某個元素是否不在容器中:
PYTHON
my_list = [1, 2, 3, 4, 5]
print(6 not in my_list) ?# 輸出:True
print(3 not in my_list) ?# 輸出:False
8. in 的使用場景
條件判斷:
PYTHON
if "hello" in my_string:print("Found!")
列表推導式:
PYTHON
filtered_list = [x for x in my_list if x in {2, 3, 5}]
Ⅱ.字典的使用
Ⅲ.1MB 、KB、 B、 b(即bit)的轉換(必學)
Ⅳ.閏年or平年
Ⅴ.count和counter方法
1. count()
方法的使用場景
count()
主要用于統計某個特定元素在字符串、列表或元組中出現的次數。
(1) count()
在字符串中的使用:
word = "banana"
print(word.count("a")) # 3
print(word.count("na")) # 2
解釋:
-
"a"
在"banana"
中出現 3 次。 -
"na"
作為子字符串出現 2 次。
(2) count()
在列表中的使用:
nums = [1, 2, 3, 4, 2, 2, 5]
print(nums.count(2)) # 3
解釋:
-
2
在列表中出現 3 次。
(3) count()
在元組中的使用:
tup = (1, 2, 2, 3, 4, 2)
print(tup.count(2)) # 3
2. Counter
類的介紹
Counter
是 collections
模塊中的一個計數器工具,適用于統計可迭代對象中每個元素的出現次數,返回一個類似字典的對象,其中鍵是元素,值是該元素出現的次數。
Counter
的基本使用:
from collections import Counter
?
word = "banana"
counter = Counter(word) # 統計字符出現次數
print(counter) ?
# 輸出:Counter({'a': 3, 'n': 2, 'b': 1})
解釋:
-
Counter
自動計算"banana"
中每個字符的出現次數,并返回Counter
類型對象。
Counter
在列表中的使用:
nums = [1, 2, 3, 2, 2, 4, 3, 1, 5]
counter = Counter(nums)
print(counter) ?
# 輸出:Counter({2: 3, 1: 2, 3: 2, 4: 1, 5: 1})
3. count()
與 Counter
的區別
特性 | count() | Counter |
---|---|---|
適用對象 | 字符串、列表、元組 | 任何可迭代對象 |
統計方式 | 僅能統計一個元素的出現次數 | 統計所有元素的出現次數 |
返回值類型 | 整數(某個元素的出現次數) | Counter字典(所有元素的統計結果) |
適合場景 | 需要統計單個元素的出現次數 | 需要統計所有元素的次數,并進行排序或其他分析 |
4. Counter
的高級應用
(1) 找出出現最多的 n
個元素 (most_common()
)
word = "banana"
counter = Counter(word)
print(counter.most_common(1)) ?
# 輸出:[('a', 3)],表示 'a' 出現了 3 次,且是最多的
(2) 統計列表中最常出現的前 n
個元素:
nums = [1, 2, 3, 2, 2, 4, 3, 1, 5]
counter = Counter(nums)
print(counter.most_common(2)) ?
# 輸出:[(2, 3), (1, 2)]
# 說明:元素 2 出現 3 次,元素 1 出現 2 次
(3) Counter
可以直接進行出現次數加減運算:
c1 = Counter("banana")
c2 = Counter("band")
print(c1 + c2) ?
# 輸出:Counter({'a': 4, 'n': 3, 'b': 2, 'd': 1})
解釋:
-
c1
統計"banana"
-
c2
統計"band"
-
相加后,統計結果合并。
5.Counter的另一種使用
》斜線為i-j 對應著y=-x----可以想一下線代矩陣里面的正對角線就是正斜線
反斜線為i+j 對應著y=x
其中r和l一開始都是默認的字典性