列表---[]

一、列表

  作用:多个装备,多个爱好,多门课程,多个女朋友等

  定义:[]内可以有多个任意类型的值,逗号分隔

以下是列表的常用操作:

l=[1,2,3] #l=list([1,2,3])
# print(type(l))

#pat1===》优先掌握部分
#  索引:l=[1,2,3,4,5]
      print(l[0])  7 #  切片
l=['a','b','c','d','e','f']

# print(l[1:5])
# print(l[1:5:2])
# print(l[2:5])
# print(l[-1])


#了解
# print(l[-1:-4])
# print(l[-4:])
# l=['a','b','c','d','e','f']
# print(l[-2:])

#   追加
# hobbies=['play','eat','sleep','study']
# hobbies.append('girls')
# print(hobbies)

#   删除
hobbies=['play','eat','sleep','study']
# x=hobbies.pop(1) #不是单纯的删除,是删除并且把删除的元素返回,我们可以用一个变量名去接收该返回值
# print(x)
# print(hobbies)

# x=hobbies.pop(0)
# print(x)
#
# x=hobbies.pop(0)
# print(x)

#队列:先进先出
queue_l=[]
#入队
# queue_l.append('first')
# queue_l.append('second')
# queue_l.append('third')
# print(queue_l)
#出队
# print(queue_l.pop(0))
# print(queue_l.pop(0))
# print(queue_l.pop(0))


#堆栈:先进后出,后进先出
# l=[]
# #入栈
# l.append('first')
# l.append('second')
# l.append('third')
# #出栈
# print(l)
# print(l.pop())
# print(l.pop())
# print(l.pop())

#了解
# del hobbies[1] #单纯的删除
# hobbies.remove('eat') #单纯的删除,并且是指定元素去删除


#   长度
# hobbies=['play','eat','sleep','study']
# print(len(hobbies))

#   包含in
# hobbies=['play','eat','sleep','study']
# print('sleep' in hobbies)

# msg='hello world egon'
# print('egon' in msg)


##pat2===》掌握部分
hobbies=['play','eat','sleep','study','eat','eat']
# hobbies.insert(1,'walk')
# hobbies.insert(1,['walk1','walk2','walk3'])
# print(hobbies)

# print(hobbies.count('eat'))
# print(hobbies)
# hobbies.extend(['walk1','walk2','walk3'])
# print(hobbies)

hobbies=['play','eat','sleep','study','eat','eat']
# print(hobbies.index('eat'))


#pat3===》了解部分
hobbies=['play','eat','sleep','study','eat','eat']
# hobbies.clear()
# print(hobbies)

# l=hobbies.copy()
# print(l)

# l=[1,2,3,4,5]
# l.reverse()
# print(l)

l=[100,9,-2,11,32]
l.sort(reverse=True)
print(l)

  

posted @ 2018-05-16 00:50  强神奇  阅读(169)  评论(0编辑  收藏  举报