我是python的新手,并将其作为数据结构和算法模块的一部分进行研究。
我们正在研究可用于字符串的方法,特别是.split()
myString = 'abracadabra'
print (myString.split('a'))
当代码运行时,输出看起来像['',' br',' c',' d',& #39; br','']
为什么python将" br"一起而不是输出类似[",''''' c'' d'',&# 39,b'' R''']
答案 0 :(得分:0)
来自docs:
str.split(sep=None, maxsplit=-1)
Return a list of the words in the string, using sep as the delimiter
string. If maxsplit is given, at most maxsplit splits are done (thus, the list
will have at most maxsplit+1 elements). If maxsplit is not specified or -1,
then there is no limit on the number of splits (all possible splits are made).
If sep is given, consecutive delimiters are not grouped together
and are deemed to delimit empty strings (for example,
'1,,2'.split(',') returns ['1', '', '2']). The sep argument may
consist of multiple characters (for example, '1<>2<>3'.split('<>')
returns ['1', '2', '3']). Splitting an empty string with a
specified separator returns [''].
答案 1 :(得分:0)
通俗地说:
support(x) / support(a) ≥ confidence level
将使用&#39; a&#39;作为分隔符。
这样想:
例如,如果.split('a')
为myString
,则会执行以下操作:
'abracadabra
- &gt; 'abracadabra'
- &gt; ' br c dbr '
如果您在['br', 'c', 'd, 'br']
上.split()
并在其上运行myString
,它将返回一个包含每个字符作为单个元素的数组。在这种情况下,list()
- &gt; myString.split()