Python syntax base (3) _ Supplementary Data Types

First, a list of

Keyword list
written format
list_name = []

List method (increase)

lst=['窗前明月光','举头望明月','低头思故乡']
lst.append('疑是地上')#追加 这个是在末尾加一个
print(lst)

lst.insert(1,'疑是地上霜')#插入 下标的前面
print(lst)

lst.extend('今天是新的一年')#扩展 ————迭代添加 意思就是分开了
print(lst)

List of methods (deletion)

lst=['窗前明月光','疑是地上霜','举头望明月','低头思故乡']
del lst#删除了整个列表
print(lst)

del lst(1:)#删除单个列表
print(lst)

del lst(1:3)#删除通过索引
print(lst)

del lst(1:3:1)#删除通过切片 +步长
print(lst)

lst.remove('举头望明月')#移除通过内容
print(lst)

lst.pop(1)#没有指定,移除默认最后一个
print(lst)

lst.clear()#清空列表
print(lst)

#有返回值的删除
lst=['窗前明月光','疑是地上霜','举头望明月','低头思故乡']
ret=lst.pop(0)#指定下标删除
print(lst)
print(ret)#返回被删除的内容

List of methods (modified)

#通过下表修改
lst=['杨紫','高圆圆','刘亦菲','关晓彤']
lst[0]='bb'
print(lst)

List of methods (inquiry)

#使用循环查询
lst=['杨紫','高圆圆','刘亦菲','关晓彤']
for i in lst:
 print(i)
#使用下标查询
print(lst[0])

List of methods (other operations)

#列表通过内容查找下标
lst=['杨紫','高圆圆','刘亦菲','关晓彤']
ret=lst.index('高圆圆')

#列表通过内容查的计数
lst=['杨紫','高圆圆','刘亦菲','关晓彤']
print(lst.count('高圆圆'))

#列表的复制
 lst=['杨紫','高圆圆','高圆圆','关晓彤']
 ret=lst.copy()
 print(ret)
 print(lst)

#列表内容的翻转
lst=['杨紫','高圆圆','高圆圆','关晓彤']
ret=lst.reverse()
print(ret)
print(lst)

#列表的排序 他是升序
lst=[1,5,99,2,999,77,66]
lst.sort()
print(lst)

#列表的排序 他是降序
lst=[1,5,99,2,999,77,66]
lst.sort(reverse=True)
print(lst)

#列表嵌套
lst=['无敌','dada','555',999,['马赛克','lll']]
print(lst[4][1])

Second, Ganso

Keywords: tuple

  1. Subscripts may be used, ancestral, slice, step.

  2. Only two methods count and index

  3. Tuples are immutable data, some users may not modify the password is stored

rang(0,10)的范围
#python 3中的range(0,10)是一个可迭代对象
#python 2中xrange和python 3一样
print(range(0,10))
#输出结果如下___________________________
 range(0,10)
    
#python 2的range是获取到一个列表
print(range(0,10))
#输出结果如下______________________________
0123456789

note! ! !

and slicing the same rang
rang first is the start position of the second end position is the third step is the
end position of the slice and the same care regardless tail separated by commas

Third, Dictionary

Dict keyword name = 'keys': colon 'value'}

#例子如下
dicter={'A'=1}
  • Dictionary for storing large amounts of data
  • The key is the dictionary data type immutable
  • Dictionary keys can not repeat, repeat, it would cover the value
  • Value dictionary, free
  • Not in the dictionary remove method

Dictionary method (increase)

method one

dic={'01':'SkyRabbit'}
dic[2]='王思聪'
print(dic)

Method Two

  • setdefault workflow
  • First go through the dictionary query by value has no value
  • If you can not add value
  • If no value is added
dic={'01':'SkyRabbit'}
dic.setdefault(11,'王健林')
print(dic)

Dictionary methods (deletion)

  dic={'01':'SkyRabbit'}
  del dic#删除整个字典
  
  dic={'01':'SkyRabbit'}
  del dic['01']#通过键删除
  
  dic={'01':'SkyRabbit'}
  dic.pop() #通过指定键删除,或者默认删除后面的
  
  dic={'01':'SkyRabbit'}
  dic.clear() #清空
  
  dic.popitem()#随机删除 在3.6版本中只删除最后一个  3.6以下随机

Dictionary method (modified)

    dic={'01':'SkyRabbit'}
    dic.update({'01':'王健林'})#被更新的内容如果存在 那么更新的内容中的值就会被覆盖
    print(dic)
    
    
    dic={'01':'SkyRabbit'}
    dic1={'02':'王健林'}
    dic.update(dic1)
    print(dic)
    #如果两个字典中都没有一样的就是会合并
    
    dic={'01':'SkyRabbit'}
    dic['01']='王健林'
    print(dic)#字典的增 是字典中没有的时候才叫增,如果字典中这个键存在就叫做修改
    

Dictionary method (to find)

#字典通过键去查找值 如果键不存在就会报错 写法如下 
dic={'01':'SkyRabbit'}
print(dic['01'])

dic={1:'SkyRabbit'}
print(dic[1])

#通过键查找使用方法 如果不存在不会报错,而是返回None
dic={'01':'SkyRabbit'}
print(dic.get('01'))

#不存在返回None
dic={'01':'SkyRabbit'}
print(dic.setdefault(11))

Dictionary Methods (else)

#获取所有键和值 方法一
dic ={1:"A",2:'B'}
for i in dic:
    print(i,dic[i])
#获取所有键和值 方法二
dic ={1:"A",2:'B'}
for i,v in dic.items():
    print(i,v)
    
#获取所有键
dic ={1:"A",2:'B'}
print(dic.keys())

#获取所有值
dic ={1:"A",2:'B'}
print(dic.values())

Fourth, the collection

Set key
is used to store data, deduplication natural, disorder, can not use the subscript
set inside the element can only put the variable data

number={'da ',8,15,1115,1,1,1212,12}
print(number)

Method set (increased)

  #方法一 直接add()添加
  Name={'小明','小刚','小美'}
  Name.add('小熊')
  print(Name)
  #方法一 直接update()迭代更新
  Age={'1','2','3'}
  Age.update('4')
  print(Age)
  #方法一 使用数列update()迭代更新
  Age={'1','2','3'}
  Age.update(['1','5','6'])
  print(Age)

Collection method (deleted)

#方法一 使用pop()随机删除
Age={'1','2','3'}
item=Age.pop()
print(Age)
#方法二 使用remove()寻找元素直接删除
Age={'1','2','3'}
Age.remove('2')
print(Age)
#方法三 使用clear()寻找元素直接删除

Method set (modified)

#先移除在再添加就是修改 ,因为集合是无序的
Age={'1','2','3'}
Age.remove('1')
Age.add(666)
print(Age)

Collection method (query)

#使用循环查看
Age={'1','2','3'}
for number in Age:
    print(number)

Guess you like

Origin www.cnblogs.com/SkyRabbit/p/11203355.html