📜  Python|将列表转换为列表元组

📅  最后修改于: 2022-05-13 01:54:59.228000             🧑  作者: Mango

Python|将列表转换为列表元组

我们得到一个列表,任务是将列表转换为列表的元组。

Input: ['Geeks', 'For', 'geeks']
Output: (['Geeks'], ['For'], ['geeks'])

Input: ['first', 'second', 'third']
Output: (['first'], ['second'], ['third'])


方法#1:使用理解

# Python code to convert a list into tuple of lists
  
# Initialisation of list
Input = ['Geeks', 'for', 'geeks']
  
# Using list Comprehension
Output = tuple([name] for name in Input)
  
# printing output
print(Output)
输出:
(['Geeks'], ['for'], ['geeks'])


方法 #2:使用 Map + Lambda

# Python code to convert a list into tuple of lists
  
# Initialisation of list
Input = ['first', 'second', 'third']
  
# Using map + lambda
Output = tuple(map(lambda x: [x], Input))
  
# printing output
print(Output)
输出:
(['first'], ['second'], ['third'])


方法 #3 :使用 Map + zip

# Python code to convert a list into tuple of lists
  
# Initialisation of list
Input = ['first', 'second', 'third']
  
# Using Map + zip
Output = tuple(map(list, zip(Input)))
  
# printing output
print(Output)
输出:
(['first'], ['second'], ['third'])