python 字符串string、列表list、字典dict、元组tuple、集合set

字符串string

属于不可变类型
(1)切片、逆序

切片:[a:b:步长]
    string[a:b],取的范围是[a,b),左闭右开
逆序:
    string[a:b:-1]
name ="01234567"
name[0:-1]      # 0123456
name[0:]        # 01234567

name[-1:0-1]  # 1234567
name[-1:-1]   # 01234567

(2)API

1len() # 求字符串长度
2、在string中查找substr的位置:find、rfind、index
      string.find(substr)   # 查找成功返回index,查找失败返回-1
      string.rfind(substr)  # 查找成功返回index,查找失败返回-1
      string.index(substr)  # 查找成功返回index,查找失败程序崩溃
      string.rindex(substr) # 查找成功返回index,查找失败程序崩溃
3、count() # 统计出现的次数
      string.count(substr)
4、replace(str1,str2,替换次数) # 把string中的str1全部替换成str2
      string.replace(str1,str2,string.count(str1))
5、
  split("分割符号")  # 按照“分割符号”将string切割,切割后的结果存放到list中
      In [1]: mystr = "My name is Tom"
      
      In [2]: mystr.split()
      Out[2]: ['My', 'name', 'is', 'Tom']
  splitlines # 按照换行符分割,返回一个包含各行作为元素的列表
      In [16]: mystr = "hello\nworld"

      In [17]: mystr.splitlines()
      Out[17]: ['hello', 'world']
6、
      capitalize # 把string的第一个字符变成大写
      title      # 把string的每一个单词的第一个字符都变成大写
      upper      # 全部变为大写
      lower      # 全部变为小写
7、
      string.startswith(obj)  # 检查string是否以obj开头,是则返回True,否则返回False
      string.endwith(obj)     
8、格式显示
      ljust   # 返回一个源字符串左对齐,并使用空格填充至长度width的新字符串
      rjust   # 返回一个源字符串右对齐,并使用空格填充至长度width的新字符串
      center  # 返回一个源字符串右对齐,并使用空格填充至长度width的新字符串
		In [8]: mystr = "Hello_Kitti"
		
		In [9]: mystr.ljust(50)
		Out[9]: 'Hello_Kitti                                       '
		
		In [10]: mystr.rjust(50)
		Out[10]: '                                       Hello_Kitti'
		
		In [11]: mystr.center(50)
		Out[11]: '                   Hello_Kitti                    '
9、
      lstrip  # 删除string左边的空格
      rstrip  # 删除string右边的空格
      strip   # 删除string两端的空格
      
10、partition # 将string以substr划分成左中右三部分,返回元组
		In [12]: mystr = "I am Tom"
		
		In [13]: mystr.partition("am")
		Out[13]: ('I ', 'am', ' Tom')   # 返回元组()
11、类型判断
      isalpha  # 是纯字母么?
      isdigit  # 是纯数字么?
      isalnum  # 既有数字也有字母,为True;纯字母、纯数字为False
      isspace  # 只包含空格(纯空格),返回True    
12、将列表中的str连接成一个整体
		In [20]:  myList = ['I','am','Tom']
		
		In [21]: ch = " "
		In [22]: ch.join(myList)
		Out[22]: 'I am Tom'
		
		In [25]: ch = '__'
		In [26]: ch.join(myList)
		Out[26]: 'I__am__Tom'

列表list

属于可变类型

1、添加
	append(待添加的内容)      # 添加到最后一个位置
	insert(位置,待添加的内容)
	list1.extend(list2)      # 将list2合并到list1中去
2、删除
	list.pop()           # 删除最后一个元素
	list.remove("老王")  # 根据内容删除,只删除一次
	del list[下标]       # 根据下标删除
3、修改
	list[下标] = new值
4、查询
	if 元素 in list:
	if 元素 not in list:

len(list) 
list.reverse()   # 逆序,改变list
list.sort(reverse=True)  # 排序,改变list
list中的元素是dict,排序使用lambda匿名函数,例如:
	list = [{"name":"Tom","age":18},
	        {"name":"James","age":34},
	        {"name":"Kobe","age":38}
	        ]
	list.sort(key=lambda x:x["age"])
	print(list)

列表生成式:使用range完成

range(l,r,step)  # [l,r) step

new_list = [ i for i in range(0,10,2) ]
	# [0, 2, 4, 6, 8]
new_list = [ i for i in range(0,10) if i%2 == 0 ]         #  可以加if条件
	# [0, 2, 4, 6, 8]
new_list = [ (i,j) for i in range(2) for j in range(3) ]  
	# [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2)]

字典dict

key属于不可变类型,value属于可变类型
dict = { 键:值,键:值,… … }
可变类型不能当key

1、添加/修改 键值对
	dict[新的key] = value      # 添加
	dict[已存在的key] = value   # 修改 
2、删除
	del list[key]    # 删除不存在的key,报错
4、查询
	dict[key]     # 查询不到,报错
	dict.get(key) # 查询不到,不报错

len(dict)
dict.keys()  # 所有的key
dict.values  # 所有的value

dict独有的函数:items()
注意:tuple、list没有items()函数,dict有items()函数

dict = {"name":"Tom","age":18}
print(dict.items())    # dict_items( [('age', 18), ('name', 'Tom')] )

items()函数详解:
# 1. items()返回值是list = [('age', 18), ('name', 'Tom')]
# 2. list中的每个元素都是tuple,即('age', 18) 和 ('name', 'Tom')

for key,value in dict.items():  # 使用拆包操作: key,value = tuple
   print("key = %s, value = %s" %(key,value))

元组tuple

属于不可变类型
与list类似,只不过它不支持“增删改”,只支持“查”


补充:拆包操作

list = [1,2,3]
a,b,c = list

tuple = ("Tom","Kobe","James")
a,b,c = tuple

集合set

set中没有重复的数据,可以用于“去重”
add、pop、update、remove # 增删改查
& 交集 | 并集 - 差集 ^ 对称差集

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值