将另一个元组添加到元组的元组中 [英] Add another tuple to a tuple of tuples

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

问题描述

我有以下元组元组:

my_choices=(
         ('1','first choice'),
         ('2','second choice'),
         ('3','third choice')
)

我想在它的开头添加另一个元组

and I want to add another tuple to the start of it

another_choice = ('0', 'zero choice')

我该怎么做?

结果是:

final_choices=(
             ('0', 'zero choice')
             ('1','first choice'),
             ('2','second choice'),
             ('3','third choice')
    )

推荐答案

another_choice 构建另一个元组的元组,然后连接:

Build another tuple-of-tuples out of another_choice, then concatenate:

final_choices = (another_choice,) + my_choices

或者,考虑使用方括号而不是圆括号使 my_choices 成为元组列表而不是元组元组:

Alternately, consider making my_choices a list-of-tuples instead of a tuple-of-tuples by using square brackets instead of parenthesis:

my_choices=[
     ('1','first choice'),
     ('2','second choice'),
     ('3','third choice')
]

那么你可以简单地做:

my_choices.insert(0, another_choice)

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

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