use*_*099 -3 python numpy list scipy python-2.7
出于仿真目的,我想生成一个字符串对列表。每个字符串对包含两个字符串。每个字符串由随机生成的数字组成。字符串的长度也是随机数。如何使用实现此功能Numpy?
这是一个无限的生成器。您可以对其进行切片(如最后一行所示),或直接对其进行迭代:
import itertools
import random
def one_string():
l = random.randint(1, 5)
return "".join(random.choice("0123456789") for _ in range(l))
def string_pairs():
while True:
yield one_string(), one_string()
print(list(itertools.islice(string_pairs(), 10)))
Run Code Online (Sandbox Code Playgroud)
产生:
[('840', '452'), ('20', '4651'), ('784', '589'), ('1', '08211'), ('809', '2103'), ('48975', '46884'), ('307', '83913'), ('88512', '212'), ('57', '11772'), ('38', '14')]
Run Code Online (Sandbox Code Playgroud)