简体   繁体   English

如何在 python 中对字符串进行切片?

[英]How to do slicing in strings in python?

I am trying to do slicing in string "abcdeeefghij" , here I want the slicing in such a way that whatever input I use, i divide the output in the format of a list (such that in one list element no alphabets repeat).我正在尝试在字符串"abcdeeefghij"中进行切片,在这里我希望以这样一种方式进行切片,即无论我使用什么输入,我都会以列表的格式划分 output(这样在一个列表元素中没有字母重复)。

In this case [abcde,e,efghij] .在这种情况下[abcde,e,efghij]

Another example is if input is "aaabcdefghiii" .另一个例子是如果输入是"aaabcdefghiii" Here the expected output is [a,a,acbdefghi,i,i] .这里预期的 output 是[a,a,acbdefghi,i,i]

Also amongst the list if I want to find the highest len character i tried the below logic: max_str = max(len(sub_strings[0]),len(sub_strings[1]),len(sub_strings[2])) print(max_str) #output - 6在列表中,如果我想找到最高 len 字符,我尝试了以下逻辑: max_str = max(len(sub_strings[0]),len(sub_strings[1]),len(sub_strings[2])) print(max_str ) #输出 - 6

which will yield 6 as the output, but i presume this logic is not a generic one: Can someone suggest a generic logic to print the length of the maximum string.这将产生 6 作为 output,但我认为这个逻辑不是通用逻辑:有人可以建议一个通用逻辑来打印最大字符串的长度。

Here is how:方法如下:

s = "abcdeeefghij"

l = ['']

for c in s: # For character in s
    if c in l[-1]: # If the character is already in the last string in l
        l.append('') # Add a new string to l
    l[-1] += c # Add the character to either the last string, either new, or old

print(l)

Output: Output:

['abcde', 'e', 'efghij']

Use a regular expression:使用正则表达式:

import re
rx = re.compile(r'(\w)\1+')

strings = ['abcdeeefghij', 'aaabcdefghiii']

lst = [[part for part in rx.split(item) if part] for item in strings]
print(lst)

Which yields哪个产量

[['abcd', 'e', 'fghij'], ['a', 'bcdefgh', 'i']]

You would loop over the characters in the input and start a new string if there is an existing match, otherwise join them onto the last string in the output list.如果存在匹配项,您将遍历输入中的字符并开始一个新字符串,否则将它们连接到 output 列表中的最后一个字符串。

input_ = "aaabcdefghiii"

output = []

for char in input_:
    if not output or char in output[-1]:
        output.append("")
    output[-1] += char

print(output)

To avoid repetition of alphabet within a list element repeat, you can greedily track what are the words that are already in the current list.为了避免列表元素重复中的字母重复,您可以贪婪地跟踪当前列表中已经存在的单词。 Append the word to your answer once you detected a repeating alphabet. Append 一旦你检测到一个重复的字母表,你的答案就是这个词。

from collections import defaultdict

s = input()
ans = []
d = defaultdict(int)
cur = ""
for i in s:
    if d[i]:
        ans.append(cur)
        cur = i # start again since there is repeatition
        d = defaultdict(int)
        d[i] = 1
    else:
        cur += i #append to cur since no repetition yet
        d[i] = 1
if cur: # handlign the last part
    ans.append(cur)
print(ans)

An input of aaabcdefghiii produces ['a', 'a', 'abcdefghi', 'i', 'i'] as expected. aaabcdefghiii的输入按预期生成['a', 'a', 'abcdefghi', 'i', 'i']

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

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