将多个字符串插入一个字符串(Python)

问题描述 投票:0回答:4

特定

original = 'Has two optional arguments which must be specified.' 

strings = [{'index': 3, 'string': 'foo'}, {'index': 7, 'string': 
'bar'}, {'index': 12, 'string': 'abc'}]

什么是一种有效的方式(理想情况下,只通过original迭代一次)将string中的所有stringss插入original指定的index?在这种情况下,该函数将返回'Hasfoo twobar optiabconal arguments which must be specified.'

例如,这是我刚写的一个低效的实现:

def add_strings(original, strings):
    added_length = 0
    for i in strings:
        insertion_index = i['index'] + added_length
        original = original[:insertion_index] + i['string'] + 
        original[insertion_index:]
        added_length += len(i['string'])
return original
python string list insert
4个回答
1
投票

收集列表中的部分,始终是original的下一部分,然后是要插入的下一个字符串。然后加入。

def add_strings(original, strings):
    parts = []
    start = 0
    for s in strings:
        parts += original[start:s['index']], s['string']
        start = s['index']
    parts += original[start:],
    return ''.join(parts)

2
投票

您可以先将original拆分为字符串列表,然后将新字符串添加到strings中找到的位置:

original = 'Has two optional arguments which must be specified.'

strings = [{'index': 3, 'string': 'foo'}, {'index': 7, 'string': 
            'bar'}, {'index': 12, 'string': 'abc'}]

hashed = list(original)
for str_pos in strings:
    pos = str_pos['index']
    temp = hashed[pos]
    hashed[pos] = str_pos['string'] + temp

result = ''.join(hashed)

print(result)

哪个输出:

Hasfoo twobar optiabconal arguments which must be specified.

1
投票

你可以试试这个:

original = 'Has two optional arguments which must be specified.' 
strings = [{'index': 3, 'string': 'foo'}, {'index': 7, 'string': 'bar'}, {'index': 12, 'string': 'abc'}]
s = [0]+[i['index'] for i in strings]+[len(original)]
split_s = '{}'.join([original[s[i]:s[i+1]] for i in range(len(s)-1)]).format(*[i['string'] for i in strings])

输出:

'Hasfoo twobar optiabconal arguments which must be specified.'

0
投票

为什么要这么复杂?只需一个循环即可解决您的问题:

original = 'Has two optional arguments which must be specified.'
strings = [{'index': 3, 'string': 'foo'}, {'index': 7, 'string':
'bar'}, {'index': 12, 'string': 'abc'}]


meta_data=sorted([(i['index'],i['string']) for i in strings])[::-1]

splitted=list(original)

for k in meta_data:
    splitted.insert(*k)



print("".join(splitted))

输出:

Hasfoo twobar optiabconal arguments which must be specified.

让我们通过三个简单的步骤解决您的问题:

数据是:

original = 'Has two optional arguments which must be specified.'
strings = [{'index': 3, 'string': 'foo'}, {'index': 7, 'string':
'bar'}, {'index': 12, 'string': 'abc'}]

第一步 :

只需使用index_no和string创建一个必须插入的元组:

meta_data=sorted([(i['index'],i['string']) for i in strings])[::-1]

print(meta_data)

输出:

[(12, 'abc'), (7, 'bar'), (3, 'foo')]

第二步:

将您的字符串转换为列表,因为我们想要修改它:

splitted=list(original)
print(splitted)

输出:

['H', 'a', 's', ' ', 't', 'w', 'o', ' ', 'o', 'p', 't', 'i', 'o', 'n', 'a', 'l', ' ', 'a', 'r', 'g', 'u', 'm', 'e', 'n', 't', 's', ' ', 'w', 'h', 'i', 'c', 'h', ' ', 'm', 'u', 's', 't', ' ', 'b', 'e', ' ', 's', 'p', 'e', 'c', 'i', 'f', 'i', 'e', 'd', '.']

第三步:

for k in meta_data:
    splitted.insert(k[0],k[1])

print("".join(splitted))

输出:

Hasfoo twobar optiabconal arguments which must be specified.
© www.soinside.com 2019 - 2024. All rights reserved.