总结:数列的一些方法

    数列的方法有那么几种:list.append(obj) ---------添加元素到数列的末尾  

             list.insert(index,obj)---------按照index下标插入元素

             list.pop(index)------------对应index的下标元素被删除,默认是删除最后一个元素

             list.count(obj)------------统计obj元素的个数

             list.remove(obj)----------删除重复且排在前面的元素

             list.severse()----------反向数列中的元素

             list.sort()----------数列元素排序

             list.index(obj)--------查看该元素的下标是多少

             list2.exten(list1)-------在list2数列结尾中添加list1数列中的元素

                                  zip(list1,list2)------合并列表

例子:

1)list.append(obj)

list1 = [1,2,3,4,'中国','美国']

list1.append(5)

print(list1)  

# 运行时输出结果

 [1,2,3,4,'中国','美国',5]

2) list.insert(2,'加拿大')

list2 =  [1,2,3,4.0,'中国','美国']

list2.insert(2,'加拿大')

print(list2)

# 运行时输出结果

 [1,2,'加拿大',3,4,'中国','美国']

3)list.pop()

list3 = [1,2,'加拿大',3,4,'中国','美国']

print(list3.pop())

#输出结果

[1,2,'加拿大',3,4,'中国']

print(list3.pop(-1))
#输出结果
[1,2,'加拿大',3,4,'中国']

print(list3.pop(3))

#输出结果
[1,2,3,4,'中国','美国']

4)list.count(jbo)

list4 = [1,2,3,3,4,'中国',3]

list4.count(3)

#输出结果

3

5)list.remove(obj)

list5 = [1,2,'加拿大',3,4,'中国','美国']
list5.remove('美国')
print(list5) # 输出结果 [1,2,'加拿大',3,4,'中国']

6)list.severse()

list7 = [1,3,4,2,'a','r','h']

list6.reverse()

print(list6)

#输出结果
['h', 'r', 'a', 2, 4, 3, 1]

7)list.sort()

list7 = [2,4,6,3,5,1]
list7.sort()
print(list7)

#输出结果

[1, 2, 3, 4, 5, 6]

8)list.index(obj)

list8 =  ['a', 'b', 'c', 'd', 1, 2, 3, 4, 5, 6]

list8.index('b')

# 输出结果

1

  

9)list2.exten(list1)

a = [1, 2, 3, 4, 5, 6]
b = ['a','b','c','d']

b.extend(a)

print(b)

# 输出结果

['a', 'b', 'c', 'd', 1, 2, 3, 4, 5, 6]

10)zip(lis1,list2)

 L1 = [1,3,5,7]
 L2 = [2,4,6,8]

for (i,j) in (L1,L2)
     print(i,j)

# 遍历结果
1 2
3 4
5 6
7 8

  

posted @ 2015-03-28 13:54  训练狂蟒  阅读(299)  评论(0编辑  收藏  举报