IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Python知识库 -> 学习Python第六天 -> 正文阅读

[Python知识库]学习Python第六天

学习Python第六天

切片

切片语法:[start : end : step]

开始的位置:结束的位置:步长

nums = [23, 24, 45, 67, 34, 56]
print(nums[2:])
# 对第二个到最后一个元素切片
print(nums[:])
# 全部切片,相当于遍历列表
print(nums[::-1])
# 反向全部切片
print(nums[1:3])
print(nums[2:7:2])
# 对第二个到第七个元素切片,步长为2
print(nums[10:19])
# 不会报错,但是为空
print(nums[5:1])
# 不会报错,但是为空

列表的相关知识点

  • 获取元素、元素个数
  • 遍历列表元素
  • 列表运算
  • 索引和切片(正向、负向索引,正向、负向切片)
  • 合并列表
  • 比较列表(比较第一个元素的第一个位置上的对应的二进制大小,例如:香蕉,苹果,就比较香和苹两个字对应的二进制大小)
list1 = ['apple', 'orange', 'pitaya', 'banana']
print(list1)
list2 = list(range(1, 10))
print(list2)
list3 = [i ** 2 for i in range(1, 10)]
# 获取列表元素个数
print(len(list1))
print(len(list3))
# 遍历列表元素
for i in range(len(list1)):
    print(list1[i])
for x in list1:
    print(x)
for i, x in enumerate(list1):#索引和对应元素一起遍历
    print(i, x)
# 和列表相关运算
# 重复运算
list4 = [1, 2, 3, 4] * 5
# 成员运算 ------>in / not in ------->True / False
print(3 in list4)
print(8 in list4)
print(10 not in list4)
print(1 not in list4)

# 索引和切片
# 正向索引:0 ~ N - 1 / 负向索引:-N ~ -1

print(list1[1])
print(list3[::-1])

# 合并运算
list5 = [1, 3, 5, 7]
list6 = [2, 4, 6, 8]
temp = list5 + list6
list5 = list5 + list6

# 比较
list7 = list(range(2, 10, 2))
list8 = [1, 8, 2]
# 比较两个列表的元素是否意义对应相等
print(list6 == list7)
print(list5 != list8)

显示汉字

显示所有汉字汉字的编码范围:0x4e00 ~ 0x9fa5ord()函数----->查看字符对应的编码chr()函数----->将编码处理成对应的字符

for i in range(0x4e00, 0x9fa5):
    # 汉字二进制范围
    print(chr(i), end='')

print(hex(ord('学')), hex(ord('习')))
查看编码

列表的操作

  • 添加元素

  • 删除元素

  • 清空元素

    itime = ['banana', 'apple', 'orange', 'apple' 'pitaya', 'watermelon', 'blueberry']
    if 'apple' in itime:# 判断apple是否在列表中输出Ture or Flase
        print(itime.index('apple'))
    # 添加元素
    itime.insert(1, 'waxberry')
    # 删除元素
    itime.pop()
    itime.pop(2)
    del itime[0]
    itime.remove('apple')
    # 清空列表
    itime.clear()
    while 'apple' in itime:
        itime.remove('apple')
    
    

    列表排序

    items = ['banana', 'apple', 'orange', 'apple' 'pitaya', 'watermelon', 'blueberry']
    # 反转
    items = items[::-1]
    print(items)
    items.reverse()
    print(items)
    
    # 排序(可以修改revers参数控制升序或者降序)
    items = ['banana', 'apple', 'orange', 'apple' 'pitaya', 'watermelon', 'blueberry']
    items.sort(reverse=False)#降序
    print(items)
    items.sort(reverse=True)#降序
    print(items)
    
    ums = [2, 34, 35, 3, 545, 34]
    nums.sort(reverse=False)
    print(nums)
    nums.sort(reverse=True)
    print(nums)
    

    简单排序

    简单选择排序----每次从剩下的元素中选择最小值

    nums = [35, 12, 99, 58, 67, 42, 49, 31, 73]
    for i in range(len(nums) - 1):
        # 假设第一个元素就是最小值
        min_value, min_index = nums[i], i
        # 通过循环寻找有没有更小的值并记下他的位置
        for j in range(i + 1, len(nums)):
            if nums[j] < min_value:
                min_value, min_index = nums[j], j
                # 将最小的值换到前面的位置
        nums[i], nums[min_index] = nums[min_index], nums[i]
    print(nums)
    

    冒泡排序

    冒泡排序:元素两两比较,如果前面的元素大于后面的元素,就交换两个元素的位置

    i位置和i+1位置两两比较大的放后面

    循环n-1次

    nums = [35, 12, 99, 58, 67, 42, 49, 31, 73]
    for i in range(1, len(nums)):
        swapped = False
        for j in range(0, len(nums) - i):#循环n-1ci
            if nums[j] > nums[j + i]:#比较大小
                nums[j], nums[j + i] = nums[j + i], nums[j]#把大的放在后面
                swapped = True
        if not swapped:#
            break
    print(nums)
    
    
    #特殊情况
    nums = [9, 2, 3, 4, 5, 6, 7, 8]
    for i in range(1, len(nums)):
        swapped = False
        for j in range(0, len(nums) - i):
            if nums[j] > nums[j + i]:
                nums[j], nums[j + i] = nums[j + i], nums[j]
                swapped = True
        if not swapped:
            break
    print(nums)
    
    
    
    nums = [2, 3, 4, 5, 6, 7, 8, 9, 1]
    for i in range(1, len(nums)):
        swapped = False
        for j in range(0, len(nums) - i):
            if nums[j] > nums[j - i]:
                nums[j], nums[j - i] = nums[j - i], nums[j]
                swapped = True
        if not swapped:#当swapped不再改变是,即为False时结束
            break
    print(nums)
    
    

    随机抽取和乱序

    sample函数无放回抽样
    choices,函数可以对列表元素进行又放回抽样,(可以重复抽中)
    choice函数可以从列表中随机抽取一个元素

    import random
    names = ['1', '2', '3', '4', '5', '6', '7', '8']
    print(random.sample(names, k=5))
    print(random.choices(names, k=5))
    print(random.choice(names))
    # 乱序,随机打乱顺序
    random.shuffle(names)
    print(names)
    
    

    练习

15个男人和15个女人同时在一艘船上在海里。现在船坏了,需要将15个人扔下去,其他的15个人才能获救。,于是把15个男人和15个女人顺序打乱围成一个圆圈。。指定一个人为第一个。从一数到九。数到第九的那个人就被扔下海里。求与人的位置是什么,才能保证15个女人都在船上,数到酒之后,从头开始数

persons = [True] * 30
index, counter, number = 0, 0, 0
# 索引, 计数器,报数
while counter < 15:#用丢进海里的人数作为停止循环的钥匙
    if persons[index]:
        number += 1
        if number == 9:
            persons[index] = False#数到九被丢进海里
            counter += 1#被丢进海里的人数加一
            number = 0
            pass
    index += 1
    if index == 30:
        index = 0#当最后一个人报完数后,索引又从头开始
for person in persons:
    # if person:
    #     print('女', end='')
    # else:
    #     print('男', end='')
    # 三元条件运算----->if后面的表达式为True,取if前面的值, 否则取else后面的值
    print('女' if person else '男', end='')#相当于上面注释部分
    
    
    
    #简化版
person = [i for i in range(30)]#将30个人依次放入列表
for _ in range(15):
    person = person[9:0] + person[:8]#每隔9切片,将9前后的列表切片相加,前面的放在后面,形成一个新的列表再次以相同的方式切片,循环15次即可
for i in range(1, 31):
    print('女' if i in person else '男', end='')

课后作业

用一个列表保存54张扑克牌,洗牌,
按斗地主的发牌方式把牌发给三个玩家,
多的3张牌给第一个玩家(地主),
把每个玩家手上的牌显示出来

主要就是遍历列表、循环、和切片的内容

自行思考明天讲解

import random
porke_numbers = [i for i in range(1, 11)]
flower_porkes = ['J', 'Q', 'K']
porke_numbers += flower_porkes
print(porke_numbers)
porke_nums = []
color_porkes = ['红桃', '草花', '方块', '黑桃']
for color_porke in color_porkes:
    for porke_number in porke_numbers:
        porke_nums.append(color_porke + str(porke_number))
porke_nums.append('大王')
porke_nums.append('小王')
print(porke_nums)
random.shuffle(porke_nums)
print(porke_nums)
print(len(porke_nums))
# player1.append(porke_nums[51:54])
# print(player1)
player1 = []
player2 = []
player3 = []
for i in range(0, len(porke_nums) - 3, 3):
    player1.append(porke_nums[i])

    player2.append(porke_nums[i + 1])
    player3.append(porke_nums[i + 2])
player1 += porke_nums[52:54]

print(player1)
print(player2)
print(player3)

  Python知识库 最新文章
Python中String模块
【Python】 14-CVS文件操作
python的panda库读写文件
使用Nordic的nrf52840实现蓝牙DFU过程
【Python学习记录】numpy数组用法整理
Python学习笔记
python字符串和列表
python如何从txt文件中解析出有效的数据
Python编程从入门到实践自学/3.1-3.2
python变量
上一篇文章      下一篇文章      查看所有文章
加:2021-07-27 16:10:51  更:2021-07-27 16:11:02 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/8 0:31:25-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码