day6 列表,元祖,字典,集合

01列表----- 增,刪,改,查

1.修改列表元素

通過下標獲取元素,然后重新賦值:列表名[下標] = 新的值

names = ['周星馳', '張家輝', '劉德華', '黃渤', '楊洋', '張家輝']
names[4] = '陳奕迅'
#names[-1]
print(names)

2.列表的其他操作

  • 1.len(列表):獲取列表的長度(元素的個數(shù))
  • 2.列表1+列表2:讓列表1和列表2的元素組合在一起產(chǎn)生一個新的列表
  • 3.列表*數(shù)字:讓列表中的元素重復(fù)N次,產(chǎn)生一個新的列表
# print(len([1,2,3,4,5]))
print(len(names))

new_names = names + ['周杰倫', '王力宏']
print(new_names)

print([1, 2] * 4)

4.in , not in操作

元素 in 列表:判斷指定的元素是否在指定的列表中

result = '高以翔' not in names
print(result)

3.獲取列表中最大的元素和最小的元素

max(列表)
min(列表)

print(max([1, 34, 67, 8]))
print(max(['a', 'hj', 'k', 'asd']))
print(min(1, 45, 89, 0, -1, 8))
# 獲取一個數(shù)字列表中的值
numbers = [1, 23, 56, 2, 445, 1]
max1 = numbers[0]
for item in numbers:
    if item > max1:
        max1 = item
print(max1)

4.其他方法

1.count:獲取指定元素在列表中出現(xiàn)的次數(shù)

print(numbers.count(-1))

2.extend(序列):將序列中的每一個元素,添加到列表中

names.extend(['王祖賢'])
print(names)

3.列表.index(元素):獲取指定元素在列表中的索引(如果元素有多個,取第一個)

print(names.index('張家輝'))

4.列表.reverse():反向列表中的元素(直接操作的原列表,不會產(chǎn)生新的列表)

numbers = [1, 2, 3, 4]
new_numbers = numbers.reverse()
print(numbers, new_numbers)

5.列表.元素.sort():對列表進行排序

numbers = [1, 2, 3, 9, 8, 7, 6, 5, 4]
numbers.sort() # 升序
print(numbers)
numbers.sort(reverse=True) # 降序
print(numbers)

6.列表.clear()

numbers.clear()
# numbers = []  效果同上
print(numbers)

7.列表.copy():將列表中的元素全部拷貝一份創(chuàng)建一個新的列表

names = ['張三', '李四']

# new_names1 = numbers.copy()
new_names1 = name[:]
print(new_names1)

注意:通過一個列表變量給另一個列表變量賦值的時候,賦的是地址的;兩個列表對元素進行操作的時候會相互影響。

想要避免這個問題就要使用copy或者切片

02元祖

1.什么是元祖
元祖就是不可變的列表,列表中除了可變的操作以外,其他的操作都說使用于元祖

元祖值:

  • a.使用()將元素包含起來,多個元素之間用逗號隔開,比如:(1,2,‘a(chǎn)bc’)
  • b.元素的類型可以是任何類型
  1. 增刪改,相關(guān)操作不能作用于元祖。查可以
colors = ('red', 'green', 'yellow', 'purple')

1.查(和列表的查一模一樣,沒有任何區(qū)別)

print(colors[1])
print(colors[0:3])
print(colors[0::2])
for item in colors:
    print(item)

2.len

print(len(colors))

3.in ,not in

print('red' in colors)

4 +和*

print((1, 2) + (3, 4))
print((1, 2) * 2)

5.元祖補充:

  • 1.獲取元祖的元素
names = ('name1', 'name2', 'name3')
x, y, z = names  # 通過多個變量分別獲取元祖的元素(變量個數(shù)和元素個數(shù)一樣)
print(x, y)

names = ('name1', 'name2', 'name2_2', 'name2_3', 'name3')
first, *midel, last = names  # 通過變量名前加*可以把變量變成列表,獲取多個元素
print(first, midel, last)

*name1, name = names
print(name1, name)

name, *name1 =names
print(name, name1)

03字典

  • 字典也是一種容器類型的數(shù)據(jù)類型(序列),存的數(shù)據(jù)是以鍵值對的形式出現(xiàn)的,字典中的元素全部都是鍵值對
    字典是可變的(可以增刪改),但是是無序的(不能使用下標)
  • 鍵值對:鍵:值(key:value):鍵值對中key是形式,值才是真正要存的內(nèi)容
  • 鍵:理論上可以是任何不可變的數(shù)據(jù)類型,但是實際開發(fā)的時候一般使用字符串作為key
  • 值:可以是任意數(shù)據(jù)類型

1.聲明一個字典

a.創(chuàng)建一個字典變量

dict1 = {} # 創(chuàng)建一個空的字典
print(type(dict1))

dict2 = {'a': 1, 'b': 'abc', 'c': 3 }
print(dict2)
dict2 = {'a': 1, 'b': 'abc', 'a': 3 }
print(dict2)

b.將其他數(shù)據(jù)類型轉(zhuǎn)換成字典

dict3 = dict([(1, 2), (3, 4)]) # 了解
print(dict3) # 列表的每個元素是元祖

2. 字典的增刪改查

a.查:獲取字典的元素的值
字典獲取元素的值是通過key來獲取的

字典[key]

person = {'name': '路飛', 'age': 17, 'face': 90}
print(person['name'], person['face'])
# print(person['aaa'])   如果key不存在,會報錯 KeyError

字典.get(key)

print(person.get('name'))
print(person.get('aaa'))   # 如果key不存在,返回None

注意:如果key值確定存在,使用[]語法去獲取值。不確定key值是否存在才使用get方法去獲取值

  • b.增加元素\修改元素

通過key獲取字典元素,然后賦值。當key存在的時候,就是修改元素的值;不存在的時候就是給字典添加鍵值對

person['height'] = 1.8
print(person)

person['age']= 25
print(person)

q.刪除:刪除的是鍵值對
del 字典[key]

print('------------------------------')
del person['face']
print(person['name'])
age = person.pop('age')
print(person, age)

3.相關(guān)的數(shù)組屬性

字典.pop(key) --->會返回
字典.key()獲取字典中所有的key,返回值的類型是dict_key,但是可以當做列表來用
字典.values()獲取字典中所有的值
字典.items()將字典中所有的鍵值對轉(zhuǎn)換成一個一個的遠足,key作為元祖的第一個元素,把value作為元祖的第二個元素

student_dict = {'name': ' 張三', 'study_id': 'py1805','scores': '96' }
keys = student_dict.keys()
print(keys, type(keys))
for key in keys:
    print(key)

print(student_dict.values())
print(student_dict.items())
print('-------------------------------------')

4.遍歷字典

a.直接遍歷字典獲取到的是所有的key(推薦使用)

for key in student_dict:
    print(key, student_dict[key])

b.(不推薦使用)

for key, value in student_dict.items():
    print(key, value)

5.列表中有字典,字典中有字典,字典中有列表

聲明一個變量,作用是用來存儲一個班級的學生的信息。其中包括姓名,性別,年齡,電話

class1 = [
    {'name': 'zs', 'age': '17', 'sex': '男', 'tel': '123456'},
    {'name': 'ls', 'age': '18', 'sex': '女', 'tel': '123000456'},
    {'name': 'ww', 'age': '19', 'sex': '男', 'tel': '123456000'}
]
class1 = {
    'name': 'py1805',
    'address': '19-1',
    'student': [
        {'name': 'as', 'age': 18},
        {'name': 'ls', 'age': 19},
        {'name': 'ww', 'age': 20}
    ]

}

6.其他操作

1.fromkeys()
dict.fromkeys(序列,value):創(chuàng)建一個新的字典,序列中的元素作為key,value作為值

new_dict = dict.fromkeys('abc', '100')
print(new_dict)
new_dict = dict.fromkeys(range(5), '100')
print(new_dict)
new_dict = dict.fromkeys(['abc', 'bbb', 'ccc'], '100')
print(new_dict)
  1. in
    key in 字典:判斷字典中是否存在指定的key
dog_dict = {'color': 'yellow', 'age': 2, 'type': '土狗'}
print('color'in dog_dict)   # 判斷的是鍵是否存在
print('yellow' in dog_dict)

3.update
字典1.update(字典2):使用字典2的鍵值對去更新字典1中的鍵值對。如果字典2中對應(yīng)鍵值對在字典1中不存在,就添加。存在就更新

print('-------------------------------')
dict1 = {'1': 'a', '2': 'b'}
dict1.update({'1': 'aaa', '3': 'bbb'})
print(dict1)

04 集合

集合也是一種容器類型的數(shù)據(jù)額類型(序列):數(shù)據(jù)放在{}中,多個之間用逗號隔開:{1,2,‘a(chǎn)’}
集合是無序的(不能通過索引去取值),可變(可以增刪改),元素不能重復(fù)

集合可以進行數(shù)學中集合相關(guān)的操作:判斷是否包含,求交集,并集,差集,補集

1,怎么聲明一個變量

  • a.聲明一個變量,賦一個集合值
set0 = set()  # 創(chuàng)建一個空的集合
set1 = {1, 2, 3, 2, 2}
print(set1, type(set1))
  • b.將其他的數(shù)據(jù)類型轉(zhuǎn)換成集合
set2 = set('abc12333hh')  # 將其他數(shù)據(jù)轉(zhuǎn)換成幾個,自帶一個去重的功能
print(set2)
set3 = set([12, 'abc', 'hh', 23, 56, 'abcdefg'])
print(set3)
print(list(set3))

2.增刪改查

a.查:遍歷
注意:集合沒有辦法單獨獲取某一個元素

for item in set2:
    print(item)

b.增 集合.add(元素):在指定的集合中添加指定的元素


set1 = {1, 2, 3}
set1.add(100)
print(set1)

集合1.update(集合2):將集合2中的元素添加到集合1中,自動去重

set1.update({'abc', 'ss', 1, 2})
print(set1)
  • c.刪

集合。remove(元素):在指定的集合中刪除指定的元素

set1.remove('ss')
print(set1)

pop 刪除是隨機刪除一個

set1.pop()
print(set1)

3.判斷是否包含

集合1 >= 集合2 ----判斷集合1中是否包含集合2
集合1 <= 集合2 ----判斷集合2中是否包含集合1

print({1, 2, 3, 4} >= {1, 4})  # True
print({1, 2, 3, 4} <= {1, 2})  # False

4.數(shù)學的集合運算

求并集:|

print({1, 2, 3} | {2, 3, 4, 5})

求交集: &

print({1, 2, 3} & {2, 3, 4, 5})

求差集: -

print({1, 2, 3} - {2, 3, 4, 5})

求補集: ^

print({1, 2, 3} ^ {2, 3, 4, 5})

5.其他方法

clear:清空集合

set1 = {1, 2, 3}
set1.clear()
print(set1, type(set1))

len:獲取集合中元素的個數(shù)

set1 = {1, 2, 3}
print(len(set1))

1.寫一個程序

'''
a.用一個變量來保存一個班級的學生信息(姓名,學號,成績(英語,體育,美術(shù)))
b.可以給這個班級添加學生
c.根據(jù)姓名刪除一個指定的學生信息
d.根據(jù)姓名刪除一個指定的學生信息
e.查看班級的所有學生信息
f.求指定的學生平均成績

'''

2 嘗試寫學生管理系統(tǒng)

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容