Hi, I want to transform my articles_list5 list comprehension back to a traditional looping construct to make things easier to read.
The articles_list5 returns a list of tuples.
But when I try to do the same using traditional looping in articles_list6 then I just get one long list from everything. How do I transform list5 to list6 correctly so it still returns a list of tuples?
>>> articles_list5 = [(article.id, article, article.num_publications, uncommented_publications, float(article.num_publications)/total_publicists*100) for article in articles]
List of tuples - :)
>>> articles_list5
[(1, <Article: Django lets you build Web apps easily>, 1, [<Publication: Popular Science>, <Publication: Science News>], 20.0),
(4, <Article: Happy new year 2014!>, 1, [<Publication: Popular Science>, <Publication: Science News>], 20.0),
(2, <Article: NASA uses Python>, 5, [<Publication: Popular Science>, <Publication: Science News>], 100.0),
(3, <Article: Twitter IPO!>, 2, [<Publication: Popular Science>, <Publication: Science News>], 40.0)]
>>> articles_list6 = []
>>>
>>> for x in articles:
... uncommented = Publication.objects.filter(article__pk=x.id).filter(comment='').count()
#articles_list6.extend([x.id, x, x.num_publications, uncommented])
... articles_list6.extend(tuple((x.id, x, x.num_publications, uncommented)))
...
>>>
List of everything - :(
>>> articles_list6
[1, <Article: Django lets you build Web apps easily>, 1, 0,
4, <Article: Happy new year 2014!>, 1, 0,
2, <Article: NASA uses Python>, 5, 2,
3, <Article: Twitter IPO!>, 2, 1]
>>>
-- The articles_list5 returns a list of tuples.
But when I try to do the same using traditional looping in articles_list6 then I just get one long list from everything. How do I transform list5 to list6 correctly so it still returns a list of tuples?
>>> articles_list5 = [(article.id, article, article.num_publications, uncommented_publications, float(article.num_publications)/total_publicists*100) for article in articles]
List of tuples - :)
>>> articles_list5
[(1, <Article: Django lets you build Web apps easily>, 1, [<Publication: Popular Science>, <Publication: Science News>], 20.0),
(4, <Article: Happy new year 2014!>, 1, [<Publication: Popular Science>, <Publication: Science News>], 20.0),
(2, <Article: NASA uses Python>, 5, [<Publication: Popular Science>, <Publication: Science News>], 100.0),
(3, <Article: Twitter IPO!>, 2, [<Publication: Popular Science>, <Publication: Science News>], 40.0)]
>>> articles_list6 = []
>>>
>>> for x in articles:
... uncommented = Publication.objects.filter(article__pk=x.id).filter(comment='').count()
#articles_list6.extend([x.id, x, x.num_publications, uncommented])
... articles_list6.extend(tuple((x.id, x, x.num_publications, uncommented)))
...
>>>
List of everything - :(
>>> articles_list6
[1, <Article: Django lets you build Web apps easily>, 1, 0,
4, <Article: Happy new year 2014!>, 1, 0,
2, <Article: NASA uses Python>, 5, 2,
3, <Article: Twitter IPO!>, 2, 1]
>>>
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/667e0a90-fd1e-4306-b074-307cb1dcf157%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment