python 列表,元組,字典

2021-08-21 06:30:17 字數 2402 閱讀 4057

list = [('a','a'),('b','b'),('c','c')]

for x in list:

#輸出的是列表裡的每乙個元素

print(x)

for x ,y in list:

#輸出的是每個元組中的每個元素

print(x,y)

for x ,y in enumerate(list):

#輸出的是每個索引和索引對應的元素

print(x,y)

list1=['a','b','c']

list2=['d','e','f']

list3=list1+list2

print(list3)

list1.extend(list2)

print(list1)

list1=[['a'],['b'],['c']]

list2=[['d'],['e'],['f']]

print(list1)

list1 = range(1,101)

list2=

for x in list1:

if x % 2 ==1:

print(list2)#列表推導式list = [x for x in list1 if x%2==1]pront(list)
list= ['陳燁','陳瑞','張毅','衛莊','蓋聶']

#startswith :以...開頭

list1=[x for x in list if x.startswith('陳')]

print(list1)

#數字排序

#倒序list=[1,5,4,3,2,6]

list.reverse()

print(list)

#正序list.sorted()

#求和result = sum(list)

print(result)

#大小正序排列

list1=[5,4,7,3,2,89,6,32,69,36]

list2=sorted(list)

print(list2)

#大小倒序排列

list3=sorted(list1,reverse = true)

print(list3)

建立元組的兩種方式:如果元組在建立的時候沒有放入元素  那麼這個元組沒有意義

列表和元組的區別  列表可以增刪改查等操作,元組可以進行 查操作

tp = ()

tp = tuple()

tp1=('a','b','c','d')

if 'a' in tp1:

print('存在')

else :

print('不存在')

item = tp1[3]

print(item)

print(len(tp1))

tp2=('q','w','e','r')

print(tp1+tp2)

#宣告字典的了兩種方式

dict1 ={}

dic2 = dict()

dic3=

print(dic3)

print(dic3['fond'])

dic['age']=18

print(dic3['age'])

1.獲取下面的列表中的每乙個值

list = [(1,[2,3]),(4,[5,6,7]),(8,[9,10,11,12])]

list = [(1,[2,3]),(4,[5,6,7]),(8,[9,10,11,12])]

for x, y in list:

print(x)

for z in y:

print(z)

2.將下面的列表進行排序  排序順序為 正序 

list2 = [1,5,21,545,48,7486,]

list2 = [1,5,21,545,48,7486,]

list3 = sorted(list2)

print(list3)

3.輸出裡面所有的friend_name和friend_age

dic4 = ,,]}

}

print(dic4['info']['friend'])

for x in dic4['info']['friend']:

print(x['friend_name'],x['friend_age'])

python列表元組字典

1.列表的資料項不需要具有相同的型別 建立乙個列表,只要把逗號分隔的不同的資料項使用方括號括起來即可 list1 google runoob 1997 2000 print list 0 list 0 2.列表的增 刪 改 查 insert delete update query 增 list.in...

python 列表,元組,字典

python中,有3種內建的資料結構 列表 元組和字典。1.列表 list是處理一組有序專案的資料結構,即你可以在乙個列表中儲存乙個序列的專案。列表中的專案。列表中的專案應該包括在方括號中,這樣python就知道你是在指明乙個列表。一旦你建立了乙個列表,你就可以新增,刪除,或者是搜尋列表中的專案。由...

python 列表 元組 字典

序列是python中最基本的資料結構。序列中的每個元素都分配乙個數字 它的位置,或索引,第乙個索引是0,第二個索引是1,依此類推。list1 physics chemistry 1997,2000 list2 1,2,3,4,5 list3 a b c d 序號 方法解釋 1在列表末尾新增新的物件 ...