Python学习Task04

列表、元组、字符串

列表

练习题

  1. 列表操作练习
  • 在列表的末尾增加元素15
lst = [2, 5, 6, 7, 8, 9, 2, 9, 9]
lst.append(15)
print(lst)
#[2, 5, 6, 7, 8, 9, 2, 9, 9, 15]
  • 在列表的中间位置插入元素20
lst=[2, 5, 6, 7, 8, 9, 2, 9, 9]
lst.insert(len(lst)//2,20)
print(lst)
#[2, 5, 6, 7, 8, 20, 9, 2, 9, 9, 15]

  • 将列表[2, 5, 6]合并到lst中
lst=[2, 5, 6, 7, 8, 9, 2, 9, 9]
lst.extend([2,5,6])
print(lst)
#[2, 5, 6, 7, 8, 9, 2, 9, 9, 2, 5, 6]
  • 移除列表中索引为3的元素
lst=[2, 5, 6, 7, 8, 9, 2, 9, 9]
lst.pop(3)
print(lst)
#[2, 5, 6, 8, 9, 2, 9, 9]
  • 翻转列表里的所有元素
lst=[2, 5, 6, 7, 8, 9, 2, 9, 9]
lst.reverse()
print(lst)
#[9, 9, 2, 9, 8, 7, 6, 5, 2]
  • 对列表里的元素进行排序,从小到大一次,从大到小一次
lst=[2, 5, 6, 7, 8, 9, 2, 9, 9]
lst.sort(reverse=True)
print(lst)
lst.sort(reverse=False)
print(lst)
#[9, 9, 9, 8, 7, 6, 5, 2, 2]
#[2, 2, 5, 6, 7, 8, 9, 9, 9]
  1. 修改列表
def DoubleNum(lst):
    for index, value in enumerate(lst):
        if isinstance(value, bool):
            pass
        elif isinstance(value, (int, float)):
            lst[index] *= 2
        elif isinstance(value, list):
            DoubleNum(value)
        else:
            pass


lst = [1, [4, 6], True]
DoubleNum(lst)
print(lst)
'''
结果为:
[2, [8, 12], True]
'''
  1. leetcode 852题 山脉数组的峰顶索引
# 山脉数组
def MountainList(lst):
    left = 0
    right = len(lst) - 1
    while left <= right:  # 二分法找顶峰索引
        mid = (left + right) // 2
        if lst[mid] > lst[mid + 1] and lst[mid] > lst[mid - 1]:  # 找到顶峰索引
            midToMin = mid
            midToMax = mid
            while midToMin > 0:
                if lst[midToMin] > lst[midToMin - 1]:
                    midToMin -= 1
                else:
                    return False
            while midToMax < len(lst) - 1:
                if lst[midToMax] > lst[midToMax + 1]:
                    midToMax += 1
                else:
                    return False
            return True  # 遍历结束,是山脉数组
        elif lst[mid] < lst[mid - 1]:
            right = mid - 1
        elif lst[mid] < lst[mid + 1]:
            left = mid + 1


lst1 = [1, 3, 4, 5, 3]
result = MountainList(lst1)
print(result)
lst2 = [1, 2, 4, 6, 4, 5]
result = MountainList(lst2)
print(result)
'''
输出结果:
True
False
'''

元组

练习题

  1. 元组概念
print((1, 2)*2) #(1, 2, 1, 2)  #
print((1, )*2) #(1, 1) #(1, )还是元组,对元组进行运算
print((1)*2) #2  #(1)是整型,直接进行了数值运算

  1. 拆包过程是什么?
    拆包过程是指利用一句赋值语句将元组当中每一个元素都赋值给一个变量。
a, b = 1, 2
  • 上述过程属于拆包吗?
    属于拆包,1, 2已经定义了一个元组,与左边结构相对应,可以算作解压元组。

  • 可迭代对象拆包时,怎么赋值给占位符?
    在可迭代对象拆包时,使用_(单个元素),*_(连续多个元素)进行占位。

字符串

练习题

  • 怎么批量替换字符串中的元素?
    replace()方法
b = "I think I like it"
print(b.replace('I','We'))
# We think We like it
  • 怎么把字符串按照空格进⾏拆分?
    split()方法
str5 = ' I Love LsgoGroup '
print(str5.strip().split())  # ['I', 'Love', 'LsgoGroup']
print(str5.strip().split('o'))  # ['I L', 've Lsg', 'Gr', 'up']

  • 怎么去除字符串⾸位的空格?
    strip()方法

  • 实现isdigit函数
    实现函数isdigit, 判断字符串里是否只包含数字0~9

def isdigit(string):
    for i in string:
        try:
            int(i)
        except ValueError:
            return False
    return True

-leetcode 5题 最长回文子串
给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。

class Solution:
   def longestPalindrome(self, s: str) -> str:
   	for i in range(len(s),0,-1):
   		s1=s[:i]
   		if s1==s1[::-1]:
   			print(s1)
   			break

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值