簡體   English   中英

如何將字符串和元組的元組轉換為字符串和列表的列表

[英]How to convert a tuple of string and tuples into a list of string and lists

我有一個字典鍵,它是一個字符串和元組的元組。 典型的關鍵看起來像

(“string1”,('string2','string3','string4'),(int1,int2,int3))

我現在需要將密鑰復制到列表中以修改string2和int2,然后將其重新轉換回新元組,然后將其用作新條目的字典鍵。

當我嘗試使用此元組制作列表時

listKey = [list(i) for i in key]

我得到一個看起來像的輸出

[['s','t','r','i','n','g','1'],['string2','string3','string4'],[int1,int2, INT3]

  1. 當我將其轉換為列表時,如何保留原始字典鍵中的'string1'?
  2. 當修改后的列表轉換回元組時,如何保留'string1'?

您可能只需要調用list(i)如果沒有,您可以嘗試(("string1"),('string2', 'string3', ...) ...)

或者你可以使用這個:

listKey = [i if isinstance(i, str) else list(i) for i in key]

希望這有助於解決您的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM