如何从两个不同的段落中添加行?

时间:2016-08-12 20:25:50

标签: python loops for-loop input

我正在试图弄清楚如何用短线输入两个不同的段落(类似于歌词的结构),然后将它们组合起来。其中一个首先是第一行,然后是另一个段落的行,依此类推。

这样的事情:

输入1:

this is an example
line that is a run
on sentence

输入2:

i am another example
paragraph that is
very similar

输出:

this is an example
i am another example
line that is a run
paragraph that is
on sentence
very similar

我猜它可以通过for循环来完成,但我不确定你将如何抓住这些线并将它们一个接一个地放入。谢谢你的帮助!

4 个答案:

答案 0 :(得分:1)

这是怎么回事?

ggplot(data = oirfsFacetPlot2, aes(x = step, y = vals, stat = "identity"))  +
    geom_line(aes(linetype = indicator)) +
    xlab("Month") + ylab("Percent change") +
    theme_bw() + scale_x_continuous(breaks = seq(0,3,1)) +
    scale_linetype_manual(name = "indicator", values = c(2,1,2))  +
    facet_wrap( ~ country, scales = "free_y", nrow = 3 )  

<强>输出

input1 = """
this is an example
line that is a run
on sentence
"""

input2 = """
i am another example
paragraph that is
very similar
"""

input1_lines = input1.strip().splitlines()
input2_lines = input2.strip().splitlines()

for line1, line2 in zip(input1_lines, input2_lines):
    print("{}\n{}".format(line1, line2))

答案 1 :(得分:1)

我知道我已经迟到了,但这是另一种方法:

a = """ this is an example 
 line that is a run
 on sentence"""

b =  '''i am another example
 paragraph that is
 very similar'''

a_list = a.split("\n")
b_list = b.split("\n")

c = [[x, b_list[index]] for index, x in enumerate(a_list)]
c_string = "\n".join(x for i in c for x in i)

输出是一个字符串,如下所示:

' this is an example \ni am another example\n line that is a run\n paragraph that is\n on sentence\n very similar'

编辑: 或者你可以在一行中完成

c = "\n".join(x for i in [[x, b.split("\n")[index]] for index, x in enumerate(a.split("\n"))] for x in i)

答案 2 :(得分:1)

关于izip的问题是,输出是什么:

input1 = """
this is an example
line that is a run
"""

input2 = """
i am another example
paragraph that is
on sentence
"""

input1_lines = input1.strip().splitlines()
input2_lines = input2.strip().splitlines()

for line1, line2 in zip(input1_lines, input2_lines):
    print("{}\n{}".format(line1, line2))

作为补充:

input1 = [
    'this is an example',
    'line that is a run',
]

input2 = [
    'i am another example',
    'paragraph that is',
    'on sentence',
]

output = ''
for i in range(3):
    output += input1[i] + '\n' if len(input1) > i else '\n'
    output += input2[i] + '\n' if len(input2) > i else '\n'
print(output)

您可以在http://www.tutorialspoint.com/execute_python_online.php

查看

答案 3 :(得分:1)

首先,您需要将数据输入程序。您可以使用input()功能输入单行,如果您需要更多功能,可以根据标准inp()创建自己的input功能。像这样:

    def inp():
        res = []
        line = input()
        while line != '':
            res.append(line)
            line = input()
        return res

尝试在控制台中运行它并输入几行。空行是输入结束:

>>> d1=inp()
this is an example
line that is a run
on sentence
>>>
>>> print(d1)
['this is an example', 'line that is a run', 'on sentence']

d2相同:

>>> d2=inp()
i am another example
paragraph that is
very similar
>>> print(d2)
['i am another example', 'paragraph that is', 'very similar']

接下来你必须混合它们。使用zip方法将元素分组成对,然后使用itertools.chain.from_iterable展平成对列表很容易。像:

>>> list(itertools.chain.from_iterable(zip(d1,d2)))
['this is an example', 'i am another example', 'line that is a run', 'paragraph that is', 'on sentence', 'very similar']

您可以join()将此数组放入一行,'\n'作为分隔符,然后print

>>> print('\n'.join(itertools.chain.from_iterable(zip(d1,d2))))
this is an example
i am another example
line that is a run
paragraph that is
on sentence
very similar