Python菜鸟入门:day18编程学习

写在前面:
此博客仅用于记录个人学习进度,学识浅薄,若有错误观点欢迎评论区指出。欢迎各位前来交流。(部分材料来源网络,若有侵权,立即删除)
传送门:

day01基础知识
day02知识分类
day03运算符
day04数字与字符串
day05列表
day06元组与字典
day07条件与循环
day08函数概念
day09数据结构
day10模块介绍
day11文件操作
day12编程学习
day13编程学习
day14编程学习
day15编程学习
day16编程学习
day17编程学习
day18编程学习

实例编程学习06

Python将列表中的指定位置的两个元素对调

对调第一个和第三个元素

def swapPositions(list, pos1, pos2):
     
    list[pos1], list[pos2] = list[pos2], list[pos1]
    return list
 
List = [23, 65, 19, 90]
pos1, pos2  = 1, 3
 
print(swapPositions(List, pos1-1, pos2-1))

def swapPositions(list, pos1, pos2):
     
    first_ele = list.pop(pos1)    
    second_ele = list.pop(pos2-1)
     
    list.insert(pos1, second_ele)  
    list.insert(pos2, first_ele)  
     
    return list
 
List = [23, 65, 19, 90]
pos1, pos2  = 1, 3
 
print(swapPositions(List, pos1-1, pos2-1))

def swapPositions(list, pos1, pos2):
 
    get = list[pos1], list[pos2]
       
    list[pos2], list[pos1] = get
       
    return list
 
List = [23, 65, 19, 90]
 
pos1, pos2  = 1, 3
print(swapPositions(List, pos1-1, pos2-1)

翻转列表

def Reverse(lst):
    return [ele for ele in reversed(lst)]
     
lst = [10, 11, 12, 13, 14, 15]
print(Reverse(lst))

输出结果:

在这里插入图片描述
或:

def Reverse(lst):
    lst.reverse()
    return lst
     
lst = [10, 11, 12, 13, 14, 15]
print(Reverse(lst))

输出结果:

在这里插入图片描述

扫描二维码关注公众号,回复: 11459166 查看本文章
def Reverse(lst):
    new_lst = lst[::-1]
    return new_lst
     
lst = [10, 11, 12, 13, 14, 15]
print(Reverse(lst))

输出结果:

在这里插入图片描述
或直接调用list列表的sort方法,设置reverse为True即可翻转列表:

li = [*range(10, 16)]
# 得到列表 li = [10, 11, 12, 13, 14, 15], * 为解包符号
print(li)

# 降序排列
li.sort(reverse = True)
print(li)
# 输出: [15, 14, 13, 12, 11, 10]

判断元素是否在列表中存在

from bisect import bisect_left  
 
# 初始化列表
test_list_set = [ 1, 6, 3, 5, 3, 4 ]
test_list_bisect = [ 1, 6, 3, 5, 3, 4 ]
 
print("查看 4 是否在列表中 ( 使用 set() + in) : ")
 
test_list_set = set(test_list_set)
if 4 in test_list_set :
    print ("存在")
 
print("查看 4 是否在列表中 ( 使用 sort() + bisect_left() ) : ")
 
test_list_bisect.sort()
if bisect_left(test_list_bisect, 4):
    print ("存在")

输出结果:

在这里插入图片描述

清空列表

zack = [6, 0, 4, 1]
print('清空前:', zack)  
 
RUNOOB.clear()
print('清空后:', zack)  

输出结果:
在这里插入图片描述

复制列表

def clone_runoob(li1):
    li_copy = li1[:]
    return li_copy
 
li1 = [4, 8, 2, 10, 15, 18]
li2 = clone_runoob(li1)
print("原始列表:", li1)
print("复制后列表:", li2)

输出结果:
在这里插入图片描述

def clone_runoob(li1):
    li_copy = []
    li_copy.extend(li1)
    return li_copy
 
li1 = [4, 8, 2, 10, 15, 18]
li2 = clone_runoob(li1)
print("原始列表:", li1)
print("复制后列表:", li2)

输出结果:
在这里插入图片描述

def clone_runoob(li1):
    li_copy = list(li1)
    return li_copy
 
li1 = [4, 8, 2, 10, 15, 18]
li2 = clone_runoob(li1)
print("原始列表:", li1)
print("复制后列表:", li2)

输出结果:
在这里插入图片描述

计算元素在列表中出现的次数

def countX(lst, x):
    count = 0
    for ele in lst:
        if (ele == x):
            count = count + 1
    return count
 
lst = [8, 6, 8, 10, 8, 20, 10, 8, 8]
x = 8
print(countX(lst, x))

def countX(lst, x):
    return lst.count(x)
 
lst = [8, 6, 8, 10, 8, 20, 10, 8, 8]
x = 8
print(countX(lst, x))

end

明天还有最后一天的实训,弄完实训就向爬虫进发,希望明天也能坚持下来吧,加油。

猜你喜欢

转载自blog.csdn.net/Q_U_A_R_T_E_R/article/details/107570012