Python list

 基本操作

l1=[1,2,"a","b",3]
l1[0]
l1[-1]
l1[-2]
l1[1:4]
l1[::2]

 #嵌套

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

 ll[2][1]

6
#倒序 l1[::-1] #更新 l1[4]="c" l1 #添加 l1[0:0]="0" l1[-1:-1]="9" l1.append("d") l1 #删除 del l1[-1] l1
del l1[1:3]
l1
l2=[5,6,7,8] #组合 print l1+l2 #重复 print l1*2 #判断元素 "a" in l1 #递归 for i in l1: print i 

 

函数

len(list) return 元素个数

l1=[1,2,3]
len(l1)

3

 

cmp(list1,list2) return 

如果比较的元素是同类型的,则比较其值,返回结果。

如果两个元素不是同一种类型,则检查它们是否是数字。

  • 如果是数字,执行必要的数字强制类型转换,然后比较。
  • 如果有一方的元素是数字,则另一方的元素"大"(数字是"最小的")
  • 否则,通过类型名字的字母顺序进行比较。

如果有一个列表首先到达末尾,则另一个长一点的列表"大"。

如果我们用尽了两个列表的元素而且所 有元素都是相等的,那么结果就是个平局,就是说返回一个 0

#如果比较的元素是同类型的,则比较其值,返回结果。

l1=[1,2]
l2=[1,3]

cmp(l1,l2)

-1

#如果两个元素不是同一种类型,则检查它们是否是数字。
#如果是数字,执行必要的数字强制类型转换,然后比较。
#如果有一方的元素是数字,则另一方的元素"大"(数字是"最小的")

l2=["1",2]

cmp(l1,l2)

-1

#否则,通过类型名字的字母顺序进行比较。

l1=["b",2]
l2=["a",3]

cmp(l1,l2)

1

#如果有一个列表首先到达末尾,则另一个长一点的列表"大"。

l2=["b",2,0]

cmp(l1,l2)

-1

#如果我们用尽了两个列表的元素而且所 有元素都是相等的,那么结果就是个平局,就是说返回一个 0


l1=[1,"2"]
l2=[1,"2"]

cmp(l1,l2)

0

 

max(list) return 最大值的列表

 

l1=[1,2,3,4]
l2=["abc","bcd",123]
max(l1)

4

max(l2)

'bcd'

 

min(list) return 最小值的列表

l1=[1,2,3,4]
l2=["abc","bcd",123]
min(l1)

1

min(l2)

123

 

list(tuple)  用于将元组转换为列表

 

t1=(1,2,3)
type(t1)

tuple

l1=list(t1)
type(l1)

list

 

 

 方法:

 

list.append(obj)
append() 方法用于在列表末尾添加新的对象

l1=[1,2,"a"]
l1.append("b")
l1

 [1, 2, 'a', 'b']

 

list.count(obj)
用于统计某个元素在列表中出现的次数

l1.append("a")
l1.count("a")

2

 

list.extend(seq)

用于在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)

l2=[6,7,"d"]
l1.extend(l2)
l1

 [1, 2, 'a', 'b', 'a', 6, 7, 'd']

#相当于l1+l2

 

list.index(obj)
用于从列表中找出某个值第一个匹配项的索引位置

l1.index("a")

2

 #The index method does a linear search, and stops at the first matching item. If no matching item is found, it raises a ValueError exception.

 

 try:

 i = L.index(value)

 except ValueError:

 i = -1 # no mat

 

list.insert(index,obj)
用于将指定对象插入列表的指定位置

l1.insert(0,"begin")
l1.insert(len(l1)/2,"middle")
l1

['begin', 1, 2, 'a', 'middle', 'b', 'a', 6, 7, 'd']

 

list.remove()
用于移除列表中某个值的第一个匹配项

l1

['begin', 1, 2, 'a', 'middle', 'b', 'a', 6, 7, 'end', 'end', 'd']

l1.remove("begin")
l1.remove("a")
l1

[1, 2, 'middle', 'b', 'a', 6, 7, 'end', 'end', 'd']

#2

 item = L.pop(index)

 L.remove(item)

 

 

list.reverse()
用于反向列表中元素

l1

[1, 2, 'middle', 'b', 'a', 6, 7, 'end', 'end', 'd']

l1.reverse()

l1

['d', 'end', 'end', 7, 6, 'a', 'b', 'middle', 2, 1]

 L.reverse()

 # append/insert/pop/delete at far end

 L.reverse()

 

 

list.sort([fun])
用于对原列表进行排序,如果指定参数,则使用比较函数指定的比较函数

l1

['d', 'end', 'end', 7, 6, 'a', 'b', 'middle', 2, 1]

l1.sort()
l1

[1, 2, 6, 7, 'a', 'b', 'd', 'end', 'end', 'middle']


list.pop(obj=list[-1])
用于移除列表中的一个元素(默认最后一个元素),并且返回该元素的值

>>> l1=["abc",123,"efg",456]
>>> l1.pop()
456
>>> l1
['abc', 123, 'efg']
>>> l1.pop(0)
'abc'
>>> l1
[123, 'efg']
>>>

 

 Looping Over list

#1 get item

for item in l1:
    print item

#2 get index 

for index in range(len(l1)):
    print index


#3迭代值的同时迭代下表

for index,value in enumerate(l1):
    print index,value

#4 use iterator

i=iter(l1)
item1=i.next()
item2=i.next()

 for i in iter(l1):

    print i

#if list only contains strings,you can combine them into a single long string


l1=["P","y","t","h","o","n"]
s1="".join(l1)


#if list only contains numbers,you can use build in sum functions

l1=[1,2,3,4]
l2=[5,6,7,8]
l1_sum=sum(l1)
l1_average=float(sum(l1))/float(len(l1))

 

一些注意的地方:

 

#1

A = B = [] # both names will point to the same list

A = []
B = A # both names will point to the same list

A = []; B = [] # independent lists


#2

L = []
M = L

# modify both lists
L.append(1)

 

# apply a function to every item in a list

def fun_1(a):
    return a+1
     
#1
l=[]
for item in l1:
    l.append(fun_1(item))
    
#2 

for index,item in enumerate(l1):
    l1[index]=fun_1(item)
    
print l1

#3

l=map(fun_1,l1)

#4

l=[fun_1(item) for item in l1 ]
    
#5

l=map(lambda x: x+1,l1)

 

Performance Notes:

The list has the following performance characteristics:

  • The list object stores pointers to objects, not the actual objects themselves. The size of a list in memory depends on the number of objects in the list, not the size of the objects.
  • The time needed to get or set an individual item is constant, no matter what the size of the list is (also known as “O(1)” behaviour).
  • The time needed to append an item to the list is “amortized constant”; whenever the list needs to allocate more memory, it allocates room for a few items more than it actually needs, to avoid having to reallocate on each call (this assumes that the memory allocator is fast; for huge lists, the allocation overhead may push the behaviour towards O(n*n)).
  • The time needed to insert an item depends on the size of the list, or more exactly, how many items that are to the right of the inserted item (O(n)). In other words, inserting items at the end is fast, but inserting items at the beginning can be relatively slow, if the list is large.
  • The time needed to remove an item is about the same as the time needed to insert an item at the same location; removing items at the end is fast, removing items at the beginning is slow.
  • The time needed to reverse a list is proportional to the list size (O(n)).
  • The time needed to sort a list varies; the worst case is O(n log n), but typical cases are often a lot better than that.
一个很好的总结 http://effbot.org/zone/python-list.htm#accessing

 

posted on 2017-01-17 11:52  大大的橙子  阅读(394)  评论(0编辑  收藏  举报

导航