Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1752590
  • 博文数量: 335
  • 博客积分: 4690
  • 博客等级: 上校
  • 技术积分: 4341
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-08 21:38
个人简介

无聊之人--除了技术,还是技术,你懂得

文章分类

全部博文(335)

文章存档

2016年(29)

2015年(18)

2014年(7)

2013年(86)

2012年(90)

2011年(105)

分类: Python/Ruby

2011-06-15 12:24:24

3.6. Mapping Lists 映射列表

One of the most powerful features of Python is the list comprehension, which provides a compact way of mapping a list into another list by applying a function to each of the elements of the list.

Python最强大的功能之一就是列表综合操作:它提供了将一个列表映射成另一个列表的紧凑方式,映射的规则是将列表中的每一个元素都应用某函数。

Example 3.24. Introducing List Comprehensions

列表3.24 列表复合操作简介

  1. >>> li = [1, 9, 8, 4]
  2. >>> [elem*2 for elem in li]
  3. [2, 18, 16, 8]
  4. >>> li
  5. [1, 9, 8, 4]
  6. >>> li = [elem*2 for elem in li]
  7. >>> li
  8. [2, 18, 16, 8]

1

To make sense of this, look at it from right to left. li is the list you're mapping. Python loops through li one element at a time, temporarily assigning the value of each element to the variable elem. Python then applies the function elem*2 and appends that result to the returned list.

为了便于理解,你可以自右向左阅读。Li是你打算映射的列表。Python循环时,每一次去列表的一个元素,然后临时的将元素值赋值给变量 elemPython稍后对该元素应用函数 elem*2 ,并将结果追加到需要返回的列表。

2

Note that list comprehensions do not change the original list.

注意的列表综合操作并不改变初始列表。

3

It is safe to assign the result of a list comprehension to the variable that you're mapping. Python constructs the new list in memory, and when the list comprehension is complete, it assigns the result to the variable.

将你打算映射的列表复合操作的返回结果赋值给一个变量是没有问题的。Python在内存中重新创建一个列表,当列表复合操作完成时,将结果返回赋值给变量。

Here are the list comprehensions in the buildConnectionString function that you declared in Chapter 2:

下面是函数buildConnectionString中列表综合操作,该函数在第二章声明。

  1. ["%s=%s" % (k, v) for k, v in params.items()]

First, notice that you're calling the items function of the params dictionary. This function returns a list of tuples of all the data in the dictionary.

首先,你需要注意的是,你在调用params字典的items函数。该函数返回字典中所有数据的一个tuple的列表。

Example 3.25. The keysvalues, and items Functions

3.25 key values &items 函数

  1. >>> params = {"server":"mpilgrim", "database":"master", "uid":"sa", "pwd":"secret"}
  2. >>> params.keys()
  3. ['server', 'uid', 'database', 'pwd']
  4. >>> params.values()
  5. ['mpilgrim', 'sa', 'master', 'secret']
  6. >>> params.items()
  7. [('server', 'mpilgrim'), ('uid', 'sa'), ('database', 'master'), ('pwd', 'secret')]

1

The keys method of a dictionary returns a list of all the keys. The list is not in the order in which the dictionary was defined (remember that elements in a dictionary are unordered), but it is a list.

字典的Key方法返回包含列表所有键的列表。列表中键的顺序不是字典中键值对的定义顺序(记住字典中元素时无需的),但是返回值是一个列表。

2

The values method returns a list of all the values. The list is in the same order as the list returned by keys, so params.values()[n] == params[params.keys()[n]] for all values of n.

Value 方法返回所有值得列表。列表中值得顺序同key方法返回的列表中键的顺序相同,因此params.value()[n] == params[params.key()[n]],适用于所有的n.

3

The items method returns a list of tuples of the form (key, value). The list contains all the data in the dictionary.

Items方法返回tuple的列表,形式是(key,value).该列表包含了字典中的所有元素。

Now let's see what buildConnectionString does. It takes a list, params.items(), and maps it to a new list by applying string formatting to each element. The new list will have the same number of elements as params.items(), but each element in the new list will be a string that contains both a key and its associated value from the params dictionary.

现在我们来考察函数buildConnectionString的功能。它接受一个列表参数,params.items,然后通过对每一个元素应用格式化函数,返回一个新列表。新列表含有的元素数目同params.items的一样多。

Example 3.26. List Comprehensions in buildConnectionString, Step by Step

3.26 逐步介绍函数buildConnectionString的列表综合操作

  1. >>> params = {"server":"mpilgrim", "database":"master", "uid":"sa", "pwd":"secret"}
  2. >>> params.items()
  3. [('server', 'mpilgrim'), ('uid', 'sa'), ('database', 'master'), ('pwd', 'secret')]
  4. >>> [k for k, v in params.items()]
  5. ['server', 'uid', 'database', 'pwd']
  6. >>> [v for k, v in params.items()]
  7. ['mpilgrim', 'sa', 'master', 'secret']
  8. >>> ["%s=%s" % (k, v) for k, v in params.items()]
  9. ['server=mpilgrim', 'uid=sa', 'database=master', 'pwd=secret']

1

Note that you're using two variables to iterate through the params.items() list. This is another use of multi-variable assignment. The first element of params.items() is('server', 'mpilgrim'), so in the first iteration of the list comprehension, k will get 'server' and v will get 'mpilgrim'. In this case, you're ignoring the value of v and only including the value of k in the returned list, so this list comprehension ends up being equivalent to params.keys().

注意,你应该使用两个变量来逐步迭代params.items()列表。这也是一个多个变量同时赋值的例子。Params.items()列表第一个元素为一个tuple’server’,’mpigrim’),所以在列表复合操作第一次迭代中,k将被赋值为’server’v将被赋值为’mpilgrim’。在本例中,你忽略了变量V,返回的列表为仅包含k的列表,因此列表复合操作到最后的效果同parmas.key()等价。

2

Here you're doing the same thing, but ignoring the value of k, so this list comprehension ends up being equivalent to params.values().

这一步所作的事情同上一步一样,不过这次忽略了变量k的值,因此列表复合操作到最后的结果同parmas.values()获得结果一样。

3

Combining the previous two examples with some simple string formatting, you get a list of strings that include both the key and value of each element of the dictionary. This looks suspiciously like the output of the program. All that remains is to join the elements in this list into a single string.

通过格式化字符串,将前面两个列子综合起来,得到了一个同时包含字典所有元素键和值的一个字符串列表。这看起来有点奇怪,如同程序的输出看起来有点奇怪一样。剩下的需要做的就是将列表中的元素连接成一个字符串。

 

阅读(1016) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~