1.1.列表常用方法
1. append用于在列表末尾追加新的对象a = [1,2,3]
a.append(4) #the result : [1, 2, 3, 4]2. count方法统计某个元素在列表中出现的次数a = ['aa','bb','cc','aa','aa']
print(a.count('aa')) #the result : 33. extend方法可以在列表的末尾一次性追加另一个序列中的多个值a = [1,2,3]
b = [4,5,6]
a.extend(b) #the result :[1, 2, 3, 4, 5, 6]4. index函数用于从列表中找出某个值第一个匹配项的索引位置a = [1,2,3,1]
print(a.index(1)) #the result : 05. insert方法用于将对象插入到列表中a = [1,2,3]
a.insert(0,'aa') #the result : ['aa', 1, 2, 3]6. pop方法会移除列表中的一个元素(默认是最后一个),并且返回该元素的值a = [1,2,3]
a.pop() #the result : [1, 2]a.pop(0)
7. remove方法用于移除列表中某个值的第一个匹配项a = ['aa','bb','cc','aa']
a.remove('aa') #the result : ['bb', 'cc', 'aa']8. reverse方法将列表中的元素反向存放a = ['a','b','c']
a.reverse() #the result : ['c', 'b', 'a']9. sort方法用于在原位置对列表进行排序,意味着改变原来的列表,让其中的元素按一定顺序排列a = ['a','b','c',1,2,3]
a.sort() #the result :[1, 2, 3, 'a', 'b', 'c']10. enumrateli = [11,22,33]
fork,v inenumerate(li, 1):
print(k,v)
1.2 字符串常用方法
#1. find方法可以在一个较长的字符串中查找子串,他返回子串所在位置的最左端索引,如果没有找到则返回-1
a = 'abcdefghijk'print(a.find('abc')) #the result : 0
print(a.find('abc',10,100)) #the result : 11 指定查找的起始和结束查找位置
2. join方法是非常重要的字符串方法,他是split方法的逆方法,用来连接序列中的元素,并且需要被连接的元素都必须是字符串。
a = ['1','2','3']
print('+'.join(a)) #the result : 1+2+3
3. split方法,是一个非常重要的字符串,它是join的逆方法,用来将字符串分割成序列
print('1+2+3+4'.split('+')) #the result : ['1', '2', '3', '4']
4. strip 方法返回去除首位空格(不包括内部)的字符串
print(" test test ".strip()) #the result :“test test”
5. replace方法返回某字符串所有匹配项均被替换之后得到字符串
print("This is a test".replace('is','is_test')) #the result : This_test is_test a test
1.3 字典常用方法
1. clear方法清除字典中所有的项,这是一个原地操作,所以无返回值(或则说返回None)
d = {'name':"tom"}
d.clear()
print(d) #the result : {}
2. fromkeys方法使用给定的键建立新的字典,每个键都对应一个默认的值None
print({}.fromkeys(['name','age'])) #the result : {'age': None, 'name': None}
3. get方法是个更宽松的访问字典项的方法,如果试图访问字典中不存在的项时不会报错仅会 返回:None
d = {'Tom':8777,'Jack':8888,'Fly':6666}
print(d.get('Tom')) #the result : 8777
print(d.get('not_exist')) #the result : None
4. for循环字典的三种方法
d = {'Tom':8777,'Jack':8888,'Fly':6666}
fork,v ind.items():
print(k,v)
fork ind.values():
print(k)
fork ind.keys():
print(k)
5. pop方法用于获得对应与给定键的值,然后将这个”键-值”对从字典中移除
d = {'Tom':8777,'Jack':8888,'Fly':6666}
v = d.pop('Tom')
print(v) #8777
6. setdefault方法在某种程度上类似于get方法,能够获得与给定键相关联的值,除此之外,setdefault还能在字典中不含有给定键的情况下设定相应的键值
d = {'Tom':8777,'Jack':8888,'Fly':6666}
d.setdefault('Tom') #the result : 8777
print(d.setdefault('Test')) #the result : None
print(d) #{'Fly': 6666, 'Jack': 8888, 'Tom': 8777, 'Test': None}
7. update方法可以利用一个字典项更新另一个字典,提供的字典中的项会被添加到旧的字典中,如有相同的键则会被覆盖
d = {'Tom':8777,'Jack':8888,'Fly':6666}
a = {'Tom':110,'Test':119}
d.update(a)
print(d) #the result :{'Fly': 6666, 'Test': 119, 'Jack': 8888, 'Tom': 110}
8. 将两个列表组合成字典
keys = ['a', 'b']
values = [1, 2]
print(dict(zip(keys,values)))
1.4 集合常用方法
list_1 = [1,2,3,4,5,1,2]
1、去重(去除list_1中重复元素1,2)
list_1 = set(list_1) #去重: {1, 2, 3, 4, 5}
print(list_1)
list_2 = set([4,5,6,7,8])
2、交集(在list_1和list_2中都有的元素4,5)
print(list_1.intersection(list_2)) #交集: {4, 5}
3、并集(在list_1和list_2中的元素全部打印出来,重复元素仅打印一次)
print(list_1.union(list_2)) #并集: {1, 2, 3, 4, 5, 6, 7, 8}
4、差集
print(list_1.difference(list_2)) #差集:在list_1中有在list_2中没有: {1, 2, 3}
print(list_2.difference(list_1)) #差集:在list_1中有在list_2中没有: {8, 6, 7}
python dataframe 判断类型
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
上一篇:华三 ospf路由聚合
下一篇:生成对抗网络模型框架
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
java中判断String类型为空和null的方法
java中判断String类型为空和null的方法
System 字符串 Apache -
python 判断for循环最后一次
本文简要介绍了Python 判断for循环最后一次的6种方法。
迭代 迭代器 python -
kubeconfig默认namespace
一、引言 kubeadm 是 k8s 重要的快速部署工具,也是其原生支持的部署工具,在实现自动化部署方面具有重要的研究价值。 本文将基于 Kubernetes 1.12 版本,分析其 init 初始化集群的执行流程,希望对读者进一步理解 k8s 有所帮助!二、流程介绍
kubeadm init 执行流程分析 kubeadm k8s 初始化 加载