日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python列表添加元组_【Python爬虫】列表、元组、集合练习

發布時間:2025/4/16 python 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python列表添加元组_【Python爬虫】列表、元组、集合练习 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

列表:

pop() 函數用于移除列表中的一個元素(默認最后一個元素),并且返回該元素的值。

list.append(obj) 在列表末尾添加新的對象

list.count(obj) 統計某個元素在列表中出現的次數

list.extend(seq) 在列表末尾一次性追加另一個序列中的多個值(用新列表擴展原來的列表)

list.index(obj) 從列表中找出某個值第一個匹配項的索引位置

list.insert(index, obj) 將對象插入列表

list.pop(obj=list[-1])移除列表中的一個元素(默認最后一個元素),并且返回該元素的值

list.remove(obj) 移除列表中某個值的第一個匹配項

list.reverse() 反向列表中元素

list.sort([func]) 對原列表進行排序

#//1. range(x,y) 產生一個包含x,不包含y的序列。

#//2. 直接將elements賦值為range(0,6),elements是range(0,6),而不是數值

#//3. # 列表方法

List=[1,2,3,1]

List.append(4) #向列表末尾添加新的對象

print(List.count(1)) #用于統計某個元素在列表中出現的次數

List.extend([5,6,7]) #用于向一個列表之后添加另一個序列。

print(List.index(2)) #從列表中查找出某個值第一個匹配項的位置

List.insert(3,'insert') #向列表指定位置添加一個元素

List.pop()#移除列表的一個元素,并且返回該元素的值。如果給了參數,則刪除該位置的元素

print(List)

List.reverse() #將列表元素反向存放

print(List)

List.remove(1) #移除列表中某個值得第一個匹配項

print(List)

List=[6,5,7,9,4]

List.sort() #將列表元素進行排序

print(type(List.sort())) #驗證List.sort()返回值為None

print(List)

2

1

[1, 2, 3, 'insert', 1, 4, 5, 6]

[6, 5, 4, 1, 'insert', 3, 2, 1]

[6, 5, 4, 'insert', 3, 2, 1]

[4, 5, 6, 7, 9]

元祖:

Python元組包含了以下內置函數

1、cmp(tuple1, tuple2):比較兩個元組元素。

2、len(tuple):計算元組元素個數。

3、max(tuple):返回元組中元素最大值。

4、min(tuple):返回元組中元素最小值。

5、tuple(seq):將列表轉換為元組。

count( ):統計某個元組段在整個元組中出現的次數

集合:

python 集合的添加有兩種常用方法,分別是add和update。

集合add方法:是把要傳入的元素做為一個整個添加到集合中。

集合update方法:是把要傳入的元素拆分,做為個體傳入到集合中。

集合刪除操作方法:remove。

習題:

一、定義列表:list1 = ['life','is','short'],

list2 = ['you','need','python']

完成以下幾個要求:

1)輸出python及其下標

2)在list2后追加 '!' , 在 'short' 后添加 ','

3)將兩個字符串合并后,排序并輸出其長度

4)將 'python' 改為 'python3'

5)移除之前添加的 '!' 和 ','

1)輸出python及其下標

list1 = ['life','is','short']

list2 = ['you','need','python']

print(list2[2],list2.index('python'))

python 2

2)在list2后追加 '!' , 在 'short' 后添加 ','

list2.append('!')

print(list2)

list1.insert(3,',')

print(list1)

['you', 'need', 'python', '!']

['life', 'is', 'short', ',']

3)將兩個字符串合并后,排序并輸出其長度

list1.extend(list2)

print(list1,len(list1))

['life', 'is', 'short', ',', 'you', 'need', 'python', '!'] 8

4)將 'python' 改為 'python3'

list2.remove('python')

print(list2)

list2.insert(2,'python3')

print(list2)

['you', 'need', '!']

['you', 'need', 'python3', '!']

5)移除之前添加的 '!' 和 ','

list1.remove(',')

print(list1)

list2.remove('!')

print(list2)

['life', 'is', 'short']

['you', 'need', 'python'

二、自己定義元組并練習常用方法(輸出元組長度、指定位置元素等)

tup1=(1,2.3,7,12,54)

list2 = ['you','need','python']

tup2=tuple(list2)

print(tup2)

print(len(tup1))

print(max(tup1))

print(min(tup1))

print(tup1[0:2])

del tup1

('you', 'need', 'python')

5

54

1

(1, 2.3)

三、定義列表:

list1 = ['life','is','short'],

list2 = ['you','need','python']

list3 = [1,2,3,4,5,3,4,2,1,5,7,9]

完成以下操作:

1)構造集合 list_set1

2)將list1和list2合并構造集合 list_set2

3)輸出兩個集合的長度

4)將兩個集合list_set1、list_set2合并后移除 'python'

5)在合并后的新列表中添加 'python3'

1)構造集合 list_set1

list_set1=set(list1)

print(list_set1)

{'is', 'life', 'short'}

2)將list1和list2合并構造集合 list_set2

list_set2=set(list1+list2)

print(list_set2)

{'is', 'short', 'python', 'you', 'life', 'need'}

3)輸出兩個集合的長度

print(len(list_set1))

print(len(list_set2))

3

6

4)將兩個集合list_set1、list_set2合并后移除 'python'

list_set1.update(list_set2)

print(list_set1)

list_set1.remove('python')

print(list_set1)

{'is', 'short', 'python', 'you', 'life', 'need'}

{'is', 'short', 'you', 'life', 'need'}

5)在合并后的新列表中添加 'python3'

list_set1.add('python3')

print(list_set1)

{'is', 'short', 'you', 'life', 'need', 'python3'}

總結

以上是生活随笔為你收集整理的python列表添加元组_【Python爬虫】列表、元组、集合练习的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。