Pin-Jiun / Python

Python Document
0 stars 0 forks source link

3-set and dictionary #3

Open Pin-Jiun opened 1 year ago

Pin-Jiun commented 1 year ago
# 1. 集合的基本運算:
# 1.1 建立集合、使用 in 和 not in。
# 1.2 集合的交集、聯集、差集、反交集運算。
# 1.3 將字串的字母拆解成集合。

# 2. 字典的基本運算:
# 2.1 鍵值對 (key-value pair) 基本操作。
# 2.2 使用 in 和 not in 判斷 key 是否存在。
# 2.3 使用 del 刪除鍵值對。
# 2.4 以列表的資料為基礎,產生字典。

# 集合的運用
s1={3,4,5}

print(3 in s1)          #True
print(10 in s1)         #False
print(10 not in s1)     #True
#in/not in也可以用在[],()

s1={3,4,5}
s2={4,5,6,7}
#集合的操作只能用在集合
s3=s1&s2        # 交集︰取兩個集合中,相同的資料
print(s3)       #{4, 5}
s3=s1|s2        # 聯集︰取兩個合中的所有資料,但不重複取
print(s3)       #{3, 4, 5, 6, 7}   
s3=s1-s2        # 差集︰從 s1 中,減去和 s2 重疊的部分
print(s3)       #{3}
s3=s1^s2        # 反交集︰取兩個集合中,不重疊的部分
print(s3)       #{3, 6, 7}

# 把字串中的字母折拆成集合︰ set(字串)
s=set("Hello")  
print(s)        #{'l', 'e', 'o', 'H'}順序會亂跳
print('H' in s) #True
print('A' in s) #False

# 字典的運用︰key-value 配對,和集合一樣使用{}
dic={"apple":"蘋果","bug":"蟲蟲"}
print(dic["apple"])                             #蘋果
dic["apple"]="小蘋果"
print(dic["apple"])                             #小蘋果
dic={"apple":"蘋果","bug":"蟲蟲"}
test={1:50,60:66}

# 判斷 key 是否存在
print("apple" in dic)                           #True
print(66 in test)                               #False
print("test" in dic)                            #False
print("test" not in dic)                        #True
print(dic)                                      #{'apple': '蘋果', 'bug': '蟲蟲'}
del dic["apple"]                                # 刪除字典中的鍵值對 (key-value pair)
print(dic)                                      #{'bug': '蟲蟲'}

#dic={key:value for x in [list]} 
dic={x:x*2 for x in [3,4,5]}                    # 從列表的資料中產生字典
print(dic)                                      #{3: 6, 4: 8, 5: 10}
Pin-Jiun commented 1 year ago

當一個dict,我們想取出他的key,value資料項,可以使用下列方法進行迭代走訪dict.items()

tinydict = {'Google': 'www.google.com', 'Runoob': 'www.runoob.com', 'taobao': 'www.taobao.com'}

print("字典值 : %s" %  tinydict.items())

for key,values in  tinydict.items():
    print(key,values)
字典值 : dict_items([('Google', 'www.google.com'), ('Runoob', 'www.runoob.com'), ('taobao', 'www.taobao.com')])
Google www.google.com
Runoob www.runoob.com
taobao www.taobao.com
Pin-Jiun commented 8 months ago

1)初始化一個空集合

a =set()

a.add() # 可以添加元素

空集合添加元素

b = set() b.add(1) b

案例2:

set_test=set('hello') #此處應說明集合的“無序性” set_test {'l', 'o', 'e', 'h'}

2) 初始化一個字典

a = {} 默認為字典 ,不是集合

Pin-Jiun commented 8 months ago

在 Python 中,你可以使用 copy() 方法或 dict() 构造函数来复制字典。这两种方法有一些细微的区别:

使用 copy() 方法: python Copy code original_dict = {"a": 1, "b": 2, "c": 3} copied_dict = original_dict.copy()

修改原始字典,不影响复制的字典

original_dict["d"] = 4

print(original_dict) print(copied_dict) 这将输出:

css Copy code {'a': 1, 'b': 2, 'c': 3, 'd': 4} {'a': 1, 'b': 2, 'c': 3} 使用 dict() 构造函数: python Copy code original_dict = {"a": 1, "b": 2, "c": 3} copied_dict = dict(original_dict)

修改原始字典,不影响复制的字典

original_dict["d"] = 4

print(original_dict) print(copied_dict) 同样,这将输出相同的结果。

总的来说,这两种方法都可以用来复制字典,你可以根据个人偏好选择其中一种。如果你需要更深层次的复制(即复制字典中的嵌套对象),可以考虑使用 copy 模块的 deepcopy() 方法。