Python向tuple添加项

时间:2022-07-04 07:32:35

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.

我有一些对象。ID-s,我将它作为元组存储在用户会话中。当我添加第一个时,它可以工作,但是tuple看起来像(u'2'),但是当我尝试使用mytuple = mytuple + new添加新一个时。id有错误只能将tuple(不是“unicode”)连接到tuple。

6 个解决方案

#1


181  

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

你需要把第二个元素做成一个1-tuple,例如:

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

#2


22  

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.

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

#3


18  

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

由于Python 3.5 (PEP 448),您可以在tuple、list set和dict类型中进行拆包:

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

#4


10  

>>> 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


9  

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

Tuple只能允许向它添加Tuple。最好的办法是:

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')

#6


-1  

#1 form

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

#2 form

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

#1


181  

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

你需要把第二个元素做成一个1-tuple,例如:

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

#2


22  

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.

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

#3


18  

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

由于Python 3.5 (PEP 448),您可以在tuple、list set和dict类型中进行拆包:

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

#4


10  

>>> 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


9  

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

Tuple只能允许向它添加Tuple。最好的办法是:

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')

#6


-1  

#1 form

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

#2 form

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