我正在努力把移动的球放到合适的垃圾桶里。我喜欢认为我在正确的轨道上,但我已经被困了一段时间了。
我忽略了那些似乎与我的问题无关的代码,但是如果那些回答的人需要更多的细节,我可以提供他们。基本上,我有200个移动球的世界。它们有一个X和Y坐标。我想把世界分割成正方形的垃圾桶,宽256,然后把球放在合适的垃圾桶里。
我的方法是把它们写进字典里。看起来是这样的:
dict_of_balls = {}
for i in range(len(balls)):
xb = int(balls[i].x/256)
yb = int(balls[i].y/256)我想把键做成(xb, yb)对的元组,然后把合适的球放在垃圾桶里,但我不认为你可以用元组作为键.
守则如下:
import math
import random
import time
import sys
ball_min_radius = 16.0 #world coordinates
ball_max_radius = 128.0 #world coordniates
number_balls = 200
class Ball:
"""
Implements a point/ball
"""
def __init__(self):
self.x = random.uniform(world_min_x,world_max_x)
self.y = random.uniform(world_min_y,world_max_y)
self.radius = int(random.uniform(ball_min_radius,ball_max_radius))
def __lt__(self, other):
return self.id < other.id
def main():
world_min_x = -200.0*number_balls**.5 # minimum x in world coordinates
world_max_x = +200.0*number_balls**.5 # maximum x in world coordinates
world_min_y = -200.0*number_balls**.5 # minimum y in world coordinates
world_max_y = +200.0*number_balls**.5 # maximum y in world coordinates
balls = [Ball() for i in range(number_balls)]那么,有没有人想过如何根据给定的世界坐标把世界划分成垃圾箱呢?我不确定要使用哪种数据结构,因为我不能用元组作为密钥。谢谢您的反馈。
发布于 2016-03-10 01:46:37
你为什么想要一本字典?这是您将如何做到这一点,但请记住,您将只得到一个球每个垃圾桶,因为您是专门地将他们的键是(int,int)和键是唯一的。
如果使用集合,还可以排序(在我的示例中,我按区域标识符排序):
我不知道你这样做是为了什么,但你可以这样做:
import math
import random
import time
import sys
ball_min_radius = 16.0 #world coordinates
ball_max_radius = 128.0 #world coordniates
number_balls = 200
world_min_x = -200.0*number_balls**.5 # minimum x in world coordinates
world_max_x = +200.0*number_balls**.5 # maximum x in world coordinates
world_min_y = -200.0*number_balls**.5 # minimum y in world coordinates
world_max_y = +200.0*number_balls**.5 # maximum y in world coordinates
class Ball:
"""
Implements a point/ball
"""
def __init__(self):
self.x = random.uniform(world_min_x,world_max_x)
self.y = random.uniform(world_min_y,world_max_y)
self.radius = int(random.uniform(ball_min_radius,ball_max_radius))
def __lt__(self, other):
return self.id < other.id
def __str__(self):
return 'x={x} y={y} r={r}'.format(x=self.x, y=self.y, r=self.radius)
def main():
balls = [Ball() for i in range(number_balls)]
dict_of_balls = {}
ball_collection = []
for b in balls:
xb = int(b.x/256)
yb = int(b.y/256)
key = (xb, yb)
dict_of_balls[key] = b
ball_collection.append((key, b))
print 'length of dictionary:{}'.format(len(dict_of_balls.keys()))
print 'length of collection:{}'.format(len(ball_collection))注意,字典中的项比集合少。
您也可以用这种方式打印每一项内容:
for b in ball_collection:
print 'ball region: {r} with coords: {c}'.format(r=b[0], c=b[1])或者,如果你愿意的话,对它们进行排序:
print 'Collections also let you sort the collection by region(s)...'
sorted_list = sorted(ball_collection, key= lambda x: (x[0][0], x[0][1]))
for b in sorted_list:
print 'ball region: {r} with coords: {c}'.format(r=b[0], c=b[1])您也可以很简单地在特定区域获得球:
print '... or get only ones in a specific region'
subset = [b for b in ball_collection if b[0][0] == 1]
for b in subset:
print 'ball region: {r} with coords: {c}'.format(r=b[0], c=b[1])
main()一个集合似乎能做你真正想要的事情。
发布于 2016-03-10 02:07:38
您可以在字典中对键使用元组,因为元组是不可变的。唯一不能用于字典键的数据类型是list []或set {}
**a = {(1,2):'example1', (2,3):'example2'}
>>> a[(1,2)]
'example1'**所以我相信这会使你的问题更容易解决。
https://stackoverflow.com/questions/35905974
复制相似问题