iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >python list
  • 443
分享到

python list

pythonlist 2023-01-31 01:01:22 443人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

同属于一个列表的数据,可以是不同的类型特色:存储于用一个列表的数据都是以数字来作为索引的,即作为操作存取其中各个元素的依据。a_list0 1 2 3 4int int int int int1 3 5 7 9索引分别为 0,1,2,3,4

同属于一个列表的数据,可以是不同的类型
特色:存储于用一个列表的数据都是以数字来作为索引的,即作为操作存取其中各个元素的依据。
a_list
0 1 2 3 4
int int int int int
1 3 5 7 9
索引分别为 0,1,2,3,4
每个元素可有自已的类型,均为int,内容分别是
1、3、5、7、9
a_list = [ 1,3,5,7,9 ]
数字列表
\>>> a_list=[1,3,5,7,9]
\>>> a_list
[1, 3, 5, 7, 9]
\>>> a_list[0]
1
字符串列表
\>>> str_list=['P','y','t','h','o','n']
\>>> str_list
['P', 'y', 't', 'h', 'o', 'n']
\>>> str_list[2]
't'
字符串split 方法
\>>> str_msg="I Love Pyton"
\>>> b_list=str_msg.split()
\>>> b_list
['I', 'Love', 'Pyton']
一个英文句子拆成字母所组成的列表,用list() 函数,
\>>> str_msg="I Love Pyton"
\>>> c_list=list(str_msg)
\>>> c_list
['I', ' ', 'L', 'o', 'v', 'e', ' ', 'P', 'y', 't', 'o', 'n']
\>>>
同一个列表中可以用不同的数据类型,列表中也可以有其他的列表
\>>> k1=['book',10]
\>>> k2=['campus',15]
\>>> k3=['cook',9]
\>>> k4=['python',26]
\>>> keyWords=[k1,k2,k3,k4]
\>>> keywords
[['book', 10], ['campus', 15], ['cook', 9], ['Python', 26]]
\>>> keywords[2]
['cook', 9]
\>>> keywords[2][0]
'cook'
\>>> keywords[2][1]
9
\>>>
可以使用”+“运算把两个列表放在一起,还可以 检测某一个数据是否在列表之中
\>>> "Python" in k4
True
\>>> k4 in keywords
True
\>>> ["Python",26] in keywords
True
\>>> keywords+k1+k2
[['book', 10], ['campus', 15], ['cook', 9], ['Python', 26], 'book', 10, 'campus', 15]
\>>>

比较常用的操作方法和函数

列表表达式 操作结果说明
lst * n 把lst类表重复n次
lst[n1:n2] 把索引组n1到n2的列表内容取出,组成一个列表
lst[n1:n2:k] 同上,但取出间隔为k
del lst[n1:n2] 删除索引值n1到n2之间的元素
lst[n1:n2]=n 把索引值n1到n2之间的元素设置为n
lst[n1:n2:k]=n 同上,但间隔为k
del lst[n1:n2:k] 删除索引值n1到n2之间的元素,但间隔为k
len(lst) 放回类表的个数
min(lst) 返回列表的最小值
max(lst) 返回列表的最大值
sum(lst) 返回列表的求和值
lst.index(n) 返回列表中第一个出现n的索引值
lst.count(n) 计算出n 在列表中出现的次数

\>>> x=list(range(10))
\>>> x
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
\>>> x*2
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
\>>> y=x[1:7]
\>>> y
[1, 2, 3, 4, 5, 6]
\>>> y=x[1:7:2]
\>>> y
[1, 3, 5]
\>>> del x[1:7]
\>>> x
[0, 7, 8, 9]
\>>> x=list(range(10))
\>>> x
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
\>>> del x[1:7:2]
\>>> x
[0, 2, 4, 6, 7, 8, 9]
\>>> x=list(range(10))
\>>> x
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
\>>> len(x)
10
\>>> max(x)
9
\>>> min(x)
0
\>>> x=list(range(3))
\>>> x
[0, 1, 2]
\>>> sum(x)
3
\>>> msg="Here is the string"
\>>> lst=list(msg)
\>>> lst
['H', 'e', 'r', 'e', ' ', 'i', 's', ' ', 't', 'h', 'e', ' ', 's', 't', 'r', 'i', 'n', 'g']
\>>> lst.index('e')
1
\>>> lst.count('e')
3
\>>>

列表操作方法:
(其中lst表示列表变量,x表示列表元素或是另外一个列表变量,n为数值)
lst.append(x) 将x视为一个元素,附加到列表的后面
lst.extend(x) 将x 中的所有元素附加到列表的后面
lst.insert(n,x) 把x插入到索引值为n的地方
lst.pop() 弹出列表中最后一个元素,可以参数指定特定的索引
lst.remove(x) 从列表中删除第一个出现的x
lst.reverse() 反转列表的顺序
lst.sort() 将列表的元素内容加以排序

append 与 extend 区别
\>>> lsta=[1,2,3,4,5]
\>>> exb=[5,5,5,5]
\>>> lsta.extend(exb)
\>>> lsta
[1, 2, 3, 4, 5, 5, 5, 5, 5]
\>>> lsta=[1,2,3,4,5]
\>>> lsta.append(exb)
\>>> lsta
[1, 2, 3, 4, 5, [5, 5, 5, 5]]
pop操作
\>>> lst=[0,1,2,3,4,5]
\>>> lst.append(9)
\>>> lst.append('x')
\>>> lst
[0, 1, 2, 3, 4, 5, 9, 'x']
\>>> lst.pop()
'x'
\>>> lst.pop(2)
2
\>>> lst
[0, 1, 3, 4, 5, 9]
\>>>
\>>> cars=['bwm','audi','toyota','subaru']
\>>> cars.sort()
\>>> cars
['audi', 'bwm', 'subaru', 'toyota']
\>>> cars.reverse()
\>>> cars
['toyota', 'subaru', 'bwm', 'audi']
\>>>
\>>> cars.remove('bwm') 用于不知道具体的索引位置,只知道具体的值
\>>> cars
['toyota', 'subaru', 'audi']
\>>> cars.insert(1,'haima') 指定位置插入
\>>> cars
['toyota', 'haima', 'subaru', 'audi']
\>>>

\>>> cars
['toyota', 'haima', 'subaru', 'audi']
\>>> for x in cars:
... print x
...
toyota
haima
subaru
audi
\>>> for x in cars:
... print x.title()
...
Toyota
Haima
Subaru
Audi
\>>>
通过循环创建所需列表
\>>> atest=[]
\>>> for value in range(1,11,2):
... a=value**2
... atest.append(a)
...
\>>> print atest
[1, 9, 25, 49, 81]

\>>> atest2=[value**2 for value in range(1,11,2)]
\>>> print atest2
[1, 9, 25, 49, 81]
\>>>

\>>> list_2d=[[0 for i in range(5)]for i in range(5)]
\>>> list_2d
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
\>>> list_2d[0].append(3)
\>>> list_2d[2].append(7)
\>>> list_2d
[[0, 0, 0, 0, 0, 3], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 7], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
\>>>

--结束END--

本文标题: python list

本文链接: https://www.lsjlt.com/news/184031.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • python list
    同属于一个列表的数据,可以是不同的类型特色:存储于用一个列表的数据都是以数字来作为索引的,即作为操作存取其中各个元素的依据。a_list0 1 2 3 4int int int int int1 3 5 7 9索引分别为 0,1,2,3,4...
    99+
    2023-01-31
    python list
  • Python -- list 类
    Python list类常用方法class list(object):        def append(self, p_object): # 向列表中添加元素; >>> name_list ['shuoming', '...
    99+
    2023-01-31
    Python list
  • python 列表(List)
    Python内置的一种数据类型是列表:list。list是一种有序的集合,可以随时添加和删除其中的元素。每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推。可以进行的操作包括索引,切片,加,乘,检查成员。...
    99+
    2023-01-30
    列表 python List
  • python list unicode转
    在从元组转换到字符串时,中文字符前会出现u'例子'类似这种,具体可参考第二个文章,需要手动去除u一切就都正常了all_symptom内容[u'\u773c', u'\u8179\u90e8', u'\u4e94\u5b98', u'\u53...
    99+
    2023-01-31
    python list unicode
  • Python list 拼接
    python合并list有几种方法: 1 .append() 向列表尾部追加一个新元素,列表只占一个索引位,在原有列表上增加 2 .extend() 向列表尾部追加一个列表,将列表中的每个元素都追加进来,在原有列表上增加 ...
    99+
    2023-01-31
    Python list
  • python---list()用法
    list列表以后再继续完善help(list) 使用help查看listHelp on class list in module __builtin__:class list(object) |  list() -> new empt...
    99+
    2023-01-31
    python list
  • Python list列表
    一:列表: list列表的定义:1,列表是由一系列元素组成,元素与元素之间可能没有任何的关联关系,但他们之间有先后顺序关系。2,列表是一种容器3,列表是一种序列4,列表是可以改变的序列python3 中的序列有如下几种:     字符串 s...
    99+
    2023-01-31
    列表 Python list
  • python list tuple d
    很意外,我的博客居然有人访问,还有一位仁兄来评价,点赞,莫名感激  一 list          name = ["aaa","bbb","ccc","ddd","eee","fff","ggg","hhh","iii"]        ...
    99+
    2023-01-31
    python list tuple
  • python list排序
    python 列表list中内置了一个十分有用的排序函数sort,sorted,它可以用于列表的排序,以下是例子。a = [5,2,1,9,6]         >>> sorted(a)                 ...
    99+
    2023-01-31
    python list
  • Python基础(list类)
    三、列表(list类)提示:以下所有方法都是类中的方法,第一个参数都是self,统一都没有写出。包含的方法有:1、append(x) #将x添加到List末尾。>>>list=['a'] >>>list....
    99+
    2023-01-31
    基础 Python list
  • Python学习之List
    **Python的列表可以存儲任何數據類型,包括列表本身。它是通過索引訪問。**Python的索引值是從0開始的,以些類推**Python是通過中括號來包含所有的值,值與值之間通過","號分隔**查看python列表的擁有哪些方法,可以通過...
    99+
    2023-01-31
    Python List
  • Python中list总结
    1:列表 list的定义:一个连续的,排列有序的数列,由若干个元素组成,元素可以是任意对象(数字、字符串,对象,列表),元素可以使用索引查找,线性的数据结构。使用[ ]表示。列表是可变的,是可迭代对象。 列表具体定义l=[ ] (空列表) ...
    99+
    2023-01-31
    Python list
  • Python之路(四)--->list、
      好久没有更新了,感觉自己写的东西并没有太多人看,可能是因为写的不好,也可能是太基础了。学习是一个漫长的过程,结果结果固然重要,但是更重要的是在学习的过程中所学到方法,这些方法在以后的生活还是工作中都能给予你很大的帮助。同时,学习也是一...
    99+
    2023-01-30
    之路 Python list
  • Python list遍历remove(
    有这样一个列表: s=list('abcdefg')  现在因为某种原因我们需要从s中踢出一些不需要的元素,方便起见这里直接以踢出所有元素的循环代替: for e in s: s.remove(e)  结果却是: I...
    99+
    2023-01-31
    遍历 Python list
  • Python 中list ,set,di
    很多时候我们可能要频繁的进行元素的find 或in操作,本人一直天真的以为python的list做了hash,通过红黑树来高效查找···直到今天我真正来测试它和set,dict的查找效率时,才发现自已想太多了!!!! 先看代码:...
    99+
    2023-01-31
    list Python di
  • python list 取倒序
     aa=[1,2,3,4,5,] >>> aaa=aa[::-1] >>> aaa[5, 4, 3, 2, 1]...
    99+
    2023-01-31
    python list 取倒序
  • python的list、tuple、di
    listtupledictset特点有序、查找速度随着元素增加而逐渐下有序\不能修改无序、查找速度快、key不能重复元素没有重复、无序、判断一个元素是否在set中速度很快创建L =['Michael', 'Bob', 'Tracy']t =...
    99+
    2023-01-31
    list python di
  • Python list初始化
    1、基本方法。lst = [1, 2, 3, 4, 5]2、初始化连续数字。>>> lst = [n for n in range(5, 10)] >>> print(lst) [5, 6, 7, 8, ...
    99+
    2023-01-31
    初始化 Python list
  • python的dict,set,list
    字典(dict)dict 用 {} 包围 dict.keys(),dict.values(),dict.items() hash(obj)返回obj的哈希值,如果返回表示可以作为dict的key del 或 dict.pop可以删除一个it...
    99+
    2023-01-31
    dict python list
  • Python-List、Tuple类型
    List集合类型 Python内置的一种数据类型: list 。list是一种有序的集合,可以添加或删除其中元素。构造 list 使用中括号 [ ] ,用 [ ] 把list所有元素括起来,就是一个list对象,通常会把list赋值给一个...
    99+
    2023-01-31
    类型 Python List
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作