我试图将元组转换为Python的列表
我正在尝试将其转动:
('a','b',['a1','b1','b3',('a2',('ab','bd','bd','cd') ),'b2','c2'))))
中:
('a','b',['a1','b1','b1','b3',('a2',[a2',[ 'ab','bd','cd'],'b2','c2')))
更改('ab'','bd','cd')为['ab' ,'bd','cd']
I'm trying to turn this:
('a','b',['a1','b1','b3',('a2',('ab','bd','cd'),'b2','c2')])
into this:
('a','b',['a1','b1','b3',('a2',['ab','bd','cd'],'b2','c2')])
changing the ('ab', 'bd', 'cd') to ['ab', 'bd', 'cd']
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
list()
函数:输出:
编辑:
如果要获取输出,则由于元素是不可变的(不变),因此要复杂一些,因此我们需要创建一个新的元组这将存储新的更改:
输出:
首先,我们将
origtup
转换为列表,origlst
,因此我们可以编辑它。然后,我们将从我们的列表中提取('a2',('ab','bd','cd'),'b2','c2')
从我们的列表中,然后将其更改为列表:<代码> ['a2',('ab','bd','cd'),'b2','c2']现在我们可以编辑它,我们将更改第一个元素,
> ('ab','bd','cd')
,到列表:['ab','bd','cd']
。然后,我们将再次将外部部分更改为最初的元组:['a2',['ab','bd','cd'],'b2','c2']
进入('a2',['ab','bd','cd'],'b2','c2')
最后,我们在
origlst
中设置了元素代码>到此元组,然后创建一个新的元组来存储我们的更改。我希望这有所帮助!请让我知道您是否需要进一步的帮助或澄清!
You can use the
list()
function:Output:
EDIT:
If you want to get your output, it is a bit more complicated since tuples are immutable (unchangable), so we need to create a new tuple that stores the new change:
Output:
First, we convert
origTup
to a list,origLst
so we can edit it. Then, we will extract('a2',('ab','bd','cd'),'b2','c2')
from our list and change it to a list:['a2',('ab','bd','cd'),'b2','c2']
Now that we can edit it, we will change the first element,
('ab','bd','cd')
, to a list:['ab','bd','cd']
. Then, we will again change the outer portion into a tuple as it originally was:['a2',['ab','bd','cd'],'b2','c2']
into('a2',['ab','bd','cd'],'b2','c2')
Finally, we set the element in our
origLst
to this tuple, and then create a new tuple to store our change.I hope this helped! Please let me know if you need any further help or clarification!