py列表

作者: 荼蘼toome | 来源:发表于2019-12-28 17:04 被阅读0次

列表

表示方式[]
对象类型list

[]
== []

type([])
== <class 'list'>

虽然是个空的集合..依旧返回的是集合类型

赋值
a=[]
bool(a)
==false
目前为空值

单一列表
a=['hello',66.6]
a
==['hello',66.6]

混合列表
b=['hello',66.6,6,['hello',66.6,6]]
b
==['hello',66.6,6,['hello',66.6,6]]

列表里面也是索引和切片

a=['hello','world']
== hello
a[1]
==world

a[0:1]
=='hello'
左包括,右不包括

二维列表
b
==['hello',66.6,6,['hello',66.6,6]]
b[3]
==['hello',66.6,6]
b[3][0]
=='hello'

反转

list=[1,2,3,4,5,6]
list
==[1,2,3,4,5,6]

list[::-1]
== [6,5,4,3,2,1]

list
==[1,2,3,4,5,6]

list[0:4]
==[1,2,3,4]

list[0:4:1]
==[1,2,3,4]

list[0:4:2]
==[1,3]

list[4:1:-1]
==[5,4,3]

对应下标 从右往左推 反过来 左包括,右不包括

list[::-2]
[6,4,2]

追加

append()

a=[1,2,3]
b=[4,5,6]

a.append(b)
a
==[1,2,3,4,5,6]

extend()
追加迭代元素

a=[1,2,3]
b=[4,5,6]

a.extend(b)
a
==[1,2,3,4,5,6]
b
==[4,5,6]

  • 区别
    a=[1,2]
    a.append([4,5])
    a
    ==[1,2,[4,5]]

a.extend([4,5])
a
==[1,2,[4,5],4,5]

append将元素列表当对象

insert

验证对象类型是否可以迭代

a=[1,2]
hasattr(a,'_iter_')
返回true false

相关文章

网友评论

    本文标题:py列表

    本文链接:https://www.haomeiwen.com/subject/febsoctx.html