2012-03-26 72 views
1

我正在更改django-taggit以便它不区分大小写。python集合中的替换/更改项目

我想获取一个项目的标签列表,检查它们是否存在不同的情况,如果存在,请用标签的替代案例替换找到的标签。

for t in tags: 
    existing_tag = self.through.tag_model().objects.get(name__iexact=t) 
    if existing_tag: 
     #Replace t in tags with existing_tag 

如何写最后一行?我需要用标记的预先存在的情况来替换集合“标记”中的标记的打印版本。我该如何更换一套中的物品?

回答

1

不要修改tags,创建一个新版本。

new_tags = [] 
for t in tags: 
    existing_tag = self.through.tag_model().objects.get(name__iexact=t) 
    if existing_tag: 
     new_tags.append(existing_tag) 
    else: 
     new_tags.append(t) 
tags = set(new_tags) 
相关问题