简体   繁体   English

一个 function ,它接受一个整数列表并在屏幕上打印一个长度为 integer 的星号

[英]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.

相关问题 编写函数mult3(),将整数列表作为输入,仅输出3的倍数,每行一个 - Write function mult3() that takes as input a list of integers and prints only the multiples of 3, one per line 在列表中找到最小的整数,它有字符串和整数 - Find lowest integer in list, which has both strings and integers 取整数n并使用字符串格式将pi打印为n位的函数 - Function that takes integer n and prints pi to n digits using string formatting Function 接受字符串文本和正数 integer 并将其转换为单词列表 - Function that takes a string text and positive integer and converts it to list of words 如果新附加的 integer 的值小于该列表中最大的 integer,如何清除列表中的所有整数 - How to clear all the integers in a list if the new appended integer has a smaller value than the largest integer of that list 从不同长度的字符串值中提取某些整数,其中包含不需要的整数。 模式或位置 - Extract certain integers from string value, of different length, which contains unwanted integers. Pattern or Position 创建longestPossible(python中的longest_possible)辅助函数,该函数接受1个整数参数,这是一首歌曲的最大长度(以秒为单位) - Create longestPossible(longest_possible in python) helper function that takes 1 integer argument which is a maximum length of a song in seconds 如何编写一个 function 接受一个字符串并按频率降序打印字母? - How to write a function that takes a string and prints the letters in decreasing order of frequency? 打印出行号和列表长度的Python程序:错误 - Python program which prints out line number and length of list: Error 编写一个 function,称为 filter_out(),它以整数列表作为输入,返回仅保留数字过滤的列表 - Write a function, called filter_out(), which takes a list of integers as input and return the list filtered by only keeping numbers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM