西安3期-陈浩 发表于 2022-6-19 22:14:43

字符串、列表、元组、字典

# 字符串、列表、元组、字典


# startswith():判断字符传是以什么开始,返回布尔值
# str1="chenhao"
# print(str1.startswith("ch")) #返回结果:true
# print(str1.startswith("h"))#返回结果:false

#endswith():判断字符以什么结尾
# str1="chenhao"
# print(str1.endswith("ao"))   #返回结果:true
# print(str1.endswith("h"))    #返回结果:false
#判断文件是否以txt结尾
# file=input("请输入文件:")
# if file.endswith("txt"):
#   print("正确")
# else:
#   print("错误")

#find()函数:字符串查询,从左边开始    结果显示下标即索引
# str1="chenhao"
# print(str1.find("n"))   #返回结果:3
# print(str1.find("h",2,5)) #返回姐果:4
# #rfind()函数:字符串查询,从右边开始    结果显示下标即索引    返回值为正向索引
# print(str1.rfind("h"))    #返回结果:4

#replace(substring,newstring,max)函数:
#substring:表示被替换的字符串
#newstring:表示要替换的字符串
#max:表示替换的次数
# str1="chenhao"
# print(str1.replace("h","n"))   #返回结果:cnennao
# print(str1.replace("h","n",1))#返回结果:cnenhao


#lower():转换字符串中所有大写字母为小写
# str1="ChenHao"
# print(str1.lower())       #结果:chenhao
# #upper():转换字符串中所有小写字母为大写
# print(str1.upper())       #结果:CHENHAO


#isdigit()函数:判断字符串中是否全为数字   返回布尔值
# str1="123456"
# print(str1.isdigit())      #返回true
# #isalpha():判断字符串中是否权威字母
# str1="abcdef"
# print(str1.isalpha())      #返回true
# #isalnum():判断字符串中是否全为字母,数字,或者字母数字组合
# #如果由特殊字符:返回false
# str1="123abc"
# print(str1.isalnum())      #返回true


#istitle():判断字符串中首字母是否为大写,其他是否为小写,其他不为小写则为否
# str1="Chenhao"
# print(str1.istitle())          #返回true
# str1="CHENHAO"
# print(str1.istitle())          #返回false


#isupper():判断字符串是否都是大写
# str1="chenhao"
# print(str1.isupper())         #返回false
# #islower():判断字符串是否都是小写
# print(str1.islower())         #返回true

#==========================================================================


#python中的列表‘
# 列表是一个容器,可以装任何类型的数据,列表是有序的,可以使用索引和切片
# 列表list表示
# 表达式list1 = [](中括号)


#定义列表方法一:
# list1=["chenhao",27,"男","咸阳"]
# print(list1)                #['chenhao', 27, '男', '咸阳']
# #定义列表方法二:
# str1="chenhao"
# list1=list(str1)
# print(list1)                #['c', 'h', 'e', 'n', 'h', 'a', 'o']


#列表中的常用用法:
#1.索引赋值
# list1=["chenhao",27,"男","咸阳"]
# list1="西安"   #list1把“西安”赋值给list1列表中索引为3的位置,覆盖原来的值
# print(list1)       #['chenhao', 27, '男', '西安']

#2.切片赋值
# list1=["chenhao","27","男","咸阳"]
# list1="hello"
# print(list1)      #['chenhao', 'h', 'e', 'l', 'l', 'o', '咸阳']


#列表中的函数
#1.append()函数,添加元素在列表中,默认列表最末端
# list1=["chenhao","27","男","咸阳"]
# list1.append("西安")    #字符串
# list1.append(123)      #整型
# list1.append(["陕西"])#列表
# print(list1)      #['chenhao', '27', '男', '咸阳', '西安', 123, ['陕西']]

#2.insert()函数,给列表中插入元素-----根据索引位置插入
# list1=["chenhao","27","男","咸阳"]
# list1.insert(2,"zhenshuai")   #字符串
# list1.insert(2,20000)         #整型
# list1.insert(2,["yes"])       #列表
# print(list1)             #['chenhao', '27', ['yes'], 20000, 'zhenshuai', '男', '咸阳']


#3.extend()函数,连接两个表,整合成一个列表,还可以列表拼接字符串
#列表连接列表
# list1=["chenhao","27","男","咸阳"]
# list2=["name","age"]
# list1.extend(list2)
# print(list1)         #['chenhao', '27', '男', '咸阳', 'name', 'age']
# #列表连接字符串
# list1=["chenhao","27","男","咸阳"]
# str1="xiaoxiong"
# list1.extend(str1)
# print(list1)#['chenhao', '27', '男', '咸阳', 'x', 'i', 'a', 'o', 'x', 'i', 'o', 'n', 'g']
#***************列表不能连接整型


#4.index()函数:查看列表元素的索引值
# list1=["chenhao","27","男","咸阳"]
# print(list1.index("chenhao"))    #0


#5.remove()函数:移除列表中的元素-------根据元素移除
# list1=["chenhao","27","男","咸阳"]
# list1.remove("chenhao")
# print(list1)      #['27', '男', '咸阳']
#如果列表中存在重复的元素,只会删除前面的
# list1=["chenhao","27","男","咸阳","咸阳"]
# list1.remove("咸阳")#['chenhao', '27', '男', '咸阳']
# list1.remove("咸阳")#删除两次
# print(list1)      #['chenhao', '27', '男']
#del关键字--------根据索引删除
# list1=["chenhao","27","男","咸阳"]
# del list1
# print(list1)       #['chenhao', '27', '男']


#6.pop()函数:删除列表元素,不传入值的话,默认删除列表最后元素
# list1=["chenhao","27","男","咸阳"]
# # print(list1.pop())   #显示删除的元素"咸阳"
# # print(list1)   #['chenhao', '27', '男']
# print(list1.pop(2))   #显示删除的元素“男”
#print(list1)      #['chenhao', '27', '咸阳']


#7.使用切片对列表进行反转
#reverse()函数:反转函数
# list1=["chenhao","27","男","咸阳"]
# print(list1[::-1])   #['咸阳', '男', '27', 'chenhao']
# list1.reverse()
# print(list1)         #['咸阳', '男', '27', 'chenhao']


#8.列表排序
#sort()函数:对列表进行排序,只能针对全部是int或者str类型的元素,不能混合排序
#sort()函数----默认升序排列-----0-9>A-Z>a-z
#sort()函数降序排列-----需要添加参数例如:sort(reverse=Ture)
# list1=["chenhao","27","男","咸阳"]
# list1.sort()
# print(list1)   #['27', 'chenhao', '咸阳', '男']
# list1.sort(reverse=True)
# print(list1)   #['男', '咸阳', 'chenhao', '27']


#9.sorted()函数:也可以对列表进行排序,排序完序之后,会生成一个排序之后的新的列表
# sorted()函数默认升序排列
# sorted()函数降序排列需要加上一个reverse=True的参数
# list1=
# list2=sorted(list1)
# print(list2)   #
# list3=sorted(list1,reverse=True)
# print(list3)   #


#python中的元组
'''
元组tuple,表达式tuple1 = () ,是一个"写保护"的,创建之后里面的元素是不能被修改的
元组是有序的,并且也是可以存储任何数据类型的
'''
#定义元组的方法一:
# tuple1 = ("dcs","xiaozhang",12,["hello","china"])
# print(tuple1) # ('dcs', 'xiaozhang', 12, ['hello', 'china'])
#定义元组的方法二:
# list1=["chenhao","27","男","咸阳"]
# tuple1=tuple(list1)
# print(tuple1)   #('chenhao', '27', '男', '咸阳')

#当元组中的元素个数为1的时候,需要在最后加一个英文,逗号
# tuple1 = ("xiaozhang",)
# print(tuple1) # ('xiaozhang',)
# print(type(tuple1)) # <class 'tuple'>


#使用索引取值------****不能给元组修改,删除,添加元素
# tuple1=("chenhao",27,"男","咸阳",["hello","lihuanying"])
# print(tuple1)   # 27


#间接修改元素------先把元组转换成列表,修改完成后,再转换成元组
# tuple1=("chenhao",27,"男","咸阳",["hello","lihuanying"])
# list1=list(tuple1)   #['chenhao', 27, '男', '咸阳', ['hello', 'lihuanying']]
# list1="女"   #['chenhao', 27, '女', '咸阳', ['hello', 'lihuanying']]
# tuple2=tuple(list1)
# print(tuple2)   #('chenhao', 27, '女', '咸阳', ['hello', 'lihuanying'])


# 元组可以改变吗?
# 当元组中的元素作为一个整体的情况之下,不是能被改变的,当元组中的元素为列表的时候
#我们可以通过索引去修改列表中的值
# tuple1=("chenhao",27,"男","咸阳",["hello","lihuanying"])
# tuple1="zhangxiaofei"
# print(tuple1)#('chenhao', 27, '男', '咸阳', ['hello', 'zhangxiaofei'])


#可迭代----通过for循环遍历元组,从而取到元组中的所有元素
# tuple1=("chenhao",27,"男","咸阳",["hello","lihuanying"])
# for i in tuple1:# i:为自定义变量
#   print(i)    #chenhao
#               # 27
#               # 男
#               #咸阳
#               #['hello', 'lihuanying']



#python中的字典
'''
字典(dict)表达式为dict = {}
字典是无序的,是以键值对的形式存在也就是key:value
键(key)是唯一的,值(value)是任意的
'''

#定义字典的方法一:
# dict1={"chenhao":"27","男":"咸阳"}
# print(dict1)#{'chenhao': '27', '男': '咸阳'}
# print(type(dict1))# <class 'dict'>
##定义字典的方法一:
# list1=[["chenhao","27"],["男","咸阳"]]
# dict1=dict(list1)
# print(dict1)      #{'chenhao': '27', '男': '咸阳'}


#1.给字典添加一个键值对
# dict1={"chenhao":"27","男":"咸阳"}
# dict1["hi"]="175"
# print(dict1)      #{'chenhao': '27', '男': '咸阳', 'hi': '175'}

#2.取出字典中的键------通过for循环遍历字典中的键(key)
#dict1={"chenhao":"27","男":"咸阳"}
# for k in dict1.keys():
#   print(k)   #chenhao
#                #男
#不加修饰的遍历
# for i in dict1:
#   print(i)      # chenhao
                # 男

#3.取出字典中的值------通过遍历取值,取字典中的所有值
# dict1={"chenhao":"27","男":"咸阳"}
# for v in dict1.values():
#   print(v)   #27    咸阳

#4.取出字典中特定的值------用键取值
# dict1 = {"chenhao": "27", "男": "咸阳"}
# print(dict1["chenhao"])    #27
# print(dict1["男"])         #咸阳


#5.setdefault()函数:给字典添加键值对----- # 如果添加的键在字典中,则不生效/只添加键,则默认值为:none
# dict1 = {"chenhao": "27", "男": "咸阳"}
# # dict1.setdefault("xiaoxing","27")
# # print(dict1)   #{'chenhao': '27', '男': '咸阳', 'xiaoxing': '27'}
# dict1.setdefault("xiaoxing",)
# print(dict1)#{'chenhao': '27', '男': '咸阳', 'xiaoxing': None}


#6.关键字del:删除字典
# dict1 = {"chenhao": "27", "男": "咸阳"}
# del dict1
# print(dict1)   #NameError: name 'dict1' is not defined

#7.删除字典中的键值对-----根据键(key)来删除键值对
# dict1 = {"chenhao": "27", "男": "咸阳"}
# del dict1["chenhao"]
# print(dict1)   #{'男': '咸阳'}

#8.同时取字典中的键与值
# dict1 = {"chenhao": "27", "男": "咸阳"}
# #方法一:
# for i in dict1:
#   print(i,dict1)    # chenhao 27   男 咸阳
# #方法二:
# for k,v in dict1.items():
#   print(k,v)       # chenhao 27   男 咸阳


#pop()函数:删除指定的键并返回删除的值-----通过key来删除值
# dict1 = {"chenhao": "27", "男": "咸阳"}
# print(dict1.pop("chenhao"))   #27 ----删除的内容
# print(dict1)         #{'男': '咸阳'}


#clear()清空字典中的所有项
# dict1 = {"chenhao": "27", "男": "咸阳"}
# dict1.clear()
# print(dict1)   #{}


#get()函数:通过字典中的key获取键的值
# dict1 = {"chenhao": "27", "男": "咸阳"}
# value=dict1.get("chenhao")
# print(value)   #   27


页: [1]
查看完整版本: 字符串、列表、元组、字典