首页 > 编程语言 >Day4:Python基础:列表、元组的方法二

Day4:Python基础:列表、元组的方法二

时间:2022-10-19 18:22:42浏览次数:56  
标签:王小二 zhangsanfeng Python Day4 List 元组 Liu Xinyouyi Wang

1、列表按切片修改

List = ['Wang', [1,2,3,4], 'Liu', 'Xinyouyi', 'zhangsanfeng' ,'王小二']

List[0:2] = '狗屎'
print(List)
----------------------------------
['狗', '屎', 'Liu', 'Xinyouyi', 'zhangsanfeng', '王小二']


List = ['Wang', [1,2,3,4], 'Liu', 'Xinyouyi', 'zhangsanfeng' ,'王小二']

List[0:3] = '狗屎刘旺张'
print(List)
--------------------------------------------
['狗', '屎', '刘', '旺', '张', 'Xinyouyi', 'zhangsanfeng', '王小二']

2、列表循环查询

List = ['Wang', [1,2,3,4], 'Liu', 'Xinyouyi', 'zhangsanfeng' ,'王小二']

for Loop in List:
    print(Loop)
-----------------------

Wang
[1, 2, 3, 4]
Liu
Xinyouyi
zhangsanfeng
王小二

 

-----------------------------

 

 

List = ['Wang', [1,2,3,4], 'Liu', 'Xinyouyi', 'zhangsanfeng' ,'王小二']

print(List[0:3])
---------------------------
['Wang', [1, 2, 3, 4], 'Liu']

3、列表长度

List = ['Wang', [1,2,3,4], 'Liu', 'Xinyouyi', 'zhangsanfeng' ,'王小二']
L = len(List)
print(L)
-----------------
6

4、列表元素统计

List = ['Wang', [1,2,3,4], 'Liu','Liu','Liu','Xinyouyi', 'zhangsanfeng' ,'王小二']
L = List.count('Liu')
print(L)

 

标签:王小二,zhangsanfeng,Python,Day4,List,元组,Liu,Xinyouyi,Wang
From: https://www.cnblogs.com/leishu/p/16807308.html

相关文章