2018-07-25 Chapter 3 About List

Chapter 3 List

Create

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

Found

print('bicycles[0] = ' + bicycles[0])

Assignment

bicycles[0] = 'trek2'
print(bicycles)

Append 附加到List末尾

bicycles.append('specialized2')
print(bicycles)

Found Last 查询倒数第2个元素

print('bicycles[-2] = ' + bicycles[-2])

Insert

bicycles.insert(0, 'trek3')
print(bicycles)

bicycles.insert(3, 'trek4')
print(bicycles)

Delete 删除

del bicycles[3]
print(bicycles)

Pop 出栈

popped_m = bicycles.pop()
print(bicycles)
print(popped_m)

出栈某个位置的元素
popped_m = bicycles.pop(3)
print(bicycles)
print(popped_m)

Remove 移除某个值的元素

bicycles.remove('cannondale')
print(bicycles)

Sort 永久性a-z排序

cars = ['bbb', 'aaa', 'ttttt', 'ssss']
cars.sort();
print(cars)

z-a倒序
cars.sort(reverse = True);
print(cars)
print('\n')

Sorted 临时排序

cars = ['bbb', 'aaa', 'ttttt', 'ssss']
print(cars)
print(sorted(cars));
print(sorted(cars, reverse = True));
print('\n')

Reverse 反转列表元素

cars = ['bbb', 'aaa', 'ttttt', 'ssss']
print(cars)
cars.reverse();
print(cars)

Length

len(cars);

你可能感兴趣的:(2018-07-25 Chapter 3 About List)