簡體   English   中英

在分割字符串列表中查找字符串

[英]Find string in list of splitted string

我有一個字符串teststring和子列表s ,但其中teststring不小心被拆分。 現在我想知道列表中的索引,如果放在一起,將重新創建teststring

teststring = "Hi this is a test!"

s = ["Hi", "this is", "Hello,", "Hi", "this is", "a test!", "How are", "you?"]

預期結果將是(在列表中的字符串s會彌補teststring需要連續出現- > [0,4,5]將是錯誤的):

[3,4,5]

誰知道怎么做?

試圖提出一個像樣的解決方案,但發現沒有任何工作......

我只是記錄每個實例,一部分teststring字符串出現在s中的一個子字符串中:

test_list = []
for si in s:
    if si in teststring:
        flag = True

    else:
        flag = False
    test_list.append(flag)

然后你會得到: [True, True, False, True, True, True, False, False] ......然后人們必須得到最長的連續“真”的索引。 Anayone知道如何獲得這些索引?

這有點令人費解,但它完成了這項工作:

start_index = ' '.join(s).index(teststring)
s_len = 0
t_len = 0
indices = []
found = False
for i, sub in enumerate(s):
    s_len += len(sub) + 1 # To account for the space
    if s_len > start_index:
        found = True
    if found:
        t_len += len(sub)
        if t_len > len(teststring):
            break
        indices.append(i)

如果你想要的是連接時形成字符串的連續索引的列表,我認為這將做你想要的:

teststring = "Hi this is a test!"

s = ["Hi", "this is", "Hello,", "Hi", "this is", "a test!", "How are", "you?"]

test_list = []
i = 0 # the index of the current element si
for si in s:
    if si in teststring:
        # add the index to the list
        test_list.append(i)

        # check to see if the concatenation of the elements at these 
        # indices form the string. if so, this is the list we want, so exit the loop
        if ' '.join(str(s[t]) for t in test_list) == teststring:
            break
    else:
        # if we've hit a substring not in our teststring, clear the list because
        # we only want consecutive indices
        test_list = []

    i += 1

將列表連接成一個大字符串,在大字符串中查找目標字符串,然后通過檢查列表中每個字符串的長度來確定起始和結束索引。

>>> teststring = "Hi this is a test!"
>>> s = ["Hi", "this is", "Hello,", "Hi", "this is", "a test!", "How are", "you?"]
>>> joined = ' '.join(s)
>>> index = joined.index(teststring)
>>> lengths = list(map(len, s))
>>> loc = 0
>>> for start,ln in enumerate(lengths):
...     if loc == index:
...             break
...     loc += ln + 1
...
>>> dist = 0
>>> for end,ln in enumerate(lengths, start=start):
...     if dist == len(teststring):
...         break
...     dist += ln + 1
...
>>> list(range(start, end))
[3, 4, 5]

這就是我如何處理這個問題,希望它有所幫助:

def rebuild_string(teststring, s):
    for i in range(len(s)): # loop through our whole list
        if s[i] in teststring:
            index_list = [i] # reset each time
            temp_string = teststring
            temp_string = temp_string.replace(s[i], "").strip()
            while i < len(s) - 1: # loop until end of list for each run through for loop
                if len(temp_string) == 0: # we've eliminated all characters
                    return index_list # all matches are found, so we'll break all our loops and exit
                i += 1 # we need to manually increment i inside while loop, but reuse variable because we need initial i from for loop
                if s[i] in temp_string: # the next item in list is also in our string
                    index_list.append(i)
                    temp_string = temp_string.replace(s[i], "").strip()
                else:
                    break # go back to for loop and try again
    return None # no match exists in the list

my_test = "Hi this is a test!"

list_of_strings = ["Hi", "this is", "Hello,", "Hi", "this is", "a test!", "How are", "you?"]


print(rebuild_string(my_test, list_of_strings))

結果:

[3, 4, 5]

基本上我只是在主字符串中找到列表項所在的位置,然后下一個連續的列表項也必須存在於字符串中,直到沒有任何東西可以匹配(沿途去除空白區)。 這也會匹配列表中亂序的字符串,只要它們組合在一起就會重新創建整個字符串。 不確定這是不是你想要的......

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM