【问题标题】:can we change the list in a nested tuple to tuple我们可以将嵌套元组中的列表更改为元组吗
【发布时间】:2017-02-21 06:28:25
【问题描述】:
t = (1,2,3,4,5,6,[7,8])

type(t[6])
<type 'list'>

tuple(t[6])
(7, 8)

type(tuple(t[6]))
<type 'tuple'>

我想在元组“t”中反映这种变化

如何将其更改为 t = (1,2,3,4,5,6,(7,8))

【问题讨论】:

    标签: python-2.7 list tuples


    【解决方案1】:

    元组是不可变的,你需要构造一个新的元组,例如将t 更改为可变类型list 更新值并转换回tuple

    >>> x = list(t)
    >>> x[6] = tuple(x[6])
    >>> tuple(x)
    (1, 2, 3, 4, 5, 6, (7, 8))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-11
      • 1970-01-01
      • 2023-04-03
      • 2012-02-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多