码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-08-16 15:26:19      阅读:153      评论:0      收藏:0      [点我收藏+]

标签: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=[none]*5

    列表的元素访问

>>> list2=[‘a‘,‘bit‘,‘colr‘,‘doll‘,‘esr‘,‘fire‘]
>>> list2=[‘a‘,‘bit‘,‘colr‘,‘doll‘,‘esr‘,‘fire‘]
>>> list2[0]   #0为第一个
‘a‘
>>> list2[-1] #-1为倒数第一个
‘fire‘>>> list2[-2]
‘esr‘
>>> list2[0:] #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[2]
>>> 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=[1,2,3,4,5,6]
>>> list2=[‘a‘,‘b‘,‘c‘,‘d‘,‘e‘]
>>> list3=list1
>>> list3
[1, 2, 3, 4, 5, 6]
>>> list3.clear
<built-in method clear of list object at 0x7fd9f2eb04c8>
>>> list1
[1, 2, 7, 4, 5, 6]


        list.sort() python3不支持同时有字符串和数字进行排序

        list.reverse() 翻转

        list.count("")

        list.extend(list2)    把list2的内容扩展到list1

 

       list.copy()   浅copy

>>> list1=[1,2,3,45,6]
>>> list2=list1.copy()
>>> list2
[1, 2, 3, 45, 6]
>>> list2
[1, 2, 3, 45, 6]
>>> list1[2]=111
>>> list2
[1, 2, 3, 45, 6]
>>> del list1
>>> del list2
>>> list1=[[1,2,3,4],121,[‘a‘,1,‘x‘],{‘x‘:20,‘pm‘:15}]
>>> list2=list1.copy()
>>> list1[1]=12580
>>> list1
[[1, 2, 3, 4], 12580, [‘a‘, 1, ‘x‘], {‘pm‘: 15, ‘x‘: 20}]
>>> list2
[[1, 2, 3, 4], 121, [‘a‘, 1, ‘x‘], {‘pm‘: 15, ‘x‘: 20}]
>>> list1[0][2]=66
>>> list1
[[1, 2, 66, 4], 12580, [‘a‘, 1, ‘x‘], {‘pm‘: 15, ‘x‘: 20}]
>>> list2
[[1, 2, 66, 4], 121, [‘a‘, 1, ‘x‘], {‘pm‘: 15, ‘x‘: 20}]


浅copy只拷贝一层,只是复制一层值,而当这层值是一个引用时,复制的是引用,他们指向的同一个内存对象,这个对象并没有复制,这是后,修改其中一个另外一个也跟随改变


本文出自 “庭前夜末空看雪” 博客,请务必保留此出处http://12550795.blog.51cto.com/12540795/1956667

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

标签:python 列表方法

原文地址:http://12550795.blog.51cto.com/12540795/1956667

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!