yxy&lxt的python容器

# ####################列表及常用的方法#######################

list1 = [6, 66, 666, 'yxy', 7, 77, 777, 8, 888]

list1[1] = 666

print(list1[1], list1[-1]) # out-666 888

list2 = [[666, 777, 888, 'yxy'], ['lxt', 999]]

print(list2[0][3], list2[1][0], list2[1][1]) # out-yxy lxt 999

# 一:list.index(?)---查找列表中元素?的下标(以第一次出现的为准),不存在会报错

print(list1.index('yxy')) # out-3

# 二:list.insert(a,b)---插入元素,将b插在下标为8的元素的前面

list1.insert(8, 88)

print(list1) # out-[6, 666, 666, 'yxy', 7, 77, 777, 8, 88, 888]

# 三①:list.append(?)---在列尾追加一个元素?

list1.append(9)

print(list1) # out-[6, 666, 666, 'yxy', 7, 77, 777, 8, 88, 888, 9]

# 三②:list.entend(container) --- 在列表尾追加多个元素

list3 = [99, 999]

list1.extend(list3)

print(list1) # out-[6, 666, 666, 'yxy', 7, 77, 777, 8, 88, 888, 9, 99, 999]

# 四:del list[?]/list.pop(?)---删除下标为?的元素

n = int(input()) # in-3

del list1[n]

# 或list1.pop(n) -可用参数接收

print(list1) # out-[6, 666, 666, 7, 77, 777, 8, 88, 888, 9, 99, 999]

# 五:list.remove(?)---从前到后删除第一个元素?

# 六:list.clear()---清空列表

list1.clear()

print(list1) # out-[]

# 七:list.count(?)---统计元素?在列表中出现的次数

list1 = [6, 6, 7, 666, 7, 6, 6, 7]

print(list1.count(6)) # out-4\

#list的遍历

list1 = [1, 2, 3, 4, 5, 6]

for i in range(0, 6):

list1[i] += 1

print(list1) # out-[2, 3, 4, 5, 6, 7]

list2 = []

list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

for i in range(0, 6):

if list1[i] % 2 == 0:

list2.append(list1[i])

print(list2) # out-[2, 4, 6]

# ####################元组及常用的方法#######################

# 如果元组只有一个元素,元素后加“,”,元组不能通过反向下标索引

t0 = ("yxy",)

print(t0) # out-('yxy',)

t1 = (("lxt", 999), 999)

print(t1[-1]) # 直接报错

print(t0[0], t1[0][0], t1[0][1]) # out-yxy lxt 999

# 一:tuple.index(?) --- 查找元素?的下标

t3 = (666, 1, 3, 5, 666, 666, 666)

print(t3.index(666)) # 以第一次出现的为准,out-0

# 二:tuple.count(?) --- 查找元素?出现的次数

# 三:len(tuple) --- 计算元组的长度

# 元组的元素不能修改,但元组中嵌套的列表中的元素可以修改

t4 = (6, 77, 666)

# t4[1] = 66 # 直接报错

t5 = (["yxy", 666], "lxt")

t5[0][1] = 999

print(t5[0][0], t5[1], t5[0][1]) # out-yxy lxt 999

# ####################字符串及常用的法#######################

# 字符串不能修改,除非开一个新字符串

# 一:str0.index(str1) -- -在字符串str0中寻找字符串str1的下标,找不到会报错

str0 = "lxt yxy 999"

str1 = "yxy"

print(str0.index("yxy")) # out-4

# 二:new_str0 = str0.replace(str0, str1) --- 将字符串str1替代str0,并形成新字符串

str3 = "fuck you"

str4 = "i love you"

new_str3 = str3.replace(str3, str4)

print(new_str3) # out-i love you

# 三:str.split("分割符/串") --- 将字符串以分割符/串进行分割,以列表形式进行报存

str5 = "yxy lxt 999"

print(str5.split(" ")) # out-['yxy', 'lxt', '999']

# 四:①str.strip() --- 字符串规整,去除字符串的前后空格和换行符

# ②:str0.strip(str) --- 去除字符串前后指定字符子串

str6 = " yxy lxt 999 666 666"

print(str6.strip()) # out-yxy lxt 999 666 666

print(str6.strip("666")) # out- yxy lxt 999 666

# 五:str0.count(str1) --- 统计字符串str0中str1出现的次数

print(str6.count("666")) # out-2

s = "itheima itcast boxuegu"

print(s.count("it")) # out-2

print(s.replace(" ", "|")) # out-itheima|itcast|boxuegu

print(s.split("|")) # out-['itheima itcast boxuegu']

# 序列的切片 --- 序列[起始下标:结束下标:步长],结束下标为空表示到结尾,起始下标为空表示从头开始,步长为n(默认为1,可以不写),表示跳过n-1个元素取,为负数表示反向取,切片后产生新序列,对原序列无影响

print(s[::3]) # out-ieatsbuu

# ####################集合及常用的方法#######################

# set不支持下标索引

# 一:set.add(?) --- 将元素?添加到集合中

set0 = {"yxy", "lxt"}

set0.add(999)

print(set0) # out-{'yxy', 'lxt', 999}

# 二:set.remove(?) --- 将集合中的元素?移除

set0.remove(999)

print(set0) # out-{'yxy', 'lxt'}

# 三:set.pop() --- 在集合中随机取出一个元素

print(set0.pop()) # out-yxy

print(set0) # out-{'lxt'}

# 四:set.clear() --- 清空集合

# 五:set1.difference(set2) --- 取出set1里有而set2里没有的元素(set1和set2的差集),set1和set2不会被修改

set1 = {1, 3, 5, 7, 9}

set2 = {3, 5, 7}

print(set1.difference(set2)) # out-{1, 9}

print(set1, set2) # out-{1, 3, 5, 7, 9} {3, 5, 7}

# 六:set1.difference(set2) --- 消除差集,在set1内消除与set2相同的元素,set1被修改而set2没有被修改

set1 = {1, 3, 5, 7, 9}

set2 = {3, 5, 7}

set1.difference_update(set2)

print(set1, set2) # out-{1, 9} {3, 5, 7}

# 七set1.union(set2) --- 将set1与set2合并,set1和set2不变

set3 = {1, 3, 5, 7, 9}

set4 = {3, 5, 7, 11}

print(set3.union(set4)) # out-{1, 3, 5, 7, 9, 11}

print(set3, set4) # out-{1, 3, 5, 7, 9} {3, 11, 5, 7}

# 八:set.len() --- 统计集合中元素的个数

# 九:集合的遍历只能用for循环

# ####################字典及常用的方法#######################

# 字典的key不允许重复,不能使用下标索引,dict的key不能是dict

d0 = {'yxy': 999, 'lxt': 999, 999: 'yxy lxt 999'}

print(d0['yxy'], d0[999]) # out-999 yxy lxt 999

# 一:字典的嵌套

d1 = {"yxy": {"语文": 99, "数学": 100, "英语": 98},

"lxt": {"语文": 100, "数学": 98, "英语": 100}}

print(d1["lxt"], d1["yxy"]["语文"]) # out-{'语文': 100, '数学': 98, '英语': 100} 99

# 二:字典新增元素

d2 = {"yxy": {"语文": 99, "数学": 100, "英语": 98},

"lxt": {"语文": 100, "数学": 98, "英语": 100}}

d2["wlh"] = {"语文": 0, "数学": 0, "英语": 0}

print(d2) # out-{'yxy': {'语文': 99, '数学': 100, '英语': 98}, 'lxt': {'语文': 100, '数学': 98, '英语': 100}, 'wlh': {'语文': 0, '数学': 0, '英语': 0}}

# 三:字典元素更新

d2["yxy"]["英语"] = 99

print(d2) # out-{'yxy': {'语文': 99, '数学': 100, '英语': 99}, 'lxt': {'语文': 100, '数学': 98, '英语': 100}, 'wlh': {'语文': 0, '数学': 0, '英语': 0}}

# 四:dict.pop(?) --- 将字典中元素?删除

d2.pop("wlh")

print(d2) # out-{'yxy': {'语文': 99, '数学': 100, '英语': 99}, 'lxt': {'语文': 100, '数学': 98, '英语': 100}}

# 五:dict.clear() --- 清空字典

# 六:dict.keys() --- 取出字典中的全部key

key = d2.keys()

print(key) # out-dict_keys(['yxy', 'lxt'])

print(type(d2.keys())) # out-<class 'dict_keys'>

# dict的遍历

# ①:

for i in d2.keys():

print(i, end=" ") # 取出key,out-yxy lxt

for i in d2:

print(i,end=" ") # 取出key,out-yxy lxt

# 七:len(dict) --- 统计字典中元素数量

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值