栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python 列表

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python 列表

python 列表
    • 创建列表
    • 列表操作
    • 列表的索引
    • 列表排序
    • 列表删除
    • 列表的迭代与搜索
    • 列表的推导式

创建列表
names = [ 'Elwood', 'Jake', 'Curtis' ]
nums = [ 39, 38, 42, 65, 111]
列表操作
names.append('Murphy')    # Adds at end
names.insert(2, 'Aretha') # Inserts in middle

‘ + ’ 连接列表

s = [1, 2, 3]
t = ['a', 'b']
s + t           # [1, 2, 3, 'a', 'b']
列表的索引
names = [ 'Elwood', 'Jake', 'Curtis' ]

names[0]  # 'Elwood'
names[1]  # 'Jake'
names[2]  # 'Curtis'
列表排序
s = [10, 1, 7, 3]
s.sort()                    # [1, 3, 7, 10]

# Reverse order
s = [10, 1, 7, 3]
s.sort(reverse=True)        # [10, 7, 3, 1]


s = ['foo', 'bar', 'spam']
s.sort()                    # ['bar', 'foo', 'spam']
列表删除
names.remove('Curtis')

#或
del names[1]
列表的迭代与搜索
for name in names:
    # e.g. print(name)
    ...

列表的搜索

names = ['Elwood','Jake','Curtis']
names.index('Curtis')   # 2
列表的推导式

一般语法。

[  for  in  if ]

列表推导来自数学(集合构建符号)。

a = [ x * x for x in s if x > 0 ] # Python

a = { x^2 | x ∈ s, x > 0 }         # Math
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/948670.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号