[英]a function that takes a list of integers and prints a string of stars which has the length of a value of an integer to the screen
first of all, hello to everyone, I'm a first timer in here.首先,大家好,我是第一次来这里。 anyways.
无论如何。 I had this problem and I dont have a real direction.:.
我有这个问题,我没有一个真正的方向。:. I tried this:
我试过这个:
def MyStars(inputList):
l = len(inputList)
ret = []
for x in inputList:
ret.append("* ")
print(ret)
but then I realised that the output is a string of * which last the number of integers I have in the original List... the outcome is:但后来我意识到 output 是一个 * 字符串,它最后是我在原始列表中拥有的整数数......结果是:
['* ', '* ', '* ']
while I want it to be for the list [3,9,7] for example:虽然我希望它用于列表 [3,9,7],例如:
*** ********* *******
can someone help?有人可以帮忙吗? tnx
tnx
I would not use append
but a list comprehension with string multiplication:我不会使用
append
而是使用字符串乘法的列表理解:
def MyStars(inputList):
print(' '.join(['*'* i for i in inputList]))
MyStars([3, 9, 7])
output: *** ********* *******
output:
*** ********* *******
To fix your code, you would need 2 loops, one to iterate over the numbers, and one to append your characters, then join
your lists:要修复您的代码,您需要 2 个循环,一个用于迭代数字,一个用于 append 您的字符,然后
join
您的列表:
def MyStars(inputList):
l = len(inputList)
for x in inputList:
ret = []
for i in range(x):
ret.append('*')
print(''.join(ret), end=' ')
MyStars([3, 9, 7])
NB.注意。 note that this version gives a trailing space.
请注意,此版本提供了一个尾随空格。
try this code.试试这个代码。
What happened with your code is that you were close to the answer, but you needed to use the range function and loop through x to add the correct number of stars to your list.您的代码发生的事情是您接近答案,但您需要使用范围 function 并循环通过 x 将正确数量的星添加到您的列表中。
def MyStars(inputList):
l = len(inputList)
ret = []
for x in inputList:
for i in range(x):
ret.append("*")
ret.append(" ")
for a in ret:
print(a)
If to print is all you want to do, you can just如果您只想打印,您可以
print(*("*" * n for n in input_list))
which for input_list = [3, 9, 7]
prints *** ********* *******
对于
input_list = [3, 9, 7]
打印*** ********* *******
<s: str> * <n: int>
multiplies string s
n
times eg "xYZ" * 3
will result in "xYZxYZxYZ"
(in this case "*" * 3
will be "***"
) <s: str> * <n: int>
将字符串s
乘以n
次,例如"xYZ" * 3
将得到"xYZxYZxYZ"
(在这种情况下"*" * 3
将是"***"
)("*" * n for n in input_list)
creates a collection of "*" * n
-s for each n
in input_list
(this creates a generator that can be iterated only once, if you want to iterate the collection more than once, you can create eg a list( [...]
) instead) ("*" * n for n in input_list)
input_list
的每个n
创建一个由"*" * n
-s 组成的集合(如果您想多次迭代该集合,这将创建一个只能迭代一次的生成器,您可以创建例如一个列表( [...]
)代替)print(*<iterable>)
prints each item of the iterable and separates them with sep
(that is by default a space, which happens to be what you want, you can pass sep
as a print
param, eg print(*<iterable>, sep="\n")
print(*<iterable>)
打印可迭代的每个项目并用sep
分隔它们(默认情况下是一个空格,这恰好是您想要的,您可以将sep
作为print
参数传递,例如print(*<iterable>, sep="\n")
An example of fixing your solution (I also adjusted naming to follow python conventions):修复您的解决方案的示例(我还调整了命名以遵循 python 约定):
def my_stars(input_list):
stars_strings = []
for x in input_list:
stars_strings.append("*" * x)
ret = " ".join(stars_strings)
print(ret)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.