-
浮點數
字符串
整數查看全部 -
d = { 'Alice': [45], 'Bob': [60], 'Candy': [75], }
d["Alice"]=[45,56,89]
d["Bob"]=[85,76,84]
d["Candy"]=[75,86,99]
查看全部 -
例:已知兩個集合s1、s2,請判斷兩個集合是否有重合,如果有,請把重合的元素打印出來。s1 = set([1, 2, 3, 4, 5]),s2 = set([1, 2, 3, 4, 5, 6, 7, 8, 9])。
s1 = set([1, 2, 3, 4, 5])
s2 = set([1, 2, 3, 4, 5, 6, 7, 8, 9])
print(s1.isdisjoint(s2))
for item? in s1:
? ? if item? in? s2:
? ? ? ? print(item)
? ? else:
? ? ? ? continue
查看全部 -
d = { 'Alice': 45, 'Bob': 60, 'Candy': 75, 'David': 86, 'Ellena': 49 }
print(d.get("ALice"))查看全部 -
例:針對以下set,給定一個list,對于list里面的每個元素,如果set中包含這個元素,就將其刪除,否則添加到set里面去。L = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],S = set([1, 3, 5, 7, 9, 11])。
L = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
S = set([1, 3, 5, 7, 9])
for item in L:
? ?if item in S:
? ? ? ?S.remove(item)
? ?else:
? ? ? ?S.add(item)
print(S)查看全部 -
刪除元素兩種方法:
1.remove()方法。特別注意,刪除前要確認所刪內容是否在set里,否則會報錯。
2.discard( )方法(此方法不會報錯)
例:針對以下set,給定一個list,對于list里面的每個元素,如果set中包含這個元素,就將其刪除,否則添加到set里面去。L = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],S = set([1, 3, 5, 7, 9, 11])。
L = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
S = set([1, 3, 5, 7, 9])
for item in L:
? ?if item in S:
? ? ? ?S.remove(item)
? ?else:
? ? ? ?S.add(item)
print(S)查看全部 -
添加set元素:
添加單個元素:add()方法,參數為要添加的內容
批量添加元素:update()方法,參數為要批量添加的內容,可以是list形式。
查看全部 -
例:由于name_set不能識別小寫的名字,請改進name_set,是小寫的名字也能判斷在name_set里面。
解1:
names = ['Alice', 'Bob', 'Candy', 'David', 'Ellena']
name_set = str(set(names)).lower()
print('alice' in name_set)
解2:
names = ['Alice','alice', 'Bob', 'Candy', 'David', 'Ellena']
name_set = set(names)
name1 = 'bob'
if name1 in str(name_set).lower():
? ? print(True)
else:
? ? print(False)
查看全部 -
創建set的方式是使用set(),并傳入一個list。list的元素將會被轉換成set的元素。
有時,我們只想要 dict 的 key,不關心 key 對應的 value,目的就是保證這個集合的元素不會重復,這時,set就派上用場了。
查看全部 -
字符串format由兩個部分組成,字符串模板和模板數據內容組成,通過大括號{},就可以把模板數據內容嵌到字符串模板對應的位置。
# 字符串模板
template = 'Hello {}'
# 模板數據內容
world = 'World'
result = template.format(world)
print(result) # ==> Hello World如果模板中{}比較多,則容易錯亂,那么在format的時候也可以指定模板數據內容的順序。
# 指定順序
template = 'Hello {0}, Hello {1}, Hello {2}, Hello {3}.'
result = template.format('World', 'China', 'Beijing', 'imooc')
print(result) # ==> Hello World, Hello China, Hello Beijing, Hello imooc.# 調整順序
template = 'Hello {3}, Hello {2}, Hello {1}, Hello {0}.'
result = template.format('World', 'China', 'Beijing', 'imooc')
print(result) # ==> Hello imooc, Hello Beijing, Hello China, Hello World.# 指定{}的名字w,c,b,i
template = 'Hello {w}, Hello {c}, Hello , Hello {i}.'
world = 'World'
china = 'China'
beijing = 'Beijing'
imooc = 'imooc'
# 指定名字對應的模板數據內容
result = template.format(w = world, c = china, b = beijing, i = imooc)
print(result) # ==> Hello World, Hello China, Hello Beijing, Hello imooc.
查看全部 -
在字符串前面加個前綴r,表示這是一個?raw?字符串,里面的字符就不需要轉義了。
但是r'...'表示法不能表示多行字符串,也不能表示包含'和?"的字符串。
如果要表示多行字符串,可以用'''...'''表示
還可以在多行字符串前面添加r,把這個多行字符串也變成一個raw字符串:
r'''Python is created by "Guido".
It is free and easy to learn.
Let's start learn Python in imooc!'''
查看全部 -
字典的特點:
優點:字典的查找速度快,無論dict有10個元素還是10萬個元素,查找速度都一樣。而list的查找速度隨著元素增加而逐漸下降。
缺點:字典占用內存大,還會浪費很多內容,list正好相反,占用內存小,但是查找速度慢。
tuple可以作為字典的key
例:同學的近三次成績如下,請把每個同學的每次成績依次輸出。d = {'Alice': [50, 61, 66], 'Bob': [80, 61, 66], 'Candy': [88, 75, 90]}
? ? ? ?
d = {'Alice': [50, 61, 66], 'Bob': [80, 61, 66], 'Candy': [88, 75, 90]}
for key in d.keys():
????value=d[key]
????for score in value:
????????print(key,score)
查看全部 -
在Python中,二進制整數使用前綴0b表示查看全部
-
Python可以處理任意大小的整數。查看全部
-
刪除字典中的元素:(結合format的用法)
例:在dict中,使用keys()方法,可以返回dict的所有key,在刪除某個元素時,可以通過這個方法先判斷某個元素是否存在,請改造前面的程序,使得即使key不存在時,刪除也不會拋異常。
d = {
? ?'Alice': 45,
? ?'Bob': 60,
? ?'Candy': 75,
? ?'David': 86,
? ?'Ellena': 49
}
name = 'John'
if name in d.keys():
? ?d.pop(name)
else:
? ?print('{} not in d'.format(name))查看全部
舉報