p04

来源:互联网 发布:gmail mac 客户端 编辑:程序博客网 时间:2024/06/08 09:03

1. 列表:

lst = [5.4, 'hello', 2]


2.列表和字符串相同点:

都有前向索引和后向索引([]运算符)

都可以向字符串一样切片([:])

拼接(+)和重复(*)

成员(in运算符)

长度(len()函数)

循环(for)


3. 列表和字符串不同点:

列表使用[]生成,元素之间用逗号分隔;字符串用引号生成

列表可以包含多种类型的对象;字符串只能是字符

列表内容是可变的;字符串是不可变的


4. 下面程序输出结果是:4

a = [1, 2, 3]b = ab[1] = 4print a[1]


5. append()与extend()函数:

>>> lst[5.4, 'hello', 2, 3, 5]>>> lst + 9Traceback (most recent call last):  File "<pyshell#6>", line 1, in <module>    lst + 9TypeError: can only concatenate list (not "int") to list>>> lst + [9][5.4, 'hello', 2, 3, 5, 9]>>> lst[5.4, 'hello', 2, 3, 5]>>> lst[3] = 8>>> lst[5.4, 'hello', 2, 8, 5]>>> lst.append(10)>>> lst[5.4, 'hello', 2, 8, 5, 10]>>> lst.extend([200, 'world'])>>> lst[5.4, 'hello', 2, 8, 5, 10, 200, 'world']>>> lst[:5:2][5.4, 2, 5]

6. insert(), pop(), remove(), sort(), reverse()

>>> lst[5.4, 'hello', 2, 8, 5, 10, 200, 'world']>>> lst.insert(5, 2)>>> lst[5.4, 'hello', 2, 8, 5, 2, 10, 200, 'world']>>> lst.pop()'world'>>> lst[5.4, 'hello', 2, 8, 5, 2, 10, 200]>>> lst.pop()200>>> lst[5.4, 'hello', 2, 8, 5, 2, 10]>>> lst.remove(2)>>> lst[5.4, 'hello', 8, 5, 2, 10]>>> lst.remove(800)Traceback (most recent call last):  File "<pyshell#48>", line 1, in <module>    lst.remove(800)ValueError: list.remove(x): x not in list>>> lst[5.4, 'hello', 8, 5, 2, 10]>>> lst.sort()>>> lst[2, 5, 5.4, 8, 10, 'hello']>>> lst.reverse()>>> lst['hello', 10, 8, 5.4, 5, 2]

7. 计算10个数字的平均值:

num = []for i in range(10):    num.append(float(raw_input()))s = 0for n in num:    s += nprint s/len(num)


8. 对于7也可以使用内建函数:print sum(num)/len(num)


9. 列表类似于c中的指针:

>>> a = 10>>> b = a>>> b = 20>>> a10>>> b20>>> lst['a', 'b', 'hello']>>> lst1 = lst>>> lst1[0] = 8>>> lst[8, 'b', 'hello']


10. 交换两个元素:

def swap(lst, a, b):    temp = lst[a]    lst[a] = lst[b]    lst[b] = lst[a]arr = [10, 20]print arrswap(arr, 0, 1)print arr

11. 列表中每个元素左移一位,第一个元素放到最后:

def ll(lst):    temp = lst[0]    for i in range(len(lst)-1):        lst[i] = lst[i+1]    lst[-1] = temp<pre name="code" class="python">def f(l):    l = [4, 5, 6]    return l     a = [1, 2, 3]f(a)print a[1]

lst = [10,20,30,40,50,60]print lstll(lst)print lst


12. 以下代码输出结果为:2 可以使用http://www.pythontutor.com/看下:

def f(l):    l = [4, 5, 6]    return l     a = [1, 2, 3]f(a)print a[1]

13.  list的index方法:

>>> lst[20, 30, 40, 50, 60, 10]>>> lst.index(30)1>>> lst.append(30)>>> lst[20, 30, 40, 50, 60, 10, 30]>>> lst.index(30)1

14. 时间复杂度:4n + 10和100n +137都与输入长度成正比:

大O表示,只保留高阶项:

4n + 4 = O(n)

n^2 + 3n + 4 = O(n^2)

2^n + n^3 = O(2^n)

15. 二分查找算法:

def bi_search(lst, v):    low = 0    high = len(lst) - 1    while low <= high:        mid = (low + high)/2        if v > lst[mid]:            low = mid + 1        elif v < lst[mid]:            high = mid -1        else:            return mid    return -1

16. 上算法中:将low=mid +1和high= mid-1改为low= mid, high = mid,将有可能出现死循环如:[0, 1]查找1;将low<=high改为low<high,有可能漏掉要查找的元素如:[0,1, 2, 3]查找3


17. 选择排序:

def swap(lst, a, b):    temp = lst[a]    lst[a] = lst[b]    lst[b] = tempdef sel(lst):    for i in range(len(lst)):        for j in range(i+1, len(lst)):            if lst[i] > lst[j]:                swap(lst, i, j)

18. 冒泡排序:

def swap(lst, a, b):    temp = lst[a]    lst[a] = lst[b]    lst[b] = tempdef bubble(lst):    flag = True    high = len(lst) - 1    while flag:        flag = False        for i in range(high):            if lst[i] > lst[i+1]:                swap(lst, i, i+1)                flag = True        high -= 1

19. 内建函数:sorted(), 如:sorted(lst),与lst.sort()相比不改变lst;使用的是快速排序


20. 列表解析或推导:

一种有原列表创建新列表的简洁方法:[表达式 for 语句 条件]

eg: lst = [x**2 for x in range(1, 10)]


21. 二维数组sort()的一些用法:

students = [['zhang', 89],['wang', 92],['li', 100],['zhao', 99]]print studentsstudents.sort()print studentsdef f(l):    return l[1]students.sort(key = f)print studentsstudents.sort(key = f, reverse = True)print studentsprint sum([stu[1] for stu in students])/float(len(students))#输出结果如下:>>> [['zhang', 89], ['wang', 92], ['li', 100], ['zhao', 99]][['li', 100], ['wang', 92], ['zhang', 89], ['zhao', 99]][['zhang', 89], ['wang', 92], ['zhao', 99], ['li', 100]][['li', 100], ['zhao', 99], ['wang', 92], ['zhang', 89]]95.0

22. 匿名函数lambda:

上条中定义的f函数可省掉,改为:

students.sort(key = lambda x: x[1])


23. 附加:

>>> [n*n for n in range(7) if n*n%2 == 0][0, 4, 16, 36]>>> l = [1, 2, 3, 4, 'hello']>>> ''.join([str(i) for i in l])'1234hello'>>> '*'.join([str(i) for i in l])'1*2*3*4*hello'>>> ''.join(str(i) for i in l)'1234hello'>>> '*'.join(str(i) for i in l)'1*2*3*4*hello'


0 0
原创粉丝点击