更优雅的方式来创建2D点在Python列表 [英] More elegant way to create a list of 2D points in Python
本文介绍了更优雅的方式来创建2D点在Python列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要创建2D点(X,Y)在python的列表。 这将做到这一点
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))
所以:打印→
将产生:
[(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?
推荐答案
使用<一个href="http://docs.python.org/library/itertools.html#itertools.product"><$c$c>itertools.product$c$c>:
from itertools import product
l = list(product(range(30,50,5), range(1,10,3)))
据扩展更好,应该比一台发电机EX pression,列出COM prehension,或显式循环。
It scales better and should be faster than a generator expression, list comprehension, or explicit loops.
这篇关于更优雅的方式来创建2D点在Python列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文