在python中解决拆分字符串而不使用split()函数

时间:2016-03-17 06:28:56

标签: python split

面对Python中的采访问题,如下所示?

ex: 
input = ('192.168.15.1', '.', -1) ===> output = (192, 168, 15, 1)
input = ('192.168.15.1', '.', 2) ===> output = ( 192, 168, 15.1 )

解决这样的输入(字符串,字符,整数)将给出输出,使得字符串被字符分割,如果整数是0或小于其他字符串,则字符串被分割为仅字符分割的字符数对于整数的值。

我已经编写了代码,尽管它在边界内工作正常,没有错误条件。那里有更好的代码吗?

由于

def split_string(string,ch,inte):

    q = string 
    c = ch
    i = inte
    s =list(q)
    #Check if integer value is negative or positive 
    if i <= 0:
        # split the string one time 
        r1 = split_once(s,c)
        print r1
    else:
        #split according to the integear value
        r2 = split_acc_intger(s,c,i)
        print r2
def split_once(s,c):
    y = 0
    d = []
    for x in range(len(s)):
        if s[x] == c:
           p=s[y:x]
           d.append(''.join(p))
           y = x + 1
        elif x == len(s)-1:
           p=s[y:]
           d.append(''.join(p))
   return d

def split_acc_intger(s,c,i):
    y = 0
    d =[]
    count = 1
    for x in range(len(s)):
        # the leat number will 1
        if s[x] == c:
            p=s[y:x]
            d.append(''.join(p))
            y = x + 1
            count += 1
       elif count == i :
            p=s[y:]
            d.append(''.join(p))
            break
    return d

5 个答案:

答案 0 :(得分:1)

简单且递归。

def split_str(s,c,i):
    if i == 0:
        return [s]
    else:
        head, _, rest = s.partition(c)
        if rest:
            return [head] + split_str(rest, c, i - 1)
        return [head]

答案 1 :(得分:0)

您可以使用以下功能

解决此任务
def split_string(istr,ich,inte):
    res = []
    prev = 0
    for i,ch in enumerate(istr):
        if ch == ich:
            res.append(istr[prev:i])
            prev = i+1
            inte = inte-1
            if inte == 0:
                break            
    if prev < len(istr):
        res.append(istr[prev:])
    return res

或其他解决方案

def split_string(istr,ich,inte):
    res = []
    h,_,r = istr.partition(ich)
    while r:
        res.append(h)
        inte = inte-1
        if inte == 0:
            h = r
            break               
        h,_,r = r.partition(ich)
    if h:
        res.append(h)
    return res

以下代码

print split_string('192.168.15.1', '.', -1)
print split_string('192.168.15.1', '.', 2)

输出

['192', '168', '15', '1']
['192', '168', '15.1']

答案 2 :(得分:0)

另一种更简单的方法是你可以使用这样的正则表达式:

import re
def split_without_using_split_fn(s, c, i):
    if i<=0:
        print(re.findall("[^"+c+"]+(?=\\"+c+"|$)", s))
    else:
        l=re.findall("[^"+c+"]+(?=\\"+c+"|$)", s)
        print(l[0:-i]+[c.join(l[i:])])

split_without_using_split_fn(*('192.168.15.1', '.', -1))
split_without_using_split_fn(*('192.168.15.1', '.', 2))

输出:

$ python3 p.py 
['192', '168', '15', '1']
['192', '168', '15.1']
$

答案 3 :(得分:0)

def split(text, sep, maxsplit=-1):
    parts = []
    end = -1
    while True:
        start = end + 1
        end = text.find(sep, start)
        if (end == -1) or (maxsplit == 0):
            parts.append(text[start:])
            break
        else:
            parts.append(text[start:end])
            if maxsplit != 0: maxsplit -= 1
    return parts

print(split('192.168.15.1', '.', -1))  # ['192', '168', '15', '1']
print(split('192.168.15.1', '.', 2))  # ['192', '168', '15.1']

答案 4 :(得分:0)

这样的事情会起作用:

def splitter(string_, splitchar, maxsplits):
    # if maxsplits is positive, split string  into maxsplits of parts, on splitchar.
    # Otherwise perform as many splits as the string allows.
    out = []
    sub = []
    for ch in string_:
        if ch != splitchar:
            sub.append(ch)
        else:
            if maxsplits < 1 or (maxsplits > 0 and (len(out) < maxsplits)):
                out.append(''.join(sub))
                sub = []
            else:
                sub.append(ch)
    out.append(''.join(sub))
    return tuple(out)


>>> splitter.splitter('192.168.15.1', '.', -1)
('192', '168', '15', '1')
>>> splitter.splitter('192.168.15.1', '.', 2)
('192', '168', '15.1')
>>> splitter.splitter('192.168.15.1', '.', 0)
('192', '168', '15', '1')
>>> splitter.splitter('192.168.15.1', '.', 1)
('192', '168.15.1')