join对于 iterable 的应用:join的使用对象应为:去掉最外层格式框,留下的为str

 1 #join对可迭代对象的使用
 2 >>> lis = ['a','b','c']
 3 >>> str = 'abc'
 4 >>> dic = {'a':'b','c':'d'}
 5 >>> s = ' '
 6 >>> s.join(lis)
 7 #'a b c'
 8 >>> s.join(str)
 9 #'a b c'
10 >>> s.join(dic.keys())
11 #'a c'
12 >>> s.join(dic.values())
13 #'b d'
14 >>> tup = ('a','b')
15 >>> s.join(tup)
16 #'a b'
17 >>> set ={'a','b','c'}
18 >>> s.join(set)
19 #'c a b'                    !!!set无序,且不重复

join对可迭代对象得应用应基于一次可迭代,即由基本元素组成,不涉及嵌套使用

join的使用对象应为:去掉最外层格式框,留下的为str

>>> tupl0 = (1,2),(3,4)
>>> s.join(tupl0)
Traceback (most recent call last):          #元素不能为int型,只能为str
  File "<pyshell#110>", line 1, in <module>
    s.join(tupl0)
TypeError: sequence item 0: expected str instance, tuple found
>>> tupl0 = ('a','b'),('c','d')                 #该种形式也不行   ---》(('a','b'),('c','d') 
>>> s.join(tupl0)
Traceback (most recent call last):
  File
"<pyshell#112>", line 1, in <module>
    s.join(tupl0)
TypeError: sequence item 0: expected str instance, tuple found

 

posted @ 2019-07-26 14:45  牧旭  阅读(588)  评论(0编辑  收藏  举报