在Python中创建2D坐标图 [英] Creating 2D coordinates map in Python

查看:403
本文介绍了在Python中创建2D坐标图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是在寻找解决方案,我正在寻找更好的解决方案,或者通过使用其他类型的列表理解或其他方式来寻找更好的解决方案。



我需要生成一个2个整数的元组列表来获取地图坐标,如[(1,1),(1,2),...,(x,y)]



所以我有以下内容:

  width,height = 10,5 

解决方案1 ​​

  coordinates = [(x,y)x中的x(宽度)x中的x(高度)] 

解决方案2

  coordinates = [] 
for x in xrange(width):
for y in xrange(height):
coordinates.append((x,y))

解决方案3

  coordinates = [] 
x,y = 0,0
而x<宽度:
而y <高度:
coordinates.append((x,y))
y + = 1
x + = 1

还有其他解决方案吗?
我最喜欢第一个。

解决方案

使用 itertools.product()



<$来自itertools导入产品的p $ p>
coordinates = list(product(xrange(width),xrange(height)))


I'm not looking for solution, I'm looking for a better solution or just a different way to do this by using some other kind of list comprehension or something else.

I need to generate a list of tuples of 2 integers to get map coordinates like [(1, 1), (1, 2), ..., (x, y)]

So I have the following:

width, height = 10, 5

Solution 1

coordinates = [(x, y) for x in xrange(width) for y in xrange(height)]

Solution 2

coordinates = []
for x in xrange(width):
    for y in xrange(height):
        coordinates.append((x, y))

Solution 3

coordinates = []
x, y = 0, 0
while x < width:
    while y < height:
        coordinates.append((x, y))
        y += 1
    x += 1

Are there any other solutions? I like the 1st one most.

解决方案

Using itertools.product():

from itertools import product
coordinates = list(product(xrange(width), xrange(height)))

这篇关于在Python中创建2D坐标图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆