一宵呛、列表
1.修改列表元素
通過下標獲取元素单匣,然后重新賦值:list[下標] = 新的值
注意:下標不能越界
示例:
names = ['jor', 'mark', 'jerry', 'AMD']
names[3] = 'GTX'
print(names)
names[-1] = 'AMD'
print(names)
結(jié)果:
['jor', 'mark', 'jerry', 'GTX']
['jor', 'mark', 'jerry', 'AMD']
2.列表的其他操作
- len(列表):獲取列表的長度(元素的個數(shù))
print(len([1, 2, 3, 4]))
結(jié)果:
4
- 列表1 + 列表2: 讓列表1和列表2的元素組合在一起產(chǎn)生一個新的列表
names_new = names + ['jjLin', 'toby']
print(names_new)
結(jié)果:
['jor', 'mark', 'jerry', 'AMD', 'jjLin', 'toby']
- 列表 * 數(shù)字:讓列表中的元素重復(fù)n次,然后產(chǎn)生一個新的列表
print([1, 2]*3)
結(jié)果:
[1, 2, 1, 2, 1, 2]
- in, not in 操作
元素 in 列表:判斷指定的元素是否在指定的列表中
result = '嚶嚶怪' in names_new
print(result)
結(jié)果:
False
3.獲取列表中的最大的元素和最小的元素
max(list)
min(list)
print(max([1, 3, 5, 2, 1]))
print(max(['a', 'hj', 'u', 'b', 'c']))
print(min(names_new))
結(jié)果:
5
u
AMD
4.count():獲取指定元素在列表中出現(xiàn)的次數(shù)
print(numbers.count(1))
結(jié)果:
2
5.extend():list.extend(序列) 將序列中的每一個元素添加到列表中
names_new.extend('王祖賢')
print(names_new)
names_new.extend(['王祖賢'])
print(names_new)
結(jié)果:
['jor', 'mark', 'jerry', 'AMD', 'jjLin', 'toby', '王', '祖', '賢']
['jor', 'mark', 'jerry', 'AMD', 'jjLin', 'toby', '王', '祖', '賢', '王祖賢']
6.index():list.index(元素)獲取第一個指定元素的下標
print(names_new.index('mark'))
結(jié)果:
1
7.reverse():list.reverse()反向列表中的元素(不會產(chǎn)生新的列表)
numbers_new = [1, 2, 3, 4]
print(numbers_new.reverse())
print(numbers_new)
結(jié)果:
None
[4, 3, 2, 1]
8.sort():list.sort():對列表元素進行排序(默認是從小到大-升序,reverse=True表示降序)
numbers_new.sort()
print(numbers_new)
numbers_new.sort(reverse=True)
print(numbers_new)
結(jié)果:
[1, 2, 3, 4]
[4, 3, 2, 1]
9.clear():list.cleat()清空列表
numbers_new.clear()
print(numbers_new)
結(jié)果:
[]
10.copy():list.copy() 將列表中的元素全部拷貝一份宝穗,返回一個新的列表
通過一個列表變量給另一個列表變量賦值的時候户秤,賦的是地址。兩個列表對元素進行操作時逮矛,會相互影響鸡号。通過copy()或者切片可以避免
names_copy = names.copy()
print(names_copy)
結(jié)果:
['jor', 'mark', 'jerry', 'AMD']
二、元組
- 元組就是不可變的列表,列表中除了可變的操作须鼎,其他的操作都適用于元組
- 元組值:
a.使用()將元素包括起來鲸伴,多個元素之間用','隔開,如:(1, 2)
b.元素的類型可以是任何的類型 - 增、刪晋控、改不能作用于元組
1.查(和列表的查一樣)
colors = ('紅', '橙', '藍', '綠', '青', '靛', '紫')
# 取單個元素
print(colors[1])
# 切片
print(colors[1:3])
# 遍歷
for color in colors:
print(color)
結(jié)果:
橙
('橙', '藍')
紅
橙
藍
綠
青
靛
紫
2.len()
print(len(colors))
print('-'*20)
結(jié)果:
7
3.in, not in
print('綠' in colors)
結(jié)果:
True
4.+和*
# 拼接
print((1, 2)+(3, 4))
# 重復(fù)
print((1, 3)*3)
結(jié)果:
(1, 2, 3, 4)
(1, 3, 1, 3, 1, 3)
5.max汞窗、min
print(max(colors))
print(min(colors))
結(jié)果:
靛
橙
6.元組補充
# 通過多個變量獲取元組中的元素(變量數(shù)和元素數(shù)相等)
names = ('name1', 'name2')
x, y = names
print(x)
print(y)
print('-'*20)
# *表達式:獲取不定元素
names = ('name1', 'name2', 'name3', 'name4')
first, middle, *last = names
print(first)
print(middle)
print(last)
結(jié)果:
name1
name2
--------------------
name1
name2
['name3', 'name4']
三、字典
- 字典是可變的(適用CRUD)赡译,但是是無序的(不可通過下標取值)
- 字典也是一種容器類型的數(shù)據(jù)類型(序列),存儲的數(shù)據(jù)是以鍵值對的形式出現(xiàn)的仲吏,即 字典的元素是鍵值對
- 鍵值對:鍵:值(key:value) 鍵值對中key是形式,值才是真正要存的內(nèi)容
- 鍵理論上可以是任何不可變的數(shù)據(jù)類型,但是實際開發(fā)時一般使用字符串作為鍵
- 鍵是唯一的
- 鍵是不可修改的
- 值:可以是任何數(shù)據(jù)類型
1.聲明一個字典
# a.創(chuàng)建一個字典變量
dict1 = {'鍵':'值', 10:1}
print(dict1, type(dict1))
print(dict1['鍵'], type(dict1['鍵']))
print(dict1[10], type(dict1[10]))
print('-'*20)
# 創(chuàng)建一個空字典
dict2 = {}
print(dict2, type(dict2))
print('-'*20)
# b.將其他類型的數(shù)據(jù)類型轉(zhuǎn)換成字典
dict3 = dict([['1', '2'], ['3', '4']])
print(dict3)
dict4 = dict([(1, 2), (3, 4)])
print(dict4)
print('-'*20)
結(jié)果:
{'鍵': '值', 10: 1} <class 'dict'>
值 <class 'str'>
1 <class 'int'>
--------------------
{} <class 'dict'>
--------------------
{'1': '2', '3': '4'}
{1: 2, 3: 4}
--------------------
2.字典的增刪改查
# a.查(獲取值)
# 通過key獲取value即通過鍵獲得值 ----- dict[key]
person = {'name': 'joe', 'age': 22, 'face':99}
print(person['name'], person['age'], person['face'])
print('-'*20)
# a.1.get():dict.get(key)
print(person.get('name'))
# get()方法獲取字典中不存在的值時裹唆,返回None
print(person.get('aaa'))
print('-'*20)
# key確定時誓斥,使用[]語法取值,不確定時许帐,用get()方法取值
# b.增加/修改元素
"""
通過key獲取字典元素劳坑,然后復(fù)制。當(dāng)key存在的時候成畦,就是修改元素的值距芬,然后不存在的時候就是給字典添加元素(鍵值對)。
"""
# 添加
person['height'] = 1.7
print(person)
print('-'*20)
# 修改
person['name'] = 'mark'
print(person)
print('-'*20)
# c.刪除:刪除的是鍵值對(根據(jù)key刪除鍵值對循帐,key不存在會報錯)
# c.1.del
del person['height']
print(person)
print('-'*20)
# c.2.pop():dict.pop(key)
# 返回值為value
name = person.pop('name')
print(person, name)
print('-'*20)
結(jié)果:
joe 22 99
--------------------
joe
None
--------------------
{'name': 'joe', 'age': 22, 'face': 99, 'height': 1.7}
--------------------
{'name': 'mark', 'age': 22, 'face': 99, 'height': 1.7}
--------------------
{'name': 'mark', 'age': 22, 'face': 99}
--------------------
{'age': 22, 'face': 99} mark
--------------------
name
stu_id
score
--------------------
dict_values(['joe', 11111, {'chinese': 80, 'english': 80}])
dict_items([('name', 'joe'), ('stu_id', 11111), ('score', {'chinese': 80, 'english': 80})])
--------------------
name joe
stu_id 11111
score {'chinese': 80, 'english': 80}
--------------------
name joe
stu_id 11111
score {'chinese': 80, 'english': 80}
--------------------
3.列表中有字典蔑穴,字典中有字典,字典中有列表
"""
聲明一個變量惧浴,作用是用來存儲一個班級的學(xué)生信息存和。其中學(xué)生的信息包括姓名、性別衷旅、年齡捐腿、電話
至少存三個學(xué)生信息
"""
clazz = {'joe':{'sex':'男', 'age':22, 'tel':13222222}, 'john':{'sex':'男', 'age':22, 'tel':1422222},
'jerry':{'sex':'男', 'age':22, 'tel':1222222}}
clazz2 = [{'name':'joe', 'sex':'男', 'age':22, 'tel':11222222}, {'name':'john', 'sex':'男', 'age':22, 'tel':12222222},
{'name':'jerry', 'sex':'男', 'age':22, 'tel':13222222}]
clazz3 = {
'name':'py1805',
'address':'19-1',
'students':[
{'name':'joe', 'sex':'男', 'age':22, 'tel':11222222},
{'name':'john', 'sex':'男', 'age':22, 'tel':12222222}
]
}
print(clazz)
print('-'*20)
print(clazz2)
print('-'*20)
結(jié)果:
{'joe': {'sex': '男', 'age': 22, 'tel': 13222222}, 'john': {'sex': '男', 'age': 22, 'tel': 1422222}, 'jerry': {'sex': '男', 'age': 22, 'tel': 1222222}}
--------------------
[{'name': 'joe', 'sex': '男', 'age': 22, 'tel': 11222222}, {'name': 'john', 'sex': '男', 'age': 22, 'tel': 12222222}, {'name': 'jerry', 'sex': '男', 'age': 22, 'tel': 13222222}]
--------------------
{'name': 'py1805', 'address': '19-1', 'students': [{'name': 'joe', 'sex': '男', 'age': 22, 'tel': 11222222}, {'name': 'john', 'sex': '男', 'age': 22, 'tel': 12222222}]}
4.其他操作
"""
a.fromkeys()
dict.fromkeys(seq, value):創(chuàng)建一個新的字典,序列中的值作為鍵柿顶,value作為值茄袖。
"""
dict5 = {'a':1, 'b':2}
dict5_fromkeys = dict.fromkeys(dict5,1)
print(dict5_fromkeys)
print('-'*20)
"""
b.in
key in dict:判斷字典中是否存在指定的key
"""
dog_dict = {'color':'red', 'age':3, 'type':'土狗', 'a':2}
print('color' in dog_dict)
print('red' in dog_dict)
print('-'*20)
"""
c.update():dict1.update(dict2):使用字典2的鍵值對去更新字典1的鍵值對,如果字典2的key不在字典1中,就添加改元素嘁锯,如果存在就更新這個key的value宪祥。
"""
dict5.update(dog_dict)
print(dict5)
print('-'*20)
結(jié)果:
{'a': 1, 'b': 1}
--------------------
True
False
--------------------
{'a': 2, 'b': 2, 'color': 'red', 'age': 3, 'type': '土狗'}
四、集合
集合也是一種容器類型的數(shù)據(jù)類型(序列),數(shù)據(jù)放在{}中家乘,多個元素之間用元素隔開,如{1, 2}
集合是無序的(不能通過索引取值)蝗羊,可變(可以增刪改), 元素不能重復(fù)
集合可以進行數(shù)學(xué)中集合的相關(guān)操作:判斷是否包含,求交集仁锯、并集耀找、差集、補集
1.聲明集合
# a.聲明一個變量业崖,賦一個集合值
set1 = {1, 2, 3, 2, 3}
print(set1, type(set1))
print('-'*20)
# 創(chuàng)建一個空的集合
set1 = set()
print(set1)
print('-'*20)
# b.將其它的數(shù)據(jù)轉(zhuǎn)換成集合
# 將其他數(shù)據(jù)轉(zhuǎn)換成集合自帶去重的功能
set2 = set('abc123cba321')
print(set2)
print('-'*20)
set3 = set([1, 3, 1, 1, 2])
print(set3)
print(list(set3))
print('-'*20)
結(jié)果:
{1, 2, 3} <class 'set'>
--------------------
set()
--------------------
{'c', 'a', '2', 'b', '1', '3'}
--------------------
{1, 2, 3}
[1, 2, 3]
--------------------
2.增刪改查
"""
a.查:遍歷
注意:集合沒有辦法單獨獲取某一個元素
"""
for num in set2:
print(num)
print('-'*20)
# b.增
# add():set.add(value)在指定的集合中添加指定的元素
set4 = {1, 2, 3}
set4.add(4)
print(set4)
# update():set1.update(set2) 作用和字典的update相似野芒。將set2中的元素添加到set1中,自動去重双炕。
set4.update({6, 8, 9})
print(set4)
print('-'*20)
# c.刪
# remove():set.remove()在指定的集合中刪除指定的元素
set4.remove(2)
print(set4)
# pop():set.pop()隨機刪一個元素
set4.pop()
print(set4)
print('-'*20)
結(jié)果:
c
a
2
b
1
3
--------------------
{1, 2, 3, 4}
{1, 2, 3, 4, 6, 8, 9}
--------------------
{1, 3, 4, 6, 8, 9}
{3, 4, 6, 8, 9}
--------------------
3.判斷是否包含
"""
集合1>=集合2 ---判斷集合1中是否包含集合2(判斷集合2中的所有元素都在集合1中)
集合1<=集合2 ---判斷集合2中是否包含集合1(判斷集合1中的所有元素都在集合2中)
"""
print({1, 2, 3, 4, 5}>={1, 5})
print({1, 2, 3, 4} <= {1, 2})
print('-'*20)
結(jié)果:
True
False
--------------------
4.數(shù)學(xué)的集合運算
# 求并集: set1 | set2
print({1, 2, 3} | {2, 3, 4, 5})
# 求交集: set1 & set2
print({1, 2, 3} & {2, 3, 4, 5})
# 求差集:set1 - set2
print({1, 2, 3} - {2, 3, 4, 5})
# 求補集:
print({1, 2, 3} ^ {2, 3, 4, 5})
結(jié)果:
{1, 2, 3, 4, 5}
{2, 3}
{1}
{1, 4, 5}
其他方法
# 清空集合
set4.clear()
print(set4, type(set4))
# len()
print(len(set4))
結(jié)果:
set() <class 'set'>
0