在 Python 中创建 2D 点列表的更优雅的方法 [英] More elegant way to create a list of 2D points in Python
本文介绍了在 Python 中创建 2D 点列表的更优雅的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在 python 中创建一个二维点 (x,y) 列表.这样做
I need to create a list of 2D points (x,y) in python. This will do it
l = []
for x in range (30,50,5):
for y in range (1,10,3):
l.append((x,y))
所以:print l
将产生:
[(30, 1), (30, 4), (30, 7), (35, 1), (35, 4), (35, 7), (40, 1), (40, 4), (40, 7), (45, 1), (45, 4), (45, 7)]
有没有更优雅的方法来做到这一点?
Is there a more elegant way of doing this?
推荐答案
from itertools import product
l = list(product(range(30,50,5), range(1,10,3)))
它的伸缩性更好,应该比生成器表达式、列表理解或显式循环更快.
It scales better and should be faster than a generator expression, list comprehension, or explicit loops.
这篇关于在 Python 中创建 2D 点列表的更优雅的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文