Python基本数据类型(列表)

基本数据类型

三、列表

列表(List)是一个有序的Python对象序列。

1.列表格式

列表可以用一对中括号“[ ]”生成,中间的元素用逗号“,”隔开:

li = [1,2,"alex"]

2.列表的运算

列表与字符串类似,支持相加和数乘。

列表相加,相当于将这两个列表按顺序连接:

li = [1,2,3] + ["alex",5]
print(li)     #结果为:[1,2,3,"alex",5]

列表数乘,相当于将这个列表重复多次:

li = [1,2,"alex"] * 2
print(li)     #结果为:[1, 2, ‘alex‘, 1, 2, ‘alex‘]

3.索引和切片

列表是个有序的序列,因此也支持索引和切片的操作。

索引:

li = [1,2,3,4,"alex"]
print(li[0])        #结果为:1
print(li[-1])        #结果为:alex

切片:

li = [1,2,3,4,"alex"]
print(li[2:-1])      #结果为:[3,4]

与字符串不同,我们可以通过索引和切片来修改列表。

4.元素的删除

Python提供了一种更通用的删除列表元素的方法:关键字del。

li = [1,2,3,4,"alex"]
del li[1]
print(li)      #结果为:[1, 3, 4, ‘alex‘]
del li[1:-1]
print(li)      #结果为:[1, ‘alex‘]

5.从属关系的判断

可以用关键字in和not in判断某个元素是否在某个序列中。例如:对于列表

li = [1,2,3,4,"alex"]
s = 2 in li
print(s)     #结果为:True

6.列表的方法

列表一些常用的方法。

(1).append()方法

.append()方法向列表追加单个元素。

li = [1,2,3,4,"alex"]
li.append(5)
print(li)      #结果为:[1, 2, 3, 4, ‘alex‘, 5]

(2).extend方法

.extend方法将另一个序列中的元素依次添加到列表。

li = [1,2,3,4,"alex"]
li.extend([5,8])
print(li)      #结果为:[1, 2, 3, 4, ‘alex‘, 5, 8]

(3).insert方法

.insert方法在指定索引位置插入元素。

li = [1,2,3,4,"alex"]
li.insert(2,"hello")
print(li)     #结果为:[1, 2, ‘hello‘, 3, 4, ‘alex‘]

(4).remove方法

.remove方法将列表中第一个出现的指定元素删除,指定元素不存在会报错。

li = [1,2,3,4,"alex"]
li.remove(3)
print(li)     #结果为:[1, 2, 4, ‘alex‘]

(5).pop方法

.pop方法将列表中指定索引的元素删除,默认最后一个元素,并返回这个元素值。

li = [1,2,3,4,"alex"]
a = li.pop(3)
print(li)     #结果为:[1, 2, 3, ‘alex‘]
print(a)     #结果为:4

(6).sort方法

.sort方法将列表中的元素按照从小到大排序。

li = [3,6,1,5,4]
li.sort()
print(li)     #结果为:[1, 3, 4, 5, 6]

可以在调用时加入一个reverse参数,如果它等于True,列表会按照从大到小进行排序:

li = [3,6,1,5,4]
li.sort(reverse=True)
print(li)      #结果为:[6, 5, 4, 3, 1]

(7).reverse()方法

.reverse()方法将列表中的元素进行翻转。

li = [3,6,1,5,4]
li.reverse()
print(li)      #结果为:[4, 5, 1, 6, 3]

(8).count方法

.count方法是计算列表的指定元素出现的次数。

li = [3,6,1,5,4,6]
print(li.count(6))    #结果为:2

(9).index方法

.index方法是获取指定元素第一次出现的索引位置。

li = [3,6,1,5,4,6]
print(li.index(6))    #结果为:1

列表所有方法归纳:

  1 class list(object):
  2     """
  3     list() -> new empty list
  4     list(iterable) -> new list initialized from iterable‘s items
  5     """
  6     def append(self, p_object): # real signature unknown; restored from __doc__
  7         """ L.append(object) -- append object to end """
  8         pass
  9
 10     def count(self, value): # real signature unknown; restored from __doc__
 11         """ L.count(value) -> integer -- return number of occurrences of value """
 12         return 0
 13
 14     def extend(self, iterable): # real signature unknown; restored from __doc__
 15         """ L.extend(iterable) -- extend list by appending elements from the iterable """
 16         pass
 17
 18     def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__
 19         """
 20         L.index(value, [start, [stop]]) -> integer -- return first index of value.
 21         Raises ValueError if the value is not present.
 22         """
 23         return 0
 24
 25     def insert(self, index, p_object): # real signature unknown; restored from __doc__
 26         """ L.insert(index, object) -- insert object before index """
 27         pass
 28
 29     def pop(self, index=None): # real signature unknown; restored from __doc__
 30         """
 31         L.pop([index]) -> item -- remove and return item at index (default last).
 32         Raises IndexError if list is empty or index is out of range.
 33         """
 34         pass
 35
 36     def remove(self, value): # real signature unknown; restored from __doc__
 37         """
 38         L.remove(value) -- remove first occurrence of value.
 39         Raises ValueError if the value is not present.
 40         """
 41         pass
 42
 43     def reverse(self): # real signature unknown; restored from __doc__
 44         """ L.reverse() -- reverse *IN PLACE* """
 45         pass
 46
 47     def sort(self, cmp=None, key=None, reverse=False): # real signature unknown; restored from __doc__
 48         """
 49         L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;
 50         cmp(x, y) -> -1, 0, 1
 51         """
 52         pass
 53
 54     def __add__(self, y): # real signature unknown; restored from __doc__
 55         """ x.__add__(y) <==> x+y """
 56         pass
 57
 58     def __contains__(self, y): # real signature unknown; restored from __doc__
 59         """ x.__contains__(y) <==> y in x """
 60         pass
 61
 62     def __delitem__(self, y): # real signature unknown; restored from __doc__
 63         """ x.__delitem__(y) <==> del x[y] """
 64         pass
 65
 66     def __delslice__(self, i, j): # real signature unknown; restored from __doc__
 67         """
 68         x.__delslice__(i, j) <==> del x[i:j]
 69
 70                    Use of negative indices is not supported.
 71         """
 72         pass
 73
 74     def __eq__(self, y): # real signature unknown; restored from __doc__
 75         """ x.__eq__(y) <==> x==y """
 76         pass
 77
 78     def __getattribute__(self, name): # real signature unknown; restored from __doc__
 79         """ x.__getattribute__(‘name‘) <==> x.name """
 80         pass
 81
 82     def __getitem__(self, y): # real signature unknown; restored from __doc__
 83         """ x.__getitem__(y) <==> x[y] """
 84         pass
 85
 86     def __getslice__(self, i, j): # real signature unknown; restored from __doc__
 87         """
 88         x.__getslice__(i, j) <==> x[i:j]
 89
 90                    Use of negative indices is not supported.
 91         """
 92         pass
 93
 94     def __ge__(self, y): # real signature unknown; restored from __doc__
 95         """ x.__ge__(y) <==> x>=y """
 96         pass
 97
 98     def __gt__(self, y): # real signature unknown; restored from __doc__
 99         """ x.__gt__(y) <==> x>y """
100         pass
101
102     def __iadd__(self, y): # real signature unknown; restored from __doc__
103         """ x.__iadd__(y) <==> x+=y """
104         pass
105
106     def __imul__(self, y): # real signature unknown; restored from __doc__
107         """ x.__imul__(y) <==> x*=y """
108         pass
109
110     def __init__(self, seq=()): # known special case of list.__init__
111         """
112         list() -> new empty list
113         list(iterable) -> new list initialized from iterable‘s items
114         # (copied from class doc)
115         """
116         pass
117
118     def __iter__(self): # real signature unknown; restored from __doc__
119         """ x.__iter__() <==> iter(x) """
120         pass
121
122     def __len__(self): # real signature unknown; restored from __doc__
123         """ x.__len__() <==> len(x) """
124         pass
125
126     def __le__(self, y): # real signature unknown; restored from __doc__
127         """ x.__le__(y) <==> x<=y """
128         pass
129
130     def __lt__(self, y): # real signature unknown; restored from __doc__
131         """ x.__lt__(y) <==> x<y """
132         pass
133
134     def __mul__(self, n): # real signature unknown; restored from __doc__
135         """ x.__mul__(n) <==> x*n """
136         pass
137
138     @staticmethod # known case of __new__
139     def __new__(S, *more): # real signature unknown; restored from __doc__
140         """ T.__new__(S, ...) -> a new object with type S, a subtype of T """
141         pass
142
143     def __ne__(self, y): # real signature unknown; restored from __doc__
144         """ x.__ne__(y) <==> x!=y """
145         pass
146
147     def __repr__(self): # real signature unknown; restored from __doc__
148         """ x.__repr__() <==> repr(x) """
149         pass
150
151     def __reversed__(self): # real signature unknown; restored from __doc__
152         """ L.__reversed__() -- return a reverse iterator over the list """
153         pass
154
155     def __rmul__(self, n): # real signature unknown; restored from __doc__
156         """ x.__rmul__(n) <==> n*x """
157         pass
158
159     def __setitem__(self, i, y): # real signature unknown; restored from __doc__
160         """ x.__setitem__(i, y) <==> x[i]=y """
161         pass
162
163     def __setslice__(self, i, j, y): # real signature unknown; restored from __doc__
164         """
165         x.__setslice__(i, j, y) <==> x[i:j]=y
166
167                    Use  of negative indices is not supported.
168         """
169         pass
170
171     def __sizeof__(self): # real signature unknown; restored from __doc__
172         """ L.__sizeof__() -- size of L in memory, in bytes """
173         pass
174
175     __hash__ = None
176
177 list

list

原文地址:https://www.cnblogs.com/lzc69/p/11032430.html

时间: 2024-07-29 11:17:22

Python基本数据类型(列表)的相关文章

python学习---数据类型---列表

Python学习 1.列表 [] 1.1)列表是处理一组有序项目的数据结构,即你可以在一个列表中存储一个序列的项目. 1.2)列表时可变数据类型 1.3)列表的组成:用[]标示列表,包含多个用逗号隔开的数字或者字符串 举例:    list[1,2,3]        list1['aaa',123,'"qwsx"] 空列表 list[] 注意:在定义时,元祖只有一个值时,要在其后面加逗号:而列表只有一个值时不用加任何符号 2. 列表的操作 2.1)列表的重新赋值   (以list1[

9 Python基本数据类型---列表

1 列表的定义和创建 定义:[]内以逗号分隔,按照索引,存放各种数据类型,每个位置代表一个元素 列表的创建 #方法一 L1 = [] #定义空列表 L2 = ['a','b','c','d'] #存4个值,索引为0-3 L3 = ['abc',['def','ghi']] #嵌套列表 #方法二 L4 = list() 2 列表的特点和常用操作 特性: 有序.可变 常用操作: #1.创建 #方法一 L1 = [] #定义空列表 L2 = ['a','b','c','d'] #存4个值,索引为0-3

python常用数据类型-列表

一.列表 方括号[]创建列表 二.防护列表中的值 通过下标索引来访问列表中的值,与字符串的索引一样,列表索引从0开始.列表可以进行截取.组合等. 举例: stus = [ '王端震','刘欣雨','单宝梁' ]#对应的下标是0 1 2print (stus[0])#打印王端震#查看变量类型 用type print( type(stus))#返回list #增加 stus.append('周伊凡') #在列表末尾增加一个元素 stus.insert(0,'聂磊') #在指定位置添加一个元素 # 删

python 高级数据类型(列表 元祖 字典 字符串)

高级变量类型 目标 列表 元组 字典 字符串 公共方法 变量高级 知识点回顾 Python 中数据类型可以分为 数字型 和 非数字型 数字型 整型 (int) 浮点型(float) 布尔型(bool) 真 True 非 0 数 -- 非零即真 假 False 0 复数型 (complex) 主要用于科学计算,例如:平面场问题.波动问题.电感电容等问题 非数字型 字符串 列表 元组 字典 在 Python 中,所有 非数字型变量 都支持以下特点: 都是一个 序列 sequence,也可以理解为 容

python基本数据类型之列表和元组(一)

python基本数据类型之列表与元组(一) python中list与tuple都是可以遍历类型.不同的是,list是可以修改的,而元组属于不可变类型,不能修改. 列表和元组中的元素可以是任意类型,并且同一个列表和元组中可以包含多种类型的元素. list中有很多内置方法,元组由于不能修改,所以只有两个方法(count.index). 列表的内置方法 list的内置方法有:append.clear.copy.count.extend.index.insert.pop.remove和sort. 1.添

python数据类型-列表创建和操作

列表创建和操作 a)? 创建列表 b)? 基本操作 c)? 遍历 与其说? 列表? 它是一个数据类型,用起来 更像一个灵活多变的数据存储方案 ? 创建列表 创建列表例子 player?=?'mao?80?50' ? mao?=?'100?60?0' ? zou?=?'100?100?100' ? player1?=?['mao',100,50] ? mao?=?[100,60,0] ? type?(mao) list ? list1?=?[] ? type(list1) list 表达含义 先定

python常用数据类型内置方法介绍

熟练掌握python常用数据类型内置方法是每个初学者必须具备的内功. 一.整型 a = 100 a.xxx() class int(object): def bit_length(self): ##如果将某个整数用2进制表示,返回这个2进制所占bit位数. return 0 def conjugate(self, *args, **kwargs): ##共轭复数 @classmethod # known case def from_bytes(cls, bytes, byteorder, *ar

python的数据类型

Python的数据类型包括以下几种: 1.整数型--int 比如1,2,3,这些我们数学上常用的整数,都是整数 还包括负整数,但是不包括小数 >>>a=8 >>>type(a) <class 'int'> 2.长整数--long 32位系统上是2**31-1,64位系统上是2**63 -1,超出位数,python会转用高精度去计算,可以是无限大的整 版本2.7.11以上,如果整数超出位数的话会自动转为长整数,以前是在整数后面加个小写或者大写的l #py2.7

python核心数据类型笔记

在这里,首先声明,python核心数据类型在这里就认为是python内置的数据类型 在python中.序列类型包含字符串,列表和元组 字符串: 字符串字面量:将文本引入单引号,双引号,三引号 默认的编码类型是字符编码(8bit) 在python2中,如果要使用unicode编码(16bit),需在定义字符串的引号前面加u 在python中,有文档字符串的概念,所谓文档字符串就是在模块,类或则是函数中的第一条语句是一个字符的话(用引号定义),那么该字符就是文档字符串,可以使用__doc__属性引用