示例我有一个清单:
sample = [a, b, c, d]
然后我想将样本传递给会话:
self.session['sample'] = None #declaring the session...
for item in sample:
self.session['sample'] = str(self.session['sample']) + "," + str(item)
但输出是:
无,a,b,c,d
我希望我的会话['sample']的值为= a, b, c, d
答案 0 :(得分:3)
您可以在一行中而不是使用join()
和列表理解的循环执行此操作:
self.session['sample'] = ", ".join(str(item) for item in sample)
如果你在循环中做得很开心,你需要制作第一项""
而不是无:
self.session['sample'] = "" # Empty string
for item in sample:
self.session['sample'] += "," + str(item) # Note I've used += here
a += 1
只是写a = a + 1