繁体   English   中英

是否有更多pythonic方式来填充此列表?

[英]Is there a more pythonic way to populate a this list?

我想从django查询中清除字符串,以便它可以在latex中使用

items = []
items_to_clean = items.objects.get.all().values()
for dic in items_to_clean:
    items.append(dicttolatex(dic))

这是我完成这项任务的标准方法。 这可以通过列表理解以某种方式解决。 因为dicttolatex是一个返回字典的函数。

您可以使用列表append避免重复调用append

items = [dicttolatex(dic) for dic in items_to_clean]

您可以使用地图为什么重新发现轮子

样品:

lst=[1,2,3,4]
def add(n):
    return n+n

a=[]
a.extend( map(add,lst))
print a

输出:

[2, 4, 6, 8]

那是你的情况:

items_to_clean = items.objects.get.all().values()
items = map(dicttolatex,items_to_clean)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM