python入门学习笔记(二)——列表

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_40301026/article/details/79959658

3.列表

3.1使用列表的值

name = ['dad','sss','ff']

print(name)

print(name[0])

print(name[-1].upper())

3.2修改列表值

name[-1] = 'aa'

print(name)

3.3添加元素

3.3.1在末尾加一个元素:append()

3.3.2在列表任意位置添加:insert()

name = ['ss',' eew ','eee']

print(name)

name.insert(1,'555')

print(name)

 

##

['ss', ' eew ', 'eee']

['ss', '555', ' eew ', 'eee']

 

3.4删除元素

3.4.1删除不返回值:del

name = ['ss',' eew ','eee']

print(name)

del name[0]

print(name)

3.4.2删除末尾元素,返回值(和出栈类似):pop()

或弹出任意位置pop(x)

name = ['ss',' eew ','eee']

print(name)

x = name.pop()

print(name)

print(x)

 

#

['ss', ' eew ', 'eee']

['ss', ' eew ']

eee

3.4.3根据值删除元素

remove()  pop()类似也可以使用其值

3.5组织列表

3.5.1对列表进行永久性排序(无法恢复到原来的顺序)

3.5.1.1按字母顺序排序:sort()

name = ["dd","cc","xx"]

name.sort()

print(name)

 

#

['cc', 'dd', 'xx']

3.5.1.2按字母相反排序:给sort()参数 reverse=True

name = ["dd","cc","xx"]

name.sort(reverse=True)

print(name)

 

#

['xx', 'dd', 'cc']

3.5.2进行临时排序:按特定顺序输出,保留列表原来排序sorted()

name = ["dd","cc","xx"]

print(sorted(name))

print(name)

 

#

['cc', 'dd', 'xx']

['dd', 'cc', 'xx']

3.5.3倒着打印列表:reverse()

name = ["dd","cc","xx"]

name.reverse()

print(name)

#

['xx', 'cc', 'dd']

3.5.4确定列表长度:len()

name = ["dd","cc","xx"]

print(len(name))

 

#

3

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页