[英]Add space between each character after it is multiplied n times in python
So basically we have to enter 2 lists.所以基本上我们必须输入 2 个列表。 Then each of the list elements is added to its corresponding element (as shown in the example below) to create a sum list.
然后将每个列表元素添加到其对应的元素(如下例所示)以创建总和列表。 Then we take a string and take the first character of the string.
然后我们取一个字符串,取字符串的第一个字符。 We then multiply the character with the numbers from the sum list.
然后我们将字符与总和列表中的数字相乘。 The problem I'm having is that I want to add a space every time the character is repeated (as shown below).
我遇到的问题是我想在每次重复字符时添加一个空格(如下所示)。
This is my code:这是我的代码:
l1 = [i for i in eval(input("Enter a list: "))]
l2 = [i for i in eval(input("Enter another list: "))]
symbol = input("Enter symbol: ")
summation = []
for (i1, i2) in zip(l1, l2):
summation.append(i1+i2)
i = 0
while i < len(summation):
print(summation[i] , symbol[0]*summation[i])
i += 1
This is how my output looks:这就是我的 output 的外观:
Enter a list: 1,2,3
Enter another list: 1,2,3
Enter symbol: hello world
2 hh
4 hhhh
6 hhhhhh
This is how I want my output to look:这就是我希望我的 output 看起来的样子:
Enter a list: 1,2,3
Enter another list: 1,2,3
Enter symbol: hello world
2 h h
4 h h h h
6 h h h h h h
Can someone please help me?有人可以帮帮我吗? It might be a stupid question but I would really appreciate it!
这可能是一个愚蠢的问题,但我真的很感激!
Replace代替
symbol[0]*summation[i]
with和
(symbol[0] + " ")*summation[i]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.