python学习——python编程:从入门到实践(3章)

3.1列表([ ]表示)数字or字母or姓名ect.任何元素都可以加入列表中,元素之间可以没有任何关系

bicycles = ['trek','cannondale','redline']
print(bicycles)

['trek', 'cannondale', 'redline']
访问列表元素(列表名称[元素索引])
bicycles = ['trek','cannondale','redline']
print(bicycles[0])

trek
输出的结果更加整洁可使用title()函数 (列表名称[元素索引].title())
print(bicycles[0].title())
Trek

元素索引从0开始而不是1,元素的最后一个指定为-1,倒数第二个元素-2.以此类推,在不知道列表长度的情况下可以访问最后的元素
bicycles = ['trek','cannondale','redline']
print(bicycles[-1].title())
print(bicycles[0].title())
print(bicycles[1].title())

Redline
Trek
Cannondale

使用列表的值各个值
bicycles = ['trek','cannondale','redline']
message = "this is a new "+bicycles[0].title() + "."
print(message)
this is a new Trek.

作业:
3-1 姓名:  将一些朋友的姓名存储在一个列表中,并将其命名为names 。依次访问该列表中的每个元素,从而将每个朋友的姓名都打印出来。
names = ["Bell","Peter","Alice","Ben"]
print(names[0])
print(names[1])
print(names[2])
print(names[3])
Bell
Peter
Alice
Ben


3-2 问候语:  继续使用练习3-1中的列表,但不打印每个朋友的姓名,而为每人打印一条消息。每条消息都包含相同的问候语,但抬头为相应朋友的姓名。
names = ["Bell","Peter","Alice","Ben"]
message = names[0]+ ", nice to meet you! "
print(message)
message = names[1]+ ", nice to meet you! "
print(message)
message = names[2]+ ", nice to meet you! "
print(message)
message = names[3]+ ", nice to meet you! "
print(message)
Peter, nice to meet you!
Alice, nice to meet you!
Ben, nice to meet you!

3-3 自己的列表:  想想你喜欢的通勤方式,如骑摩托车或开汽车,并创建一个包含多种通勤方式的列表。根据该列表打印一系列有关这些通勤方式的宣言,如“I would like to own a Honda motorcycle”。
ways = ["bike","on foot","train"]
sayings = "I would like to own a Honda "+ ways[0]
print(sayings)
sayings = "I like "+ ways[1].title()
print(sayings)
sayings = "The "+ ways[-1] + " is fast!"
print(sayings)

I would like to own a Honda bike
I like  On Foot
The train is fast!
3.2 修改,添加和删除元素

修改列表元素
ways = ["bike","on foot","train"]
print(ways)
ways[0]="plane"
print(ways)
['bike', 'on foot', 'train']
['plane', 'on foot', 'train']

添加元素

列表末尾添加
ways = ["bike","on foot","train"]
ways.append('plane')
print(ways)


['bike', 'on foot', 'train', 'plane']
ways = []
ways.append('plane')
ways.append('on foot')
ways.append('bike')
ways.append('train')
print(ways)

['plane', 'on foot', 'bike', 'train']

列表中插入元素

insert(元素索引,元素)
ways = ["bike","on foot","train"]
print(ways)
ways.insert(0,'plane')
print(ways)

['bike', 'on foot', 'train']
['plane', 'bike', 'on foot', 'train']


从列表中删除元素
del语句
ways = ["bike","on foot","train"]
print(ways)
del ways[0]
print(ways)

['bike', 'on foot', 'train']
['on foot', 'train']


pop()函数 pop(弹出):列表就像一个栈,而删除列表末尾的元素相当于弹出栈顶元素,按时间存储pop()打印最后的

ways = ["bike","on foot","train"]
print(ways)
poped_ways = ways.pop()
print(ways)
print(poped_ways)

['bike', 'on foot', 'train']
['bike', 'on foot']
train

根据值删除元素
remove() 这个函数有啥子用呢?当你不知道要删除的元素的位置,只知道要删除的元素的值
ways = ["bike","on foot","train"]
ways.remove("train")
print(ways)

['bike', 'on foot']

amazing!!!!当我们将值从列表中删除时,还可以接着使用值
ways = ["bike","on foot","train"]
print(ways)
ways.remove("train")
too_fast = "train"
print(ways)
print("The " + too_fast +" is fast.")

['bike', 'on foot', 'train']
['bike', 'on foot']
The train is fast.

元素从列表中删除时,但是它还储存在too_fast 中

注意啦:::remove()只能删除第一个指定的值
ways = ["bike","on foot","train","train"]
print(ways)
ways.remove("train")
print(ways)

['bike', 'on foot', 'train', 'train']
['bike', 'on foot', 'train']


组织列表
永久性排序
sort(),按字母顺序排序,永久性修改,无法恢复
ways = ["bike","on foot","train","car"]
ways.sort()
print(ways)

['bike', 'car', 'on foot', 'train']

与字母顺序相反的排序,久性修改,无法恢复
ways.sort(reverse=True) 向sort()传递reverse=True
ways = ["bike","on foot","train","car"]
ways.sort(reverse=True)
print(ways)

临时排序

sorted()函数能按特定的顺序显示元素,不影响它们在列表中的原始排序

ways = ["bike","on foot","train","car"]
print("Here are the ways:")
print(ways)
print("\nHere are the ways:")
print(sorted(ways))
print("\nHere are the ways:")
print(ways)

Here are the ways:
['bike', 'on foot', 'train', 'car']
Here are the ways:
['bike', 'car', 'on foot', 'train']
 
Here are the ways:
['bike', 'on foot', 'train', 'car']
 
 与字母顺序相反,向sorted()传递reverse=True
 
ways = ["bike","on foot","train","car"]
print("Here are the ways:")
print(ways)
print("\nHere are the ways:")
print(sorted(ways,reverse=True))
print("\nHere are the ways:")
print(ways)

Here are the ways:
['bike', 'on foot', 'train', 'car']
Here are the ways:
['train', 'on foot', 'car', 'bike']
Here are the ways:
['bike', 'on foot', 'train', 'car']
 
倒着打印列表
reverse()只是反转列表元素序列,永久改变了元素顺序,但可恢复,需对列表再次调用reverse()
ways = ["bike","on foot","train","car"]
print(ways)
ways.reverse()
print(ways)

['bike', 'on foot', 'train', 'car']
['car', 'train', 'on foot', 'bike']

确定列表的长度
len()函数,列表的长度
ways = ["bike","on foot","train","car"]
print(len(ways))
4
 
 
 


猜你喜欢

转载自www.cnblogs.com/ljx12579/p/12535901.html