列表框架
列表习题
1、列表操作练习
列表lst 内容如下
lst = [2, 5, 6, 7, 8, 9, 2, 9, 9]
请写程序完成下列操作:
- 在列表的末尾增加元素15
- 在列表的中间位置插入元素20
- 将列表[2, 5, 6]合并到lst中
- 移除列表中索引为3的元素
- 翻转列表里的所有元素
- 对列表里的元素进行排序,从小到大一次,从大到小一次
lst = [2,5,6,7,8,9,2,9,9]
lst.append(15)
lst.insert(20,4)
lst.extend([2,5,6])
lst.pop(3)
lst.reverse()
lst1=lst.sort(reverse=False)
lst2=lst.sort(reverse=True)
print(lst,lst1,lst2)
2、修改列表
问题描述:
lst = [1, [4, 6], True]
请将列表里所有数字修改成原来的两倍
lst= [1, [4, 6], True]
lst[0]=2
lst[1][0]=8
lst[1][1]=12
print (lst)
3、leetcode 852题 山脉数组的峰顶索引
class Solution:
def peakIndexInMountainArray(self, A):
"""
:type A: List[int]
:rtype: int
"""
return A.index(max(A))
元组框架
元组习题
print((1, 2)*2)
print((1, )*2)
print((1)*2)
print(type((1, 2)*2))
print(type((1, )*2))
print(type((1)*2))
#(1, 2, 1, 2)
#(1, 1)
#2
#<class 'tuple'>
#<class 'tuple'>
#<class 'int'>
单个元组元素后面必须加上逗号“,”才能表示单元素元组
2.拆包过程
拆包就相当于把元组中的元素拆分成单个的元素。
t = (1,2)
(a,b) = t
print(a,b)代码片
拆包后的元素我们不一定全部需要,对于不需要的元素可以用任意符号占位
可迭代对象
字符串框架
字符串习题
- 怎么批量替换字符串中的元素? replace函数
str5 = ' I Love LsgoGroup '
print(str5.strip().replace('I', 'We'))
# We Love LsgoGroup
- 怎么把字符串按照空格进⾏拆分? split(str="", num)
str5 = ' I Love LsgoGroup '
print(str5.strip().split()) # ['I', 'Love', 'LsgoGroup']
- 怎么去除字符串⾸位的空格? lstrip([chars]) 截掉字符串左边的空格或指定字符。
str5 = ' I Love LsgoGroup '
print(str5.lstrip()) # 'I Love LsgoGroup '
2、实现isdigit函数
题目要求
实现函数isdigit, 判断字符串里是否只包含数字0~9
# -*- coding:utf-8 -*-
lst = input("请输入一个字符串:")
print (lst.isdigit())
3、leetcode 5题 最长回文子串
给定一个字符串 s ,找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。
不会…