列表、元组和字符串
1 三者区别
✔✔✔✔ 列表是最常用的Python数据类型,它可以作为一个方括号内的逗号分隔值出现。
✨✨✨✨元组其实跟列表差不多,也是存一组数,只是他一旦创建,便不能再修改,所以又叫只读列表。
👀👀👀👀字符串是 Python 中最常用的数据类型。我们可以使用引号('或")来创建字符串。
1.1 ***列表list
有序的,可使用索引
列表是可变的
线性的数据结构
使用[]表示***
1.2元组tuple
一个有序的元素组成
使用小括号()表示
不可变的
1.3字符串
由字符组成的有序的序列,使用引号引住的字符序列
不可变的
字符串支持索引访问
难点
💖💖💖💖浅拷贝与深拷贝*🌹🌹🌹
import copy
name=["张三","赵四","王五",["李易峰","朱一龙"],"刘德华","周杰伦"]
name2=name.copy()
name3=copy.deepcopy(name)
name2[0]="张八"
name3[3][0]="王一峰"
print(name)
print(name2)
print(name3)
['张三', '赵四', '王五', ['李易峰', '朱一龙'], '刘德华', '周杰伦']
['张八', '赵四', '王五', ['李易峰', '朱一龙'], '刘德华', '周杰伦']
['张三', '赵四', '王五', ['王一峰', '朱一龙'], '刘德华', '周杰伦']
❤❤❤❤name是一个既包含元素又包含列表的列表,name2和name3是copy列表name的两个列表,不同的是name2是浅copy,而name3是深copy。name2只copy了列表name的第一层,而其中包含的列表没有copy进来 ,只是copy了包含的列表的地址,因此name和name2两个列表只要有一个列表修改了内层的列表,两个列表便都修改的列表的内容。但是name3却不会发生这样的事情,因为name3是深copy,copy的不只是内层的列表的地址,而是将内层的列表也copy了一份,因此:name3[3][0]="王一峰"这句语句只改变name3内层的列表而并不改变name的内层列表。 👌👌👌👌
😃😃😃😃 当然,浅copy也有用处。浅copy,只copy第一层列表之中可以包含一层列表,但此时copy只能copy到包含列表的地址,而不能copy到实际内容。✌✌✌
列表练习操作
lst = [2, 5, 6, 7, 8, 9, 2, 9, 9]
lst.append(15)
print(lst)
lst.insert(5,20)
print(lst)
lst.extend([2,5,6])
print(lst)
lst.pop(3)
print(lst)
print(lst[::-1])
lst.sort()
print(lst)
lst.sort(reverse=True)
print(lst)
[2, 5, 6, 7, 8, 9, 2, 9, 9, 15]
[2, 5, 6, 7, 8, 20, 9, 2, 9, 9, 15]
[2, 5, 6, 7, 8, 20, 9, 2, 9, 9, 15, 2, 5, 6]
[2, 5, 6, 8, 20, 9, 2, 9, 9, 15, 2, 5, 6]
[6, 5, 2, 15, 9, 9, 2, 9, 20, 8, 6, 5, 2]
[2, 2, 2, 5, 5, 6, 6, 8, 9, 9, 9, 15, 20]
[20, 15, 9, 9, 9, 8, 6, 6, 5, 5, 2, 2, 2]
lst = [1, [4, 6], True]
请将列表里所有数字修改成原来的两倍
def double_list(lst):
for index, item in enumerate(lst):
if isinstance(item, bool):
continue
if isinstance(item, (int, float)):
lst[index] *= 2
if isinstance(item, list):
double_list(item)
if __name__ == '__main__':
lst = [1, [4, 6], True]
double_list(lst)
print(lst)
给定一个山脉数组,求顶峰索引
class Solution(object):
def peakIndexInMountainArray(self, A):
"""
:type A: List[int]
:rtype: int
"""
a = max(A)
for i in range(len(A)):
if A[i]==a:
return i
元组
写出下面代码的执行结果和最终结果的类型
print((1, 2)*2 )
print((1, )*2)
print((1)*2)
(1, 2, 1, 2)
(1, 1)
2
拆包
拆包: 对于函数中的多个返回数据, 去掉元组, 列表 或者字典 直接获取里面数据的过程.
💖💖💖💖***a, b = 1, 2不属于拆包***🌹🌹🌹🌹
*拆包将一个结构中的数据拆分为多个单独变量中 args kwargs
元组拆包可以应用到任何迭代对象上, 唯一的要求是, 被可迭代对象中的元素数量必须要和这些元素的元组的空档数一致, 除非我们用 来表示忽略多余的元素。*
字符串
💋💋💋💋***批量替换字符串内容***
replace(x, old, new=None, strip=False) -> str:
x: 原始字符串
old: 要替换的内容,可为 str , list
new: 新内容,可为 str , list , None
strip: 是否删除前后空格
不传新内容 new ,则要替换的内容 old 被删掉。
💖💖💖💖***怎么把字符串按照空格进⾏拆分***
str.split()
😁😁😁 😁***怎么去除字符串⾸位的空格?***
str.lstrip()
leetcode 5题 最长回文子串
给定一个字符串 s ,找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。
class Solution:
def longestPalindrome(self, s: str) -> str:
if not s:
return s
length = len(s)
if length == 1:
return s
if length > 1000:
return null
result = ''
auxiliary = 0
for index in range(1, length):
index1 = index - 1
index2 = index
while index1 >= 0 and index2 < length and s[index1] == s[index2]:
index1 -= 1
index2 += 1
if index2 - index1 + 1 > auxiliary:
auxiliary = index2 - index1 + 1
result = s[index1 + 1: index2]
index3 = index - 1
index4 = index + 1
while index3 >= 0 and index4 < length and s[index3] == s[index4]:
index3 -= 1
index4 += 1
if index4 - index3 + 1 > auxiliary:
auxiliary = index4 - index3 + 1
result = s[index3 + 1: index4]
return result