我试图将元组转换为Python的列表

发布于 2025-02-11 02:46:01 字数 269 浏览 1 评论 0原文

我正在尝试将其转动:

('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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只涨不跌 2025-02-18 02:46:01

您可以使用list()函数:

tup = ('ab', 'bd', 'cd')
lst = list(tup)
print(lst)

输出:

['ab', 'bd', 'cd']

编辑:

如果要获取输出,则由于元素是不可变的(不变),因此要复杂一些,因此我们需要创建一个新的元组这将存储新的更改:

origTup = ('a','b',['a1','b1','b3',('a2',('ab','bd','cd'),'b2','c2')])
origLst = list(origTup) #convert origTup to a list so we can edit it

partialLst = list(origLst[2][3]) #extract ('a2',('ab','bd','cd'),'b2','c2') and change it to a list
partialLst[1] = list(partialLst[1]) #change ('ab','bd','cd') to a list
partialTup = tuple(partialLst) #convert ['a2',['ab','bd','cd'],'b2','c2'] back to a tuple

origLst[2][3] = partialTup #put the tuple back into our origLst

newTup = tuple(origLst) #create a new tuple that converts our origLst to a tuple

print(newTup)

输出:

('a', 'b', ['a1', 'b1', 'b3', ('a2', ['ab', 'bd', 'cd'], 'b2', 'c2')])

首先,我们将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:

tup = ('ab', 'bd', 'cd')
lst = list(tup)
print(lst)

Output:

['ab', 'bd', 'cd']

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:

origTup = ('a','b',['a1','b1','b3',('a2',('ab','bd','cd'),'b2','c2')])
origLst = list(origTup) #convert origTup to a list so we can edit it

partialLst = list(origLst[2][3]) #extract ('a2',('ab','bd','cd'),'b2','c2') and change it to a list
partialLst[1] = list(partialLst[1]) #change ('ab','bd','cd') to a list
partialTup = tuple(partialLst) #convert ['a2',['ab','bd','cd'],'b2','c2'] back to a tuple

origLst[2][3] = partialTup #put the tuple back into our origLst

newTup = tuple(origLst) #create a new tuple that converts our origLst to a tuple

print(newTup)

Output:

('a', 'b', ['a1', 'b1', 'b3', ('a2', ['ab', 'bd', 'cd'], 'b2', 'c2')])

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文