logo

Llista d'unió a Python

En aquest tema, parlarem de com podem unir dues o més llistes amb diferents funcions de Python. Abans de passar pels conceptes, fem una breu introducció a la llista de Python. A Llista de Python és la col·lecció de múltiples elements que s'agrupen amb el mateix nom. Pot emmagatzemar diferents tipus de dades (enter, cadena, flotant, etc.) dins d'un claudàtor [], que està separat per una coma (,).

Llista d'unió a Python

Programa per imprimir la llista de Python

List.py

 # list of characters List1 = ['A', 'B', 'C', 'D', 'E'] # list of integers List2 = [1, 2, 3, 4, 5,] # mixed lists List3 = ['A', 1, 'C', 'E', 5, 8] print (' Display the List1 ', List1) print (' Display the List2 ', List2) print (' Display the List3 ', List3) 

Sortida

 Display the List1 ['A', 'B', 'C', 'D', 'E'] Display the List2 [1, 2, 3, 4, 5] Display the List3 ['A', 1, 'C', 'E', 5, 8] 

Quan unim dues o més llistes juntes en a Python programa, ofereix llistes unides. I aquest procés s'anomena composició o unió de llistes.

Parlem de les diferents maneres d'unir dues o més llistes a Python:

  • Uniu llistes a Python mitjançant la funció join() i els delimitadors
  • Uniu-vos a una llista en Python mitjançant la funció join() sense delimitadors
  • Uniu dos llistats d'enters a Python mitjançant la funció map().
  • Uniu dues llistes a Python utilitzant la funció for loop i append().
  • Uniu diverses llistes a Python mitjançant el mètode itertools.chain().
  • Uniu dues llistes a Python mitjançant l'operador (+) més
  • Uniu dues llistes a Python mitjançant l'operador de multiplicació (*) o asterisc
  • Uniu dues llistes a Python mitjançant la funció extend().

Uniu llistes a Python mitjançant la funció join().

A uneix-te () La funció s'utilitza per unir una llista iterable a una altra llista, separada per delimitadors especificats com ara comes, símbols, un guionet, etc.

Sintaxi

 str_name.join( iterable) 

nom_str: És el nom del delimitador que separa una llista iterable.

iterable: És la llista que conté un conjunt d'elements i s'uneix amb un delimitador.

Valor de retorn: Retorna una llista concatenada que està separada per delimitadors especificats.

Nota: si la llista iterable conté valors o elements que no siguin de cadena, genera una excepció TypeError.

Programa per unir dues llistes mitjançant la funció join() i el delimitador

Join.py

 List1 = [ 'Apple', 'Orange', 'Banana', 'Mango', 'Grapes' ] Str2 = ', ' # It is the comma delimiter # use join() function to join List1 with the ' . ' delimiter Str2 = Str2.join( List1) # print the join list print (' Display the concatenated List1 using join() function and delimiter', Str2) List2 = [ 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday' ] Str3 = ' - ' # It is the hyphen delimiter # use join() function to join List2 with the ' - ' delimiters Str3 = Str3.join( List2) # print the join list print (' Display the concatenated List2 using join() function and delimiter', Str3) 

Sortida

 Display the concatenated List1 using join() function and delimiter Apple, Orange, Banana, Mango, Grapes Display the concatenated List2 using join() function and delimiter Sunday - Monday - Tuesday - Wednesday - Thursday 

Programa per unir-se a una llista sense utilitzar delimitador

Prog.py

 # declare a python list Lt1 = [ 'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't' ] print ( ' Display the elements of the List L1 ' , Lt1) L2 = ' ' # declare any empty string without defining any delimiter Ret = L2.join( Lt1) # use join method to join L1 list with L2 print ( ' Display the List without using delimiters', Ret) 

Sortida

 Display the elements of the List L1 ['j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't'] Display the List without using delimiters j a v a t p o i n t 

Uniu dos llistats d'enters mitjançant la funció map().

Llista d'enters: Recull tots els nombres enters en una llista anomenada llista d'enters, i no podem unir dues llistes d'enters a Python mitjançant la funció join(). Per tant, fem servir a mapa () funció que converteix una llista d'enters en una cadena. Després d'això, utilitzem una funció join() per unir els resultats de la funció map() amb els delimitadors adequats.

Sintaxi:

 map(str, list_name) 

A la sintaxi anterior, una funció map() té dos paràmetres, list_name i str. On nom_lista és el nom de la llista d'enters i str representa la cadena. Una funció map() converteix el nom_lista en la cadena (str).

tutorial de swing de java

Programa per utilitzar una funció map() i join() a la llista

Creem un programa per convertir la llista d'enters donada en una cadena utilitzant la funció map() i després la funció join() per unir-se a la llista.

Convert.py

 lt = [1, 2, 3, 4, 5] # use map() function to convert integer list into string list_map = map(str, lt) lt2 = ', ' # use join() function to join lists and delimiter comma (,) res = lt2.join (list_map) print (' Display the concatenated integers list using map() and join() function ', res) 

Sortida

 Display the concatenated integers list using map() and join() function 1, 2, 3, 4, 5 

Programa per unir dues llistes a Python utilitzant la funció for loop i append().

An adjuntar La funció () s'utilitza per afegir o unir seqüencialment cada element d'una llista iterable al final d'una altra llista mitjançant el bucle for. Creem un programa senzill per afegir elements d'una llista al final d'una altra llista mitjançant la funció append().

Append.py

 List1 = [1, 2, 3, 4, 5] # declare List1 List2 = [5, 6, 7, 8, 9, 10] # declare List2 print (' Given List1 ', List1) print (' Given List2 ', List2) # use for loop to iterate each element of Lt1 to l2 for i in List2: List1.append(i) # use append() function to insert each elements at the end of Lt1 print (' Display concatenation list using append() function ', List1) 

Sortida

 Given List1 [1, 2, 3, 4, 5] Given List2 [5, 6, 7, 8, 9, 10] Display concatenation list using append() function [1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 10] 

Programa per unir diverses llistes mitjançant el mètode itertools.chain().

Creem un programa senzill en Python per concatenar diverses llistes utilitzant el cadena () mitjançant la importació del itertools paquet.

New.py

mètode tostring en java
 # use Python itertools.chain() method to join two list import itertools # declare different lists a = [1, 2, 3, 4, 5] b = [6, 7, 8, 9, 10] c = [11, 12, 13, 14, 15] print (' Display the first list ', a) print (' Display the second list ', b) print (' Display the third list ', c) # use itertools.chain() method to join the list result = list (itertools.chain (a, b, c)) # pass the result variable in str() function to return the concatenated lists print (' Concatenated list in python using itertools.chain() method ', str (result)) 

Sortida

 Display the first list [1, 2, 3, 4, 5] Display the second list [6, 7, 8, 9, 10] Display the third list [11, 12, 13, 14, 15] Concatenated list in python using itertools.chain() method [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] 

Programa per unir dues llistes mitjançant l'operador +

Considerem un exemple per unir dues llistes a Python mitjançant l'operador (+) més.

Mypro.py

 # Create a program to join two lists in Python using the '+' operator # declare two lists of characters list1 = [ 'A', 'B', 'C', 'D', 'E'] list2 = [ 'F', 'G', 'H', 'I', 'J'] # join two characters lists using '+' operator lt_sum1 = list1 + list2 # declares two lists of integers list3 = [ '1', '2', '3', '4', '5'] list4 = [ '6', '7', '8', '9', '10'] # join two integers lists using '+' operator lt_sum2 = list3 + list4 # display the concatenation list print (' Join two list of characters in Python using + operator: ', str(lt_sum1)) # display the concatenation list print (' Join two list of integers in Python using + operator: ', str(lt_sum2)) 

Sortida

 Join two list of characters in Python using + operator: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'] Join two list of integers in Python using + operator: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'] 

Programa per unir dues llistes mitjançant l'operador de multiplicació (*)

Considereu un exemple per unir dues llistes de en Python mitjançant l'operador *.

Mypro2.py

 # declare two lists of characters List1 = [ 'A', 'B', 'C', 'D', 'E'] List2 = [ 'F', 'G', 'H', 'I', 'J'] print (' Display character List1 ', List1) print (' Display character List2 ', List2) # join two characters lists using '*' operator lt_sum1 = [*List1, *List2] # declares two lists of integers List3 = [ 1, 2, 3, 4, 5] List4 = [ 6, 7, 8, 9, 10] print (' Display integer List3 ', List3) print (' Display integer List4 ', List4) # join two integers lists using '*' operator lt_sum2 = [*List3, *List4] # display the concatenation list print (' Join two characters list in Python using * operator: '+ str(lt_sum1)) # display the concatenation list print (' Join two integers list in Python using * operator: '+ str(lt_sum2)) 

Sortida

 Display integer List3 [1, 2, 3, 4, 5] Display integer List4 [6, 7, 8, 9, 10] Join two characters list in Python using * operator: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'] Join two integers list in Python using * operator: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 

Programa per unir dues llistes a Python mitjançant el mètode extend().

Escrivim un programa senzill per unir dues llistes mitjançant el mètode extend() a Python.

Prog.py

 # takes two integers lists List1 = [5, 10, 5] List2 = [ 2, 4, 6, 8] print (' Display the List1 ', List1) print (' Display the List1 ', List2) # takes two string lists List3 = [ 'RED', 'BLUE', 'BLACK'] List4 = [ 'BROWN', 'PURPLE', 'GREY' ] print (' Display the List3 ', List3) print (' Display the List4 ', List4) # use extend() method to join two lists List1.extend(List2) List3.extend(List4) # print concatenation lists print( '
 Adding two lists of integers in Python using the extend() function: ', str(List1)) print( '
 Adding two lists of strings in Python using the extend() function: ', str(List3)) 

Sortida

 Display the List1 [5, 10, 5] Display the List1 [2, 4, 6, 8] Display the List3 ['RED', 'BLUE', 'BLACK'] Display the List4 ['BROWN', 'PURPLE', 'GREY'] Adding two lists of integers in Python using the extend() function: [5, 10, 5, 2, 4, 6, 8] Adding two lists of strings in Python using the extend() function: ['RED', 'BLUE', 'BLACK', 'BROWN', 'PURPLE', 'GREY']