代码改变世界

【Python】-列表LIST函数实例

2012-07-27 13:09  12月  阅读(284)  评论(0编辑  收藏  举报

#P3-107
L=[1,2,3,4,5];
L1=[7,8,9];

#append(x),添加X到L尾部
L.append(6)
print("L.append(6):",L);

#count(x),返回X在列表L中出现的次数
print("L.count(2):",L.count(2));

#index(x,start,end):返回L在列表中最左边现出现的索引
print("L.index(3):",L.index(3));

#extend(l),将L的项追加到列表结尾处
L.extend(L1)
print("L.extend(L1):",L);

#insert(x,i):在列表L索引i处插入X
L.insert(0,-1);
print("L.insert(0,-1):",L);

#reverse() 将列表L反向排序
L.reverse();
print("L.reverse:",L);

#sort():对列表L进行排序
L.sort();
print("L.reverse:",L);

#pop:返回并移除L最右边的数据项
L.pop();
print("L.pop:",L);

#pop(i):返回并移除列表L索引为i处的项
L.pop(2);
print("L.pop(2):",L);
s=L.pop(-1);
print("L.pop(-1):",L);
#赋值操作
first,*middle,end="Charles Philip Arthur George Windsor".split();
print("All is:Charles Philip Arthur George Windsor");
print("first:",first);
print("middle:",middle);
print("end:",end);