- 论坛徽章:
- 0
|
原来的目的是想用一个简单的命令将一个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)
但为什么下面的命令出错呢?
- >;>;>; list1=['a','p','p','p','l','e']
- >;>;>; print list1
- ['a', 'p', 'p', 'p', 'l', 'e']
- >;>;>; str1=''
- >;>;>; str1.join(list1,'+')
- Traceback (most recent call last):
- File "<pyshell#27>;", line 1, in -toplevel-
- str1.join(list1,'+')
- TypeError: join() takes exactly one argument (2 given)
- >;>;>; str1.joinfields(list1,'+')
- Traceback (most recent call last):
- File "<pyshell#28>;", line 1, in -toplevel-
- str1.joinfields(list1,'+')
- AttributeError: 'str' object has no attribute 'joinfields'
- >;>;>; str1=join(list1,'+')
- Traceback (most recent call last):
- File "<pyshell#29>;", line 1, in -toplevel-
- str1=join(list1,'+')
- NameError: name 'join' is not defined
- >;>;>;
复制代码 |
|