如何在python中打印任意数量的x次列表?

时间:2016-07-14 17:25:35

标签: python arrays list printing

例如,如果我要打印数字locationArray [{ annotation = "<MKPointAnnotation: 0x7fb75a782380>"; country = Canada; latitude = "71.47385399229037"; longitude = "-96.81064609999999"; }, { annotation = "<MKPointAnnotation: 0x7fb75f01f6c0>"; country = Mexico; latitude = "23.94480686844645"; longitude = "-102.55803745"; }, { annotation = "<MKPointAnnotation: 0x7fb75f0e6360>"; country = "United States of America"; latitude = "37.99472997055178"; longitude = "-95.85629150000001"; }] ,十次......然后从中创建一个列表,即:

5

我将如何实现这一目标?

3 个答案:

答案 0 :(得分:3)

In [39]: [5]*10
Out[39]: [5, 5, 5, 5, 5, 5, 5, 5, 5, 5]

答案 1 :(得分:1)

您可以使用列表乘数(作为Vishnu Upadhyay答案)生成整个列表(处理巨大列表时,请注意内存):

[number] * times

或者您可以使用 itertools.repeat 生成生成器:

import itertools
gen_list = itertools.repeat(number, times)
# to print it resolve the generator
print(list(gen_list))

可以找到有关重复的更多信息here

答案 2 :(得分:0)

n = int(input())
list = [5]*n

如果有人输入非整数

,可能只需添加try