Python-列表

列表中的每一个元素都是可变的。

这意味着我们可以在列表中添加、删除、修改元素。

 

列表中的元素是有序的。

Weekday = ['Monday','Tuesday','Wednesday','Thursday','Friday']
print(Weekday[0])
Monday

 

列表可以装Python中的任何对象 。

all_in_list = [

  1,

  1.2,

  ‘a word’,

  True,

  [1,2],

  (1,2),

  {'key':'value'}

]

 

列表的增删改查

增:

fruit = ['pineapple','pear']
fruit.insert(1,'grape')  #指定插入元素位置为1,如果指定位置超出列表长度,那么插入的元素放在列表最后
print(fruit)

week=['Monday','Tuesday','Wednesday']

week.append('Thursday')  #append方法是把元素添加在最后

print(week)

a=[]
a.extend([1,2,3])  #extend是将新对象的元素逐一加在列表末尾。append是将整个对象加在列表末尾
print(a)

删:

fruit = ['pinapple','pear','grape']
fruit.remove('grape')
print(fruit)

fruit ['pinapple','pear','grape']
del fruit[0]
print(fruit)

 

改:

fruit ['pinapple','pear','grape']

 

fruit[0]='apple'

print(fruit)

查:

使用切片

name_list = ["zhangyanlin","suoning","nick"]

print(name_list.index('nick'))  #index找到字符所在的位置
posted @ 2023-01-09 11:58  昵称不重要!  阅读(17)  评论(0)    收藏  举报