爱若晨风 发表于 2018-8-13 13:40:27

python的基本数据类型:列表的方法

  整数和浮点(Python3 没有int和long只有int类型)
  十进制 默认
  二进制 0bnnn
  八进制 0onnn
  十六进制 0xnnn
  float() decimal 固定小数模块
  fractions有理分数
  Bool类型 Ture False
  bool()默认False
  and or not 不支持xor
  & l ^ ~(or)
  << >> 二进制移位
  None 空对象(唯一的)
  不可调用
  容器內型
  可迭代的类型
  字符串 单重 双重 三重 引号
  字符串不可变,基于当年字符串重新创建字符串,指向新的地址
  uppper lower capitalize
  center ljust rjust 对齐方式
  startwith endwith
  find index rfind
  isalpha isdigit isalnum
  join
  split splitlines partition 分割
  strip lstrip rstrip 出去指定字符默认除去空格,全局replace
  replace
  format
  字节和字节数组
  byte bytearry
  1、列表、元组操作
  定义列表
list1=[]  
list2=['a','bit','clor']
  
list3=*5
  列表的元素访问
>>> list2=['a','bit','colr','doll','esr','fire']  
>>> list2=['a','bit','colr','doll','esr','fire']
  
>>> list2   #0为第一个
  
'a'
  
>>> list2[-1] #-1为倒数第一个
  
'fire'>>> list2[-2]
  
'esr'
  
>>> list2 #0开始取完,由于是开区间,右边就不能写-1,不然取不完
  
['a', 'bit', 'colr', 'doll', 'esr', 'fire']
  
>>> list2[:-2] #第一个到倒数第二个,不包含倒数第二个,左闭右开的区间
  
['a', 'bit', 'colr', 'doll']
  
>>> list2[::2] #步长为2
  
['a', 'colr', 'esr']

  基本方法:
  增加
  list.append()
>>> list2.append('thisisappendin')  
>>> list2
  
['a', 'bit', 'colr', 'doll', 'esr', 'fire', 'thisisappendin']
  list.insert("positon",'')
>>> list2.insert(2,'inse')  
>>> list2
  
['a', 'bit', 'inse', 'colr', 'doll', 'esr', 'fire', 'thisisappendin']
  删除
  del list
>>> del list2  
>>> list2
  
Traceback (most recent call last):
  
File "<stdin>", line 1, in <module>
  
NameError: name 'list2' is not defined
  
>>> list2=['a', 'bit', 'inse', 'colr', 'doll', 'esr', 'fire', 'thisisappendin']
  
>>> del list2
  
>>> list2
  
['a', 'bit', 'colr', 'doll', 'esr', 'fire', 'thisisappendin']
  list.pop(下标)
>>> list2.pop(2)   #不加位置参数,默认最后一个  
'colr'
  
>>> list2
  
['a', 'bit', 'doll', 'esr', 'fire', 'thisisappendin']
  
                list.remove('value')
  
>>> list2.remove('doll')
  
>>> list2
  
['a', 'bit', 'esr', 'fire', 'thisisappendin']
  索引
  list.index()   返回第一个匹配到的
>>> list2  
['a', 'bit', 'esr', 'fire', 'thisisappendin']
  
>>> list2.index('esr')
  
2
  list.clear()
>>> list1=  
>>> list2=['a','b','c','d','e']
  
>>> list3=list1
  
>>> list3
  

  
>>> list3.clear
  
<built-in method clear of list object at 0x7fd9f2eb04c8>
  
>>> list1
  

  list.sort() python3不支持同时有字符串和数字进行排序
  list.reverse() 翻转
  list.count("")
  list.extend(list2)    把list2的内容扩展到list1
  list.copy()   浅copy
>>> list1=  
>>> list2=list1.copy()
  
>>> list2
  

  
>>> list2
  

  
>>> list1=111
  
>>> list2
  

  
>>> del list1
  
>>> del list2
  
>>> list1=[,121,['a',1,'x'],{'x':20,'pm':15}]
  
>>> list2=list1.copy()
  
>>> list1=12580
  
>>> list1
  
[, 12580, ['a', 1, 'x'], {'pm': 15, 'x': 20}]
  
>>> list2
  
[, 121, ['a', 1, 'x'], {'pm': 15, 'x': 20}]
  
>>> list1=66
  
>>> list1
  
[, 12580, ['a', 1, 'x'], {'pm': 15, 'x': 20}]
  
>>> list2
  
[, 121, ['a', 1, 'x'], {'pm': 15, 'x': 20}]
  浅copy只拷贝一层,只是复制一层值,而当这层值是一个引用时,复制的是引用,他们指向的同一个内存对象,这个对象并没有复制,这是后,修改其中一个另外一个也跟随改变
页: [1]
查看完整版本: python的基本数据类型:列表的方法