How to remove extra ',' in tuple in django(python)

后端 未结 5 1292
长情又很酷
长情又很酷 2021-01-06 07:45

I have one problem in building a list of id using django. if i choose more than 1 id it ok but if i choose only one it will produce extra \',\' in list.

test         


        
5条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-06 07:57

    thats' how python displays the tuples with single values , also note that the recommended syntax for creating a tuple with single value is also x = (1,) that helps in differentiating a tuple from a function call . If you really want an output like you describe you can try

    testa = tuple([k['id'] for k in queryset.values('id')])
    if len(testa)==1:
         print '(%s)'%testa[0]
    else:
         print testa
    

提交回复
热议问题