Dada una lista de tuplas, la tarea es encontrar si todas las tuplas tienen la misma longitud.
A continuación se presentan algunas formas de lograr la tarea anterior.
Método #1: Usar la iteración
Python3
# Python code to find whether all # tuple have equal length # Input List initialization Input = [(11, 22, 33), (44, 55, 66)] # printing print("Initial list of tuple", Input) # K Initialization k = 3 flag = 1 # Iteration for tuple in Input: if len(tuple) != k: flag = 0 break # Checking whether all tuple # have length equal to 'K' in list of tuple if flag: print("All tuples have same length") else: print("Tuples does not have same length")
Producción:
Initial list of tuple [(11, 22, 33), (44, 55, 66)] All tuples have same length
Método #2: Usar todo()
Python3
# Python code to find whether all tuple # have equal length # Input list initialization Input = [(11, 22, 33), (44, 55, 66), (11, 23)] k = 2 # Printing print("Initial list of tuple", Input) # Using all() Output =(all(len(elem) == k for elem in Input)) # Checking whether all tuple # have equal length if Output: print("All tuples have same length") else: print("Tuples does not have same length")
Producción:
Initial list of tuple [(11, 22, 33), (44, 55, 66), (11, 23)] Tuples does not have same length
Publicación traducida automáticamente
Artículo escrito por everythingispossible y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA