Python 向元组添加项 [英] Python add item to the tuple

查看:44
本文介绍了Python 向元组添加项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些 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,)

这篇关于Python 向元组添加项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆