Python将项目添加到元组

2022-01-20 00:00:00 python python-2.7 tuples

问题描述

我有一些 object.ID-s,我尝试将其作为元组存储在用户会话中.当我添加第一个它可以工作但元组看起来像 (u'2',) 但是当我尝试使用 mytu​​ple = mytuple + new.id 添加新的时出错只能将元组(不是unicode")连接到元组.

I have some object.ID-s which I try to store in the user session as tuple. When I add first one it works but tuple looks like (u'2',) but when I try to add new one using mytuple = mytuple + new.id got error can only concatenate tuple (not "unicode") to tuple.


解决方案

需要将第二个元素设为一元组,例如:

You need to make the second element a 1-tuple, eg:

a = ('2',)
b = 'z'
new = a + (b,)

相关文章