广东海洋大学 电子1151 孔yanfei python语言程序设计 第七周

Posted sinat_32097435

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了广东海洋大学 电子1151 孔yanfei python语言程序设计 第七周相关的知识,希望对你有一定的参考价值。

在python中,数据访问模型分为三种,直接存取,序列和映射
对于非容器类的数据类型,都属于直接存取访问;序列的典型代表:tuple, list, string, unicode string, buffer, xrange;映射的典型代表:dict。

Python还有一种叫做容器的数据结构。容器是包含其他对象的任意对象。序列(如元组和列表)和映射(比如字典)就是两类主要的容器。

序列的每个元素有自己的编号(元组可以作为字典的键名),而映射的每个元素则有一个自己的名字(键)。另外还有种容器类型既不是序列也不是映射,叫做集合。

I, 序列的通用操作
1, 索引index

seq = 'sample'
print seq[1]
# a
print seq[-1]
# e

2, 切片slice

seq = 'sample'
print seq[1:4]
# amp
print seq[:]
# sample

注意,上面的操作都省略了步长。

print seq[1:4:2]
# ap

注意下面的用法

print seq[::-1]
#elpmas

这样就完成字符串的翻转

3, 相加

seq1 = 'abcd'
seq2 = ''dfgh
print seq1 + seq2
# abcddfgh

4, 相乘

seq = 'abcd'
print seq*2
# abcdabcd

5, 成员资格

seq = 'abcd'
'bcd' in seq
# True

II, 序列的通用操作函数
1, len
返回序列的长度

seq = 'abcd'
print len(seq)
# 4

2, max, min
返回序列中的最大成员和最小成员

seq = 'abcd'
print max(seq)
print min(seq)
# d
# a

4, sorted
对序列进行排序,返回一个新的序列

Help on built-in function sorted in module __builtin__:

sorted(...)
    sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list

该函数是builtin中的通用函数。
示例1:对字符串进行排序

seq = 'agde'
print sorted(seq)
# ['a', 'd', 'e', 'g']

示例2:对复杂的list进行排序

la = [[4, 1], [3, 3], [2, 5], [1, 2], [5, 4]]
lsort1 = sorted(la)
lsort2 = sorted(la, key=lambda x:x[1])
lsort3 = sorted(la, cmp=lambda x,y:cmp(x[1], y[1]))
lsort4 = sorted(la, cmp=lambda x,y:cmp(x[1], y[1]), reverse=True)

print(la)
print(lsort1)
print(lsort2)
print(lsort3)
print(lsort4)

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

lsort1没有制定cmp, key, 默认按照list中第一个成员进行排序
lsort2指定key为list中的第二个成员
lsort3指定cmp指教函数为比较list的第二个成员
lsort4使用反转排序
3, sort
上面提到的sorted函数是builtin模块中的通用函数,对于不是immutable的序列,如list,内建了sort函数。
示例:

seq = ['a', 'd', 'g', 'b', 'c']
seq.sort()
print seq
# ['a', 'b', 'c', 'e', 'g']

要注意的是,sorted函数返回一个新的list,但是sort函数直接修改了原来的list。
4, map

map(...)
    map(function, sequence[, sequence, ...]) -> list

    Return a list of the results of applying the function to the items of
    the argument sequence(s). If more than one sequence is given, the
    function is called with an argument list consisting of the corresponding
    item of each sequence, substituting None for missing values when not all
    sequences have the same length. If the function is None, return a list of
    the items of the sequence (or a list of tuples if more than one sequence).

示例

def func(a):
        return a*2

la = list('abcd')
lmap = map(func, la)
print la
print lmap

# ['a', 'b', 'c', 'd']
# ['aa', 'bb', 'cc', 'dd']

如果使用lambda函数,则可以写得更简单

la = list('abcd')
lmap = map(lambda x:x*2, la)

5, filter

filter(...)
    filter(function or None, sequence) -> list, tuple, or string

    Return those items of sequence for which function(item) is true. If
    function is None, return the items that are true. If sequence is a tuple
    or string, return the same type, else return a list.

示例

la = ['abc', 'agd', 'rad', 'ljh']
lfilter = filter(lambda x:'a' in x, la)
print la
print lfilter

上面的例子将不包含字母‘a’的元素过滤掉

6, reduce

reduce(...)
    reduce(function, sequence[, initial]) -> value

    Apply a function of two arguments cumulatively to the items of a sequence,
    from left to right, so as to reduce the sequence to a single value.
    For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
    ((((1+2)+3)+4)+5). If initial is present, it is placed before the items
    of the sequence in the calculation, and serves as a default when the
    sequence is empty.

示例

la = ['abc', 'agd', 'rad', 'ljh']
vreduce = reduce(lambda x,y:x+y, la)
print la
print vreduce
# ['abc', 'agd', 'rad', 'ljh']
# abcagdradljh

在上面的例子中,执行了这样的操作:abc与agd相加, 结果再与rad相加,结果再与ljh相加。
上面的例子中永乐lambda,实际上,在这种情况下,可以使用operator。

NAME
operator - Operator interface.
FILE
(built-in)
DESCRIPTION
This module exports a set of functions implemented in C corresponding
to the intrinsic operators of Python. For example, operator.add(x, y)
is equivalent to the expression x+y. The function names are those
used for special methods; variants without leading and trailing
'__' are also provided for convenience.

示例:

import operator

ln = [1, 2, 3, 4]
result = reduce(operator.mul, ln)

在这个例子中,使用operator.mul代替了lambda x,y:x*y。如description,operator.mul(x,y)与x*y

7, reversed

reversed(sequence) -> reverse iterator over values of the sequence
Return a reverse iterator

reversed函数用于将一个序列反转,返回一个iterator。注意,这里并不返回一个序列。所以,如果要使用reversed反转一个序列,应写成:

a = range(10)
b = [x for x in reversed(a)]

8, sum
对序列的所有成员求和。

以上是关于广东海洋大学 电子1151 孔yanfei python语言程序设计 第七周的主要内容,如果未能解决你的问题,请参考以下文章

广东海洋大学 电子1151 孔yanfei python语言程序设计 第八周

广东海洋大学 电子1151 孔yanfei python语言程序设计 第一周

广东海洋大学 电子1151 孔yanfei python语言程序设计 第三周

广东海洋大学 电子1151 孔yanfei python语言程序设计 第三周

广东海洋大学 电子1151 孔yanfei python语言程序设计 第五周

广东海洋大学 电子1151 孔yanfei python语言程序设计 第五周