如何动态地将未知数量的插入插入字符串?

时间:2019-06-26 19:22:10

标签: python python-3.x

我正在尝试将未知数量的插入(由input_list定义)添加到预定义的字符串集(单词)中。这些input_lists和word将更改,但是将提供所有输入值,并且word函数将始终在';'上分割。并且需要按顺序在每个“ {}”处添加input_list。

例如:

input_list = ['input', 'second', 1, 'input2', 'third', 4, 'input3', 'fourth', 5]
words =  'this is my {} string1;this will be my {} input string {}'

应生成:

'this is my input string1'
'this will be my second input string 1'
'this is my input2 string'
'this will be my third input string 4'
'this is my input3 string'
'this will be my fourth input string 5'

我一直在玩以下游戏:

input_counter = 0

words = words.split(';')
variation = len(input_list)/len(words)

for similar in range(0, variation):
    for phrase in words:
        new_count = len(phrase.split('{}'))
        print(phrase.format(input_list[input_counter:input_counter+new_count]))
        input_counter += new_count

但是无法找到将未知数量(列表)的输入值添加到字符串中的方法。

有什么想法吗?

5 个答案:

答案 0 :(得分:2)

几件事:

  • 将要格式化的行列表重命名为lines
  • 基于外部循环的控制明确地决定要使用多少input_list
  • 要在行中填写{}的数量;这比您在{}上拆分得到的零件少了一个。
  • 使用*不传递值列表来填充格式,而是将每个列表项作为单独的参数传递

哪个让我们到这里:

lines = words.split(';')
while input_counter < len(input_list):
    for phrase in lines:
        new_count = len(phrase.split('{}')) - 1
        print(phrase.format(*input_list[input_counter:input_counter+new_count]))
        input_counter += new_count

答案 1 :(得分:1)

每个单词中可以包含{}的数量。 这样,您就可以在单词中替换大量参数,并且可以在input_list

上相应地执行切片
words = words.split(';')
params = input_list[:]

while params:
  for w in words:
    num_vars = w.count('{}')
    args, params = params[:num_vars], params[num_vars:]
    print(w.format(*args))

答案 2 :(得分:0)

这样的事情怎么样? 所提供的单词总是具有与上述相同的输入,并且输入列表可以分为与上述相同的部分

https://onlinegdb.com/B1EZFB-xH

input_list = ['input', 'second', 1, 'input2', 'third', 4, 'input3', 'fourth', 5]
words =  'this is my {} string;this will be my {} input string {}'
count = int(len(input_list)/3)
words = words.split(';')
print(words)
iter = 0
for i in range(count):
    print(words[0].format(input_list[iter]))
    print(words[1].format(input_list[iter+1], input_list[iter+2]))
    iter += 3

答案 3 :(得分:0)

input_list = ['input', 'second', 1, 'input2', 'third', 4, 'input3', 'fourth', 5]
words =  'this is my {} string1;this will be my {} input string {}'

placeholders = words.count('{}')

# Splitting the input list based on how many values go into the string
inputs = [
    input_list[i:i+placeholders]
    for i in range(0, len(input_list), placeholders)
]
'''
inputs = [
    ['input', 'second', 1], 
    ['input2', 'third', 4],
    ['input3', 'fourth', 5]
]
'''

for input in inputs:
    # place the inputs in the {} and split it at ; then join it back with \n
    print('\n'.join(words.format(*input).split(';')))
    print()

输出:

this is my input string1
this will be my second input string 1

this is my input2 string1
this will be my third input string 4

this is my input3 string1
this will be my fourth input string 5

答案 4 :(得分:0)

您也可以尝试以下方法:

x = []

input_list = ['input', 'second', 1, 'input2', 'third', 4, 'input3', 'fourth', 5]
words =  'this is my {} string;this will be my {} input string {}'

variation = len(input_list)//3
words = words.split(';')

for i in range(variation):
    x.append(words[0].replace('{}', str(input_list[i*3])))
    x.append(words[1].split('{}')[0]+str(input_list[(i*3)+1])+words[1].split('{}')[1]+str(input_list[(i*3)+2]))
print(x)

输出:

['this is my input string',
 'this will be my second input string 1',
 'this is my input2 string',
 'this will be my third input string 4',
 'this is my input3 string',
 'this will be my fourth input string 5']