简体   繁体   English

特定场景下的字符串切片 Python

[英]String slicing in specific scenarios Python

I have a string I'd like to split to new strings which will contain only text (no commas, spaces, dots etc.).我有一个字符串,我想将其拆分为只包含文本(没有逗号、空格、点等)的新字符串。 The length of each new string must be of variable n.每个新字符串的长度必须是变量 n。 The slicing must go through each possible combination.切片必须通过每个可能的组合 go。 Meaning, for example, an input of func('banana pack', 3) will result in ['ban','ana','nan','ana',pac','ack'].例如,输入func('banana pack', 3)将导致 ['ban','ana','nan','ana',pac','ack']。 So far what I managed to achieve is:到目前为止,我设法实现的是:

def func(text, n):
    text = text.lower()
    text = text.translate(str.maketrans("", "", " .,"))
    remainder = len(text) % n
    split_text = [text[i:i + n] for i in range(0, len(text) - remainder, n)]
    if remainder > 0:
        split_text.append(text[-n:])
    return split_text

First I clean the input, by removing ',' and '.'首先,我通过删除',''.'来清理输入。 . . The input is then split at spaces to take only full words into account.然后在空格处拆分输入以仅考虑完整的单词。 For each word the sections are appended.对于每个单词,都附加了部分。

def func(text,n):
    text=text.replace('.','').replace(',','') #Cleanup
    words = text.split() #split words
    output = []
    for word in words:
        for i in range(len(word)-n+1):
            output.append(word[i:i+n])
    return output

You could unroll the loop one level if you just iterate over everything and discard results with unwanted symbols.如果您只是遍历所有内容并丢弃带有不需要符号的结果,则可以将循环展开一层。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM