免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 27449 | 回复: 7
打印 上一主题 下一主题

python中string.join方法的疑问 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-09-26 23:17 |只看该作者 |倒序浏览
原来的目的是想用一个简单的命令将一个list里的各项组成一个字符串。如:将list1=[ 'a','p','p','l','e']转换为字串:"a+p+p+l+e"。

我查了下帮助文件,发现string里的join应该能实现,help文件如下:

    join(words, sep=' ')
        join(list [,sep]) ->; string
        
        Return a string composed of the words in list, with
        intervening occurrences of sep.  The default separator is a
        single space.
        
        (joinfields and join are synonymous)
   
    joinfields = join(words, sep=' ')
        join(list [,sep]) ->; string
        
        Return a string composed of the words in list, with
        intervening occurrences of sep.  The default separator is a
        single space.
        
        (joinfields and join are synonymous)


但为什么下面的命令出错呢?

  1. >;>;>; list1=['a','p','p','p','l','e']
  2. >;>;>; print list1
  3. ['a', 'p', 'p', 'p', 'l', 'e']
  4. >;>;>; str1=''
  5. >;>;>; str1.join(list1,'+')

  6. Traceback (most recent call last):
  7.   File "<pyshell#27>;", line 1, in -toplevel-
  8.     str1.join(list1,'+')
  9. TypeError: join() takes exactly one argument (2 given)
  10. >;>;>; str1.joinfields(list1,'+')

  11. Traceback (most recent call last):
  12.   File "<pyshell#28>;", line 1, in -toplevel-
  13.     str1.joinfields(list1,'+')
  14. AttributeError: 'str' object has no attribute 'joinfields'
  15. >;>;>; str1=join(list1,'+')

  16. Traceback (most recent call last):
  17.   File "<pyshell#29>;", line 1, in -toplevel-
  18.     str1=join(list1,'+')
  19. NameError: name 'join' is not defined
  20. >;>;>;
复制代码

论坛徽章:
0
2 [报告]
发表于 2005-09-27 06:34 |只看该作者

python中string.join方法的疑问

string.join(list1,'+')

论坛徽章:
0
3 [报告]
发表于 2005-09-27 08:34 |只看该作者

python中string.join方法的疑问

再仔细看一下文档吧。你看的是string模块的join方法,因此它需要两个参数,一个是list,另一个是分隔符。而且调用时正如cnxo所示,如果你导入了 string模块:

import string
你需要使用string.join()这样来用。

但现在string对象本身就有这样方法,这样它只需要一个分隔符参数。用法为:

'+'.join(list1)

这样更pythonic,再多看一看例子和文档吧。

论坛徽章:
0
4 [报告]
发表于 2005-09-27 08:51 |只看该作者

python中string.join方法的疑问

木头正解

论坛徽章:
0
5 [报告]
发表于 2005-09-28 13:54 |只看该作者

python中string.join方法的疑问

谢谢以上各位!
python自带的帮助文件里,例子太少了,几乎没有!只能自己找了:( 各位老大有什么建议?:)

论坛徽章:
0
6 [报告]
发表于 2005-09-28 16:21 |只看该作者

python中string.join方法的疑问

你要转变观念,文档是很重要,但代码更重要,它才是真正可以运行的东西。而且学习Python这类的开源软件正是需要多读源码,而且还有一点就是多做练习,多做测试,用代码去验证你的想法,而不能只靠文档就解决问题,那是不可能,也学不好的。

论坛徽章:
0
7 [报告]
发表于 2007-02-06 10:41 |只看该作者
>;>;>;list1=['a','p','p','p','l','e']
>;>;>; print list1
['a', 'p', 'p', 'p', 'l', 'e']
>;>;>; str1=''
>;>;>; str1.join(list1[0:])
apple

[ 本帖最后由 me09 于 2007-2-6 10:42 编辑 ]

论坛徽章:
0
8 [报告]
发表于 2012-05-30 13:42 |只看该作者
回复 7# me09
  1. ''.join(list1)
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP