iT邦幫忙

2021 iThome 鐵人賽

DAY 8
0
自我挑戰組

我以為我今年休息但怎麼還是來報名了Python入門挑戰30天系列 第 8

D8 - 彭彭的課程#Python 集合、字典的基本運算 - Set、Dictionary

累累累
今天人生新成就達成
被調查局大哥詢問嚇爆我這個善良好公民

但應該是無大礙
我就去上班了XDD

好的今天老師要講的內容感覺很多喔 Set&Dictionary
link: https://www.youtube.com/watch?v=L3-KuGYhw78&list=PL-g0fdC5RMboYEyt6QS2iLb_1m7QcgfHk&index=6&t=188s

  • 集合 Set
    資料沒有順序性(差異:list有序,set無)
    判斷資料是否存在:in和not in運算符號
    交集& , 聯集|
    差集- , 反交集^
#集合
s={1,2,3}
print(9 in s)
print(9 not in s)

s1={1,2,3,4}
s2={3,4,5,6,7}
#交集(取兩個重複數值)
s3=s1&s2
print(s3)
#連集(取兩個所有資料且不重複)
s4=s1|s2
print(s4)

s1={1,2,3,4}
s2={3,4,5,6,7}

#差集
s5=s1-s2
print(s5)

#反交集
s6=s1^s2
print(s6)

#字串拆解成集合
s=set("hello")
print(s)

  • 字典 Dictionary
    字典的:key-value配對
#輸入key返回value
dic={"apple":"蘋果","bug":"蟲蟲"}
print(dic)
print(dic["apple"])

#判斷key是否存在
print("apple" in dic)

dic={"apple":"蘋果","bug":"蟲蟲"}

#刪除鍵值對(key-value pair): del key值
del dic["apple"]
print(dic)

# 利用列表資料產生字典
# dic={key:value for x in list}
dic={x:x* 2 for x in [3,4,5]}
print(dic)


上一篇
D7 - 彭彭的課程#Python 有序列表的基本運算 - List、Tuple
下一篇
D9 - 彭彭的課程#Python 流程控制:if 判斷式
系列文
我以為我今年休息但怎麼還是來報名了Python入門挑戰30天30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言