python坐标系建立_在Python中创建2D坐标图

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.

解决方案from itertools import product

coordinates = list(product(xrange(width), xrange(height)))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值