python编程基础知识—列表(一)
1 列表
- 用[]来表示列表,并用逗号分隔其中的元素。如:
B=['trek','cannondale','redline','specialized']
print(B)
['trek', 'cannondale', 'redline', 'specialized']
1.1 访问列表元素(可指出列表的名称,再取出元素的索引,并将其放在括号内)
- 如:从B中取出第一个元素:
B=['trek','cannondale','redline','specialized']
print(B[0])
trek
- 在python中,第一个列表元素的索引为0,第二个元素索引为1,以此类推,最后一个列表元素索引为-1
- 如:找出列表B中第2个和最后一个元素:
B=['trek','cannondale','redline','specialized']
print(B[1])
print(B[-1])
cannondale
specialized
1.2 使用列表中的各个值
B=['trek','cannondale','redline','specialized']
messge = "My name is "+B[0]+'.'
print(messge)
My name is trek.
1.3 修改、添加和删除元素
1.31 修改列表元素
- 如列表B,如何修改第一个元素‘trek’?
B=['trek','cannondale','redline','specialized']
print(B)
B[0]='dau'
print(B)
['trek', 'cannondale', 'redline', 'specialized']
['dau', 'cannondale', 'redline', 'specialized']
1.32 在列表中添加元素
1.在列表末尾处添加元素
- 继续使用列表B,在其末尾处添加新元素‘cat’
B=['trek','cannondale','redline','specialized']
print(B)
B.append('cat')
print(B)
['trek', 'cannondale', 'redline', 'specialized']
['trek', 'cannondale', 'redline', 'specialized', 'cat']
- 方法append()将元素‘cat’加到了列表末尾处,而不影响列表中的其他元素
- 下来呢,我们创建一个空列表,再在其中添加元素‘honda’,‘woo’,‘hod’
A=[]
A.append('honda')
A.append('woo')
A.append('hod')
print(A) #输出最终的结果
['honda', 'woo', 'hod']
2.在列表中插入元素
- 用方法insert()可在列表中任何位置添加新元素,为此,需指定新元素的索引和值
B=['trek','cannondale','redline','specialized']
B.insert(1,'pig')##在索引1出添加空间,并将值‘pig’存储到这个地方
print(B)
['trek', 'pig', 'cannondale', 'redline', 'specialized']
1.33 从列表中删除元素
1.使用del语句删除元素
- 如果知道要删除的元素在列表中的位置,可使用del()语句
B=['trek','cannondale','redline','specialized']
print(B)
del B[0]##删除了B中第一个元素‘trek’
print(B)
['trek', 'cannondale', 'redline', 'specialized']
['cannondale', 'redline', 'specialized']
2. 使用pop方法删除元素
- pop()删除列表末尾处的元素
B=['trek','cannondale','redline','specialized']
print(B)
popped_B=B.pop()
print(B)
print(popped_B)
['trek', 'cannondale', 'redline', 'specialized']
['trek', 'cannondale', 'redline']
specialized
B=['trek','cannondale','redline','specialized']
led_B = B.pop()
print("The last was " + led_B + '.')
The last was specialized.
3. 弹出列表中任何位置处的元素
B=['trek','cannondale','redline','specialized']
first_B = B.pop(1)
print("I owned was a " + first_B + '.')
I owned was a cannondale.
- 如果你要从列表中删除一个元素,且不再以任何方式使用它,就用del语句;如果你要在删除元素后还能继续使用它,就使用方法pop()方法.
4. 根据值删除元素
- 如果只删除元素的值,可使用方法remove()
B=['trek','cannondale','redline','specialized']
print(B)
B.remove('redline')
print(B)
['trek', 'cannondale', 'redline', 'specialized']
['trek', 'cannondale', 'specialized']
B=['trek','cannondale','redline','specialized']
print(B)
tee = 'redline'
B.remove(tee)
print(B)
print("\nThe " + tee + " is too big to me. ")
['trek', 'cannondale', 'redline', 'specialized']
['trek', 'cannondale', 'specialized']
The redline is too big to me.
2 组织列表
2.1 使用方法sort()对列表进行永久性排序
- 按字母顺序进行排序
B=['trek','cannondale','redline','specialized']
B.sort()
print(B)
['cannondale', 'redline', 'specialized', 'trek']
- 按相反方向进行排序,只需向sort()方法传递参数reverse=True
B=['trek','cannondale','redline','specialized']
B.sort(reverse = True)
print(B)
['trek', 'specialized', 'redline', 'cannondale']
2.2 使用函数sorted()对列表进行临时排序
- sorted()可按特定顺序显示列表元素,同时不影响它们在列表中的原始排列顺序
cars = ['bmw','audi','toyota','Jeep']
print("here is the original list: ")
print(cars)
print("\nHere is the sorted list:")
print(sorted(cars))
print("\nHere is the original list again:")
print(cars)
here is the original list:
['bmw', 'audi', 'toyota', 'Jeep']
Here is the sorted list:
['Jeep', 'audi', 'bmw', 'toyota']
Here is the original list again:
['bmw', 'audi', 'toyota', 'Jeep']
2.3 倒着打印列表
- 使用reverse()方法
cars = ['bmw','audi','toyota','Jeep']
print(cars)
cars.reverse()
print(cars)
['bmw', 'audi', 'toyota', 'Jeep']
['Jeep', 'toyota', 'audi', 'bmw']
2.4 确定列表的长度
- 使用len()获取cars = ['bmw','audi','toyota','Jeep']
cars = ['bmw','audi','toyota','Jeep']
len(cars)
未完待续!