[英]Removing the [ ] and commas in brackets in a list
I am trying to remove the commas and box brackets [ ] in the list. 我正在尝试删除列表中的逗号和方括号[]。 The program asks the user to enter in the number of values they want randomized within a range of their choosing then displays the results. 该程序要求用户在选择的范围内输入要随机化的值的数量,然后显示结果。 Currently the program prints out as: 当前程序打印为:
How many #s do you want? 10
Enter the lowest number to generate 1
Enter the highest number to generate 10
[1, 8, 6, 3, 6, 10, 4, 2, 10, 9]
but I need it to display as: 但我需要将其显示为:
How many #s do you want? 10
Enter the lowest number to generate 1
Enter the highest number to generate 10
1 8 6 3 6 10 4 2 10 9
Maybe I'm going about it incorrectly? 也许我做错了吗? Thoughts? 思考?
import random
def main():
numbers = int(input("How many #s do you want? "))
numbers_2 = int(input("Enter the lowest number to generate "))
numbers_3 = int(input("Enter the highest number to generate "))
pop = make_list(numbers, numbers_2, numbers_3)
print(pop)
num_string = ""
for i in sorted(pop):
num_string += str(i) + " "
return num_string
def make_list(numbers, numbers_2, numbers_3):
num_list = []
for numbers in range(numbers):
num_list.append(random.randint(numbers_2, numbers_3))
return num_list
main()
You can do this with print(*pop)
. 您可以使用print(*pop)
。
When given several parameters (eg, print(1, 2, 3)
, the print
command will print them all out, converted to strings, separated by spaces. 当给定几个参数(例如print(1, 2, 3)
, print
命令将把它们全部打印出来,转换为字符串,并用空格分隔。
With the *
, your array pop
is unpacked so that it forms the parameters for the print
function. 使用*
,您的数组pop
被解压缩,从而形成print
功能的参数。
By setting the sep
parameter, you can even change the separator. 通过设置sep
参数,您甚至可以更改分隔符。 For example, print(*pop, sep=",")
would give 1,8,6,3,6,10,4,2,10,9
. 例如, print(*pop, sep=",")
将给出1,8,6,3,6,10,4,2,10,9
。
You are returning the list, and so it is formatted appropriately in the output. 您正在返回列表,因此它在输出中的格式正确。 To control the formatting, use print
eg 要控制格式,请使用print
例如
print(' '.join(map(str,pop)))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.