3 回答
TA贡献1818条经验 获得超3个赞
您可以使用列表理解将它们组合成一个数组,如下所示:
sample_length = 10
values = np.random.randint(low=10000, high= 15000, size = sample_length)
# Do this
column = ['q' + str(i) for i in values ]
如果上面的代码太多而无法为您解压,请参阅:
sample_length = 10
values = np.random.randint(low=10000, high= 15000, size = sample_length)
columns = []
for i in values:
columns.append('q' + str(i))
两个代码片段都做同样的事情。
希望这可以帮助...!
TA贡献1820条经验 获得超10个赞
几乎一个班轮将是:
sample_length = 10
coloumns = ["q" + "".join(uniqueNumbers) for uniqueNumbers in list(map(lambda generatedNumbers: [str(number) for number in generatedNumbers], [random.randint(0, 9, 5) for _ in range(sample_length)]))]
为了更好的可读性:
sample_length = 10
coloumns = [
"q" + "".join(uniqueNumbers) for uniqueNumbers in list(
map(
lambda generatedNumbers: [
str(number) for number in generatedNumbers
],
[
random.randint(0, 9, 5) for _ in range(sample_length)
]
)
)
]
在这种情况下,id 可以以 0 开头。
更好的方法如下,因为我猜每个数字都应该是唯一的:
sample_length = 10
def getNumbers():
return "".join(list(map(lambda number: str(number), random.randint(0, 9, 5))))
numbers = []
while len(numbers) != sample_length:
newNumbers = getNumbers()
if newNumbers not in numbers:
numbers.append(newNumbers)
coloumns = ["q" + "".join(uniqueNumbers) for uniqueNumbers in numbers]
添加回答
举报