问题:Python将项目添加到元组

我有一些object.ID-s,我尝试将其作为元组存储在用户会话中。当我添加第一个时,它可以工作,但是元组看起来像,(u'2',)但是当我尝试使用mytuple = mytuple + new.idgot error 添加新的时can only concatenate tuple (not "unicode") to tuple

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.


回答 0

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

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

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

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

回答 1

从Python 3.5(PEP 448)开始,您可以在元组,列表集和字典中进行解包:

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

Since Python 3.5 (PEP 448) you can do unpacking within a tuple, list set, and dict:

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

回答 2

从元组到列表再到元组:

a = ('2',)
b = 'b'

l = list(a)
l.append(b)

tuple(l)

或附有较长的项目清单

a = ('2',)
items = ['o', 'k', 'd', 'o']

l = list(a)

for x in items:
    l.append(x)

print tuple(l)

给你

>>> 
('2', 'o', 'k', 'd', 'o')

这里的重点是:列表是可变的序列类型。因此,您可以通过添加或删除元素来更改给定列表。元组是不可变的序列类型。您不能更改元组。因此,您必须创建一个新的

From tuple to list to tuple :

a = ('2',)
b = 'b'

l = list(a)
l.append(b)

tuple(l)

Or with a longer list of items to append

a = ('2',)
items = ['o', 'k', 'd', 'o']

l = list(a)

for x in items:
    l.append(x)

print tuple(l)

gives you

>>> 
('2', 'o', 'k', 'd', 'o')

The point here is: List is a mutable sequence type. So you can change a given list by adding or removing elements. Tuple is an immutable sequence type. You can’t change a tuple. So you have to create a new one.


回答 3

元组只能允许添加tuple到其中。最好的方法是:

mytuple =(u'2',)
mytuple +=(new.id,)

我使用以下数据尝试了相同的情况,但似乎一切正常。

>>> mytuple = (u'2',)
>>> mytuple += ('example text',)
>>> print mytuple
(u'2','example text')

Tuple can only allow adding tuple to it. The best way to do it is:

mytuple =(u'2',)
mytuple +=(new.id,)

I tried the same scenario with the below data it all seems to be working fine.

>>> mytuple = (u'2',)
>>> mytuple += ('example text',)
>>> print mytuple
(u'2','example text')

回答 4

>>> x = (u'2',)
>>> x += u"random string"

Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    x += u"random string"
TypeError: can only concatenate tuple (not "unicode") to tuple
>>> x += (u"random string", )  # concatenate a one-tuple instead
>>> x
(u'2', u'random string')
>>> x = (u'2',)
>>> x += u"random string"

Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    x += u"random string"
TypeError: can only concatenate tuple (not "unicode") to tuple
>>> x += (u"random string", )  # concatenate a one-tuple instead
>>> x
(u'2', u'random string')

回答 5

#1表格

a = ('x', 'y')
b = a + ('z',)
print(b)

#2表格

a = ('x', 'y')
b = a + tuple('b')
print(b)

#1 form

a = ('x', 'y')
b = a + ('z',)
print(b)

#2 form

a = ('x', 'y')
b = a + tuple('b')
print(b)

回答 6

最简单的说,添加到元组的最简单方法是用括号和逗号将要添加的元素括起来。

t = ('a', 4, 'string')
t = t + (5.0,)
print(t)

out: ('a', 4, 'string', 5.0)

Bottom line, the easiest way to append to a tuple is to enclose the element being added with parentheses and a comma.

t = ('a', 4, 'string')
t = t + (5.0,)
print(t)

out: ('a', 4, 'string', 5.0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。