Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1798317
  • 博文数量: 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 14:41:12

3.7. Joining Lists and Splitting Strings

列表连接与字符串分隔

You have a list of key-value pairs in the form key=value, and you want to join them into a single string. To join any list of strings into a single string, use the join method of a string object.

在一个列表中,你有许多以key=value形式存在的键值对,你可能会想将它们连接成一个单独的字符串。使用字符串对象的join方法可以将列表中字符串连接成一个更长的字符串。

Here is an example of joining a list from the buildConnectionString function:

下面是函数buildConnectionString中列表中字符串连接的列子。

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

   One interesting note before you continue. I keep repeating that functions are objects, strings are objects... everything is an object. You might have thought I meant that stringvariables are objects. But no, look closely at this example and you'll see that the string ";" itself is an object, and you are calling its join method.

在你继续前行之前,有一个非常有趣的且值得注意的地方。我一直在反复强调所有的函数都是对象,所有的字符串也是对象,Python中所有的事物都是对象。你可能想到字符串变量也是对象。但是实际上不是这样的,仔细观察这个列子,你会发现字符串”:”本身是对象,你在调用该对象的Join方法。

The join method joins the elements of the list into a single string, with each element separated by a semi-colon. The delimiter doesn't need to be a semi-colon; it doesn't even need to be a single character. It can be any string.

Join方法将列表的元素连接成一个更长的字符串,每一个元素之间通过分号来分隔。分隔符不一定是分号,也不一定是一个单一字符,它可以是任何字符串。

Caution

 

join works only on lists of strings; it does not do any type coercion. Joining a list that has one or more non-string elements will raise an exception.

Join方法只适用于列表中的字符;它不能对任意其它类型进行强制转换。在对List应用jion方法时,如果有多于一个非字符串元素,该方法将抛出一个异常。

 

Example 3.27. Output of odbchelper.py

3.27 odbchelper.py 的输出

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

This string is then returned from the odbchelper function and printed by the calling block, which gives you the output that you marveled at when you started reading this chapter.

Odbchelper函数返回字符串然后通过调用代码块打印出来,当你在阅读本章是很惊奇的看到了程序的输出

You're probably wondering if there's an analogous method to split a string into a list. And of course there is, and it's called split.

你可能回很自然的想到,是不是存在一个类似的函数,能够将一个字符串分隔成一个列表。当然是有的,这个函数就是split

Example 3.28. Splitting a String

3.28 字符串分隔

  1. >>> li = ['server=mpilgrim', 'uid=sa', 'database=master', 'pwd=secret']
  2. >>> s = ";".join(li)
  3. >>> s
  4. 'server=mpilgrim;uid=sa;database=master;pwd=secret'
  5. >>> s.split(";")
  6. ['server=mpilgrim', 'uid=sa', 'database=master', 'pwd=secret']
  7. >>> s.split(";", 1)
  8. ['server=mpilgrim', 'uid=sa;database=master;pwd=secret']

1

split reverses join by splitting a string into a multi-element list. Note that the delimiter (“;”) is stripped out completely; it does not appear in any of the elements of the returned list.

Split,与join 相反,将一个字符串分割成为含有多个元素的一个列表。需要注意的是,分隔符‘;’被移除了,在返回列表中不存在该分隔符了。

 

2

split takes an optional second argument, which is the number of times to split. (“"Oooooh, optional arguments...” You'll learn how to do this in your own functions in the next chapter.)

split 带有一个可选参数,它确定了分割的次数。(哦可选参数….”,在下章,你会学习到在自己的函数如何来实现。)

 

 

Tip

 

 

anystring.split(delimiter, 1) is a useful technique when you want to search a string for a substring and then work with everything before the substring (which ends up in the first element of the returned list) and everything after it (which ends up in the second element).

Anything.split(delimiter,1)是一个非常有用的技巧,特别是当你想在一个字符串中,搜索一个子字符串,你可以对子串前面的串(返回列表返回的第一个元素)或是字串后面的字符串(到第二个参数为止)进行你想要的操作。

 

 

3.7.1. Historical Note on String Methods

字符串方法的历史

When I first learned Python, I expected join to be a method of a list, which would take the delimiter as an argument. Many people feel the same way, and there's a story behind the join method. Prior to Python 1.6, strings didn't have all these useful methods. There was a separate string module that contained all the string functions; each function took a string as its first argument. The functions were deemed important enough to put onto the strings themselves, which made sense for functions like lowerupper, and split. But many hard-core Python programmers objected to the new join method, arguing that it should be a method of the list instead, or that it shouldn't move at all but simply stay a part of the old string module (which still has a lot of useful stuff in it). I use the new join method exclusively, but you will see code written either way, and if it really bothers you, you can use the old string.join function instead.

当我刚开始学习Python的时候,我希望jion会是list的一个方法,带有一个分隔符作为参数。许多人和我有相同的想法。Jion方法后面有一个小故事。在Python1.6以前,字符串是没有这些强大功能的。字符串单独模块包含了关于字符串的所有函数;所有的函数的第一个参数都是字符窜类型。这些函数被认为是如此的重要以至于字符串本身也是用他们,像Lower,upper,split的函数变得很重要。但是许多资深的Python编程人员反对新的join方法,它们争辩说它应该是列表的方法而不是相反,或者争辩说Join方法根本就不应该移动,而是仅仅作为string模块的组成部分(string模块含有很多有用的方法。我个人更倾向于单独是用join方法,但是你可能会看到代码是用任何一种方式书写。如果这确实让你不爽,你可以使用stirng.join方法来代替。

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