Nos dan una lista, la tarea es convertir la lista en una tupla de listas.
Input: ['Geeks', 'For', 'geeks'] Output: (['Geeks'], ['For'], ['geeks']) Input: ['first', 'second', 'third'] Output: (['first'], ['second'], ['third'])
Método #1: Usando la Comprensión
# 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)
Producción:
(['Geeks'], ['for'], ['geeks'])
Método #2: Usando 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)
Producción:
(['first'], ['second'], ['third'])
Método #3: Usar Mapa + 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)
Producción:
(['first'], ['second'], ['third'])
Publicación traducida automáticamente
Artículo escrito por everythingispossible y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA