logo

Python Join List

În acest subiect, vom discuta despre cum putem uni două sau mai multe liste cu diferite funcții ale Python. Înainte de a trece prin concepte, să facem o scurtă introducere în Lista Python. A Lista Python este colecția de articole multiple care sunt grupate cu același nume. Poate stoca diferite tipuri de date (întreg, șir, float etc.) în interiorul unei paranteze drepte [], care este separată prin virgulă (,).

Python Join List

Program pentru a tipări lista 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) 

Ieșire

 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] 

Când unim două sau mai multe liste împreună într-un Piton program, oferă liste unite. Și acest proces se numește alcătuire sau alăturare a listelor.

Să discutăm despre diferitele moduri de a alătura două sau mai multe liste în Python:

  • Uniți liste în Python folosind funcția join() și delimitatorii
  • Alăturați-vă unei liste în Python folosind funcția join() fără delimitatori
  • Uniți două liste de numere întregi în Python folosind funcția map().
  • Uniți două liste în Python folosind funcția for loop și append().
  • Alăturați mai multe liste în Python folosind metoda itertools.chain().
  • Uniți două liste în Python folosind operatorul (+) plus
  • Alăturați două liste în Python folosind (*) operator de înmulțire sau asterisc
  • Uniți două liste în Python folosind funcția extend().

Uniți liste în Python folosind funcția join().

A a te alatura() funcția este utilizată pentru a uni o listă iterabilă la o altă listă, separată de delimitatori specificați, cum ar fi virgulă, simboluri, o cratimă etc.

Sintaxă

 str_name.join( iterable) 

nume_str: Este numele delimitatorului care separă o listă iterabilă.

iterabil: Este lista care conține un set de elemente și se unește cu un delimitator.

Valoare returnată: Returnează o listă concatenată care este separată de delimitatori specificați.

Notă: Dacă lista iterabilă conține valori sau elemente care nu sunt șir, se afișează o excepție TypeError.

Program pentru a uni două liste folosind funcția join() și delimitator

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) 

Ieșire

 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 

Program pentru a se alătura unei liste fără a utiliza delimitator

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) 

Ieșire

 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 

Uniți două liste de numere întregi folosind funcția map().

Lista intregi: Acesta colectează toate numerele întregi dintr-o listă numită listă de întregi și nu putem uni două liste întregi în Python folosind funcția join(). Prin urmare, folosim a Hartă() funcție care convertește o listă de numere întregi într-un șir. După aceea, folosim o funcție join() pentru a uni rezultatele funcției map() cu delimitatorii corespunzători.

Sintaxă:

 map(str, list_name) 

În sintaxa de mai sus, o funcție map() are doi parametri, list_name și str. Unde list_name este numele listei de numere întregi și str reprezintă șirul. O funcție map() convertește list_name în șirul (str).

verificarea nulă în java

Program pentru a utiliza o funcție map() și funcția join() în listă

Să creăm un program pentru a converti lista de numere întregi dată într-un șir folosind funcția map() și apoi funcția join() pentru a se alătura listei.

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) 

Ieșire

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

Program pentru a uni două liste în Python folosind funcția for loop și append().

Un adăuga Funcția () este utilizată pentru a adăuga sau a uni secvențial fiecare element al unei liste iterabile la sfârșitul altei liste folosind bucla for. Să creăm un program simplu pentru a adăuga elemente dintr-o listă la sfârșitul altei liste folosind funcția 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) 

Ieșire

int un șir java
 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] 

Program pentru alăturarea mai multor liste folosind metoda itertools.chain().

Să creăm un program simplu în Python pentru a concatena mai multe liste folosind lanţ () prin importul iertools pachet.

Nou.py

 # 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)) 

Ieșire

 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] 

Program pentru a uni două liste folosind operatorul +

Să luăm în considerare un exemplu pentru a uni două liste în Python folosind operatorul (+) plus.

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)) 

Ieșire

 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'] 

Program pentru a uni două liste folosind operatorul de multiplicare (*)

Luați în considerare un exemplu pentru a uni două liste de în Python folosind operatorul *.

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)) 

Ieșire

 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] 

Program pentru a uni două liste în Python folosind metoda extend().

Să scriem un program simplu pentru a uni două liste folosind metoda extend() în 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)) 

Ieșire

 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']