Python 奇怪的词典分配问题
我有两个模型、相册和图片以及一个函数Python 奇怪的词典分配问题,python,dictionary,Python,Dictionary,我有两个模型、相册和图片以及一个函数album\u reorder,该函数接收图片ID列表并相应地更改权重。这很好,但是我正在尝试为函数编写测试,例如: Class ReposTests(unittest.TestCase): def test_album_reorder__fail__not_owned(self): # Create a test user, album and pictures. user = self.create_test_us
album\u reorder
,该函数接收图片ID列表并相应地更改权重。这很好,但是我正在尝试为函数编写测试,例如:
Class ReposTests(unittest.TestCase):
def test_album_reorder__fail__not_owned(self):
# Create a test user, album and pictures.
user = self.create_test_user()
# create_default_album() generates an Album along with 6 default Picture records.
create_default_album(user)
album = get_first_album(user.profile)
pictures = Picture.objects.filter(album=album)
# Get all picture IDs into a list and record each pictures existing weight.
picture_ids = []
picture_weights = {}
for picture in pictures:
picture_ids.append(picture.id)
print "%s:%s:%s" % (album.id, picture.id, picture.weight)
picture_weights[picture.id] = picture.weight
# Create another test user, album and pictures.
user2 = self.create_test_user()
create_default_album(user2)
album2 = get_first_album(user2.profile)
pictures2 = Picture.objects.filter(album=album2)
# Add a Picture ID from a different user to test the restriction that an album cannot be re-ordered if any pictures do not belong to the specified user.
picture_ids.append(pictures2[0].id)
print "%s:%s:%s" % (album2.id, pictures2[0].id, pictures2[0].weight)
picture_weights[pictures[2].id] = pictures2[0].weight
# Shuffle all picture IDs
random = Random()
while pictures[0].id == picture_ids[0]:
random.shuffle(picture_ids)
# Reorder picture IDs according to shuffled ID list, however this should fail due to the ownership check.
album_reorder(user, picture_ids)
print picture_ids
print picture_weights
# Check each
for picture_id in picture_ids:
self.assertEqual(picture_weights[picture_id], Picture.objects.get(id=picture_id).weight)
现在问题似乎出在picture\u weights
dict的构造方式上。由于以下两行输出:
# Loop output: (album.id, picture.id, picture.weight)
# 1346:5699:0
# 1346:5700:1
# 1346:5701:2
# 1346:5702:3
# 1346:5703:4
# 1346:5704:5
# 1347:5705:0
print picture_ids
# Outputs: [5700L, 5703L, 5702L, 5699L, 5704L, 5705L, 5701L]
print picture_weights
# Outputs: {5699L: 0L, 5700L: 1L, 5701L: 0L, 5702L: 3L, 5703L: 4L, 5704L: 5L}
请注意,picture\u id
与预期一样长7个元素,其中aspicture\u权重
仅为6个元素,权重与循环输出的权重不匹配。第二张用户图片5705
在picture\u权重中丢失
,但是其中一张picture\u权重
被分配了不正确的权重0L
我对dict一点经验都没有,但是,我只是想应用我对PHP关联数组的知识,所以我假设我在这方面的知识还有差距。谢谢你的建议 列表和词典在以下几行出现分歧:
# Add a Picture ID from a different user to test the restriction that an album cannot be re-ordered if any pictures do not belong to the specified user.
picture_ids.append(pictures2[0].id)
print "%s:%s:%s" % (album2.id, pictures2[0].id, pictures2[0].weight)
picture_weights[pictures[2].id] = pictures2[0].weight
注意您是如何使用图片[2].id
,即5701L
(这就是特定键在字典中的权重发生变化的原因)。你的意思可能是:
picture_weights[pictures2[0].id] = pictures2[0].weight
列表和词典在以下几行出现分歧:
# Add a Picture ID from a different user to test the restriction that an album cannot be re-ordered if any pictures do not belong to the specified user.
picture_ids.append(pictures2[0].id)
print "%s:%s:%s" % (album2.id, pictures2[0].id, pictures2[0].weight)
picture_weights[pictures[2].id] = pictures2[0].weight
注意您是如何使用图片[2].id
,即5701L
(这就是特定键在字典中的权重发生变化的原因)。你的意思可能是:
picture_weights[pictures2[0].id] = pictures2[0].weight
为什么你有一行
图片权重[pictures[2]。id]=pictures2[0]。权重
而不是图片权重[pictures2[0]。id]=pictures2[0]。权重
?为什么你有一行图片权重[pictures[2]。id]=pictures2[0]。权重
而不是图片权重[pictures2[0]。id]=pictures2[0]。权重
?修复!很抱歉,没有担心,这会发生在我们所有人身上,修正了!对不起,勃鲁什不担心,这件事发生在我们所有人身上。