如何计算列表中的元素的重复python,django [英] how to count the repetition of the elements in a list python, django
问题描述
我有一个django应用程式,我使用 django-taggit
为我的网志。
tags = [< Tag:some>,< Tag:here>,< Tag:tags>,< Tag:some> ;,< Tag:created> ;,< Tag:here> b
现在如何找到列表中每个元素的计数,并返回如下的元组列表
结果应该如下:
; Tag:some>,2),(< Tag:here>,2),(< Tag:created>,1),(< Tag:tags>,2)]
以便我可以通过循环使用它们在模板中。
查看
def display_list_of_tags(request):
tags = [< Tag:some> ;< Tag:here>,< Tag:tags>,< Tag:tags>,< Tag:some> ;,< Tag:created> ;,< Tag:here> ;,< Tag:tags>]
#After在如上所示的上面的列表上做一些操作
tags_with_count = [(< Tag:some>,2),(< Tag:here>,2),(< Tag:created>,1) < Tag:tags>,2)]
return HttpResponse('some_template.html',dict(tags_with_count:tags_with_count))
模板
{tag_obj在tag_with_count% b $ b< a href ={%url'tag_detail'tag_obj%}> {{tag_obj}}< / a> < span> count:{{tags_with_count [tag_obj]}}< / span>
{%endfor%}
,如上所述如何计算每个元素的出现在列表中?
该过程最终应该是快速的,因为我可能在标记应用程序中有数百个标签?
如果列表只包含字符串作为元素,我们可以使用类似于
的集合import counter
并计算计数,但在上述情况下如何做?
我的意图是计数发生,并在模板中打印它们,如
标签对象和出现
,
寻找快速高效的方式来执行上述功能?
编辑:
所以我从
得到了必要的答案,我通过将结果元组列表
转换为字典,将结果发送到模板{< Tag:created> ;: 1,< Tag:some> ;: 2,< Tag:here&标签>:2}
并试图通过循环打印上述字典, p>
{tags_with_count%}中的tag_obj的%}
< a href ={%url'tag_detail'tag_obj%} > {{tag_obj}}< / a> < span> count:{{tags_with_count [tag_obj]}}< / span>
{%endfor%}
但它显示以下错误
TemplateSyntaxError:无法解析余数:'tag_obj]'from'tags_with_count [tag_obj]'
那么如何使用键和值来显示django模板中的字典?
可以改变上面的模板循环,如下所示:
{%for tag_obj,count in tags_with_count.iteritems%}
尝试Python的计数器:
来自集合import Counter
l = ['some','here','tags','some','created','here','tags']
print(Counter(l).items
输出:
code> [('created',1),('some',2),('here',2),('tags',2)]
/ pre>I have a django app, i am using
django-taggit
for my blog.Now i have a list of elements(In fact objects) that i got from database in one of my view as below
tags = [<Tag: some>, <Tag: here>, <Tag: tags>, <Tag: some>, <Tag: created>, <Tag: here>, <Tag: tags>]
Now how to find the count of each element in the list and return a list of tuples as below
result should be as below
[(<Tag: some>,2),(<Tag: here>,2),(<Tag: created>,1),(<Tag: tags>,2)]
so that i can use them in template by looping it something like below
view
def display_list_of_tags(request): tags = [<Tag: some>, <Tag: here>, <Tag: tags>, <Tag: some>, <Tag: created>, <Tag: here>, <Tag: tags>] # After doing some operation on above list as indicated above tags_with_count = [(<Tag: some>,2),(<Tag: here>,2),(<Tag: created>,1),(<Tag: tags>,2)] return HttpResponse('some_template.html',dict(tags_with_count:tags_with_count))
template
{% for tag_obj in tags_with_count %} <a href="{% url 'tag_detail' tag_obj %}">{{tag_obj}}</a> <span>count:{{tags_with_count[tag_obj]}}</span> {% endfor %}
so as described above how to count the occurences of each element in the list ? The process should be ultimately fast, because i may have hundreds of tags in the tagging application right ?
If the list contains only strings as elements, we could use something like
from collections import counter
and calculate the count, but how do do in the above case ?All my intention is to count the occurences and print them in the template like
tag object and occurences
,So i am searching for a fast and efficient way to perform above functionality ?
Edit:
So i got the required answer from and i am sending the result to template by converting the resultant
list of tuples
to dictionary as below{<Tag: created>: 1, <Tag: some>: 2, <Tag: here>: 2, <Tag: tags>: 2}
and tried to print the above dictionary by looping it in the format like
{% for tag_obj in tags_with_count %} <a href="{% url 'tag_detail' tag_obj %}">{{tag_obj}}</a> <span>count:{{tags_with_count[tag_obj]}}</span> {% endfor %}
But its displaying the below error
TemplateSyntaxError: Could not parse the remainder: '[tag_obj]' from 'tags_with_count[tag_obj]'
So how to display the dictionary in the django templates by like key and value ?
Done we can change the above template looping as below
{% for tag_obj, count in tags_with_count.iteritems %}
解决方案Try Python's Counter:
from collections import Counter l = ['some', 'here', 'tags', 'some', 'created', 'here', 'tags'] print(Counter(l).items())
Output:
[('created', 1), ('some', 2), ('here', 2), ('tags', 2)]
这篇关于如何计算列表中的元素的重复python,django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!