1

样品我有一个清单:

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

我希望我的 session['sample'] 的值是 =a, b, c, d

4

1 回答 1

3

您可以在一行中执行此操作,而不是使用join()和列表理解的循环:

self.session['sample'] = ", ".join(str(item) for item in sample)

如果您乐于循环执行此操作,则需要制作第一项""而不是 None:

self.session['sample'] = "" # Empty string
for item in sample:
   self.session['sample'] += "," + str(item) # Note I've used += here 

a += 1只是一种整洁的写作方式a = a + 1

于 2013-01-08T05:44:20.157 回答