logo

Python-tilmeldingsliste

I dette emne vil vi diskutere, hvordan vi kan forbinde to eller flere lister med forskellige funktioner i Python. Inden vi gennemgår begreberne, lad os tage en kort introduktion til Python-listen. EN Python liste er samlingen af ​​flere elementer, der er grupperet i samme navn. Det kan gemme forskellige datatyper (heltal, streng, flydende osv.) elementer inden for en firkantet parentes [], som er adskilt af et (,) komma.

Python-tilmeldingsliste

Program til at udskrive Python-listen

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) 

Produktion

 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] 

Når vi slår to eller flere lister sammen i en Python program, giver det en samlet lister. Og denne proces kaldes sammensætning eller sammenføjning af lister.

Lad os diskutere de forskellige måder at slutte sig til to eller flere lister i Python:

  • Deltag i lister i Python ved hjælp af join()-funktionen og skilletegn
  • Deltag i en liste i Python ved hjælp af join()-funktionen uden skilletegn
  • Forbind to heltalsliste i Python ved hjælp af map()-funktionen
  • Forbind to lister i Python ved hjælp af for loop og append() funktion
  • Slut dig til flere lister i Python ved hjælp af itertools.chain()-metoden
  • Slut dig til to lister i Python ved hjælp af (+) plus operator
  • Slut dig til to lister i Python ved hjælp af (*) multiplikation eller asterisk-operator
  • Slut dig til to lister i Python ved hjælp af extend()-funktionen

Deltag i lister i Python ved hjælp af join()-funktionen

EN tilslutte() funktion bruges til at forbinde en itererbar liste til en anden liste, adskilt af specificerede afgrænsningstegn såsom komma, symboler, en bindestreg osv.

Syntaks

 str_name.join( iterable) 

str_navn: Det er navnet på afgrænsningstegnet, der adskiller en itererbar liste.

iterable: Det er listen, der indeholder et sæt af elementer og forbinder med en afgrænsning.

Returneringsværdi: Det returnerer en sammenkædet liste, der er adskilt af specificerede skilletegn.

Bemærk: Hvis den itererbare liste indeholder ikke-strengværdier eller elementer, udløser den en TypeError-undtagelse.

Program til at forbinde to lister ved hjælp af join()-funktionen og skilletegn

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) 

Produktion

 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 til at deltage i en liste uden at bruge afgrænsning

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) 

Produktion

 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 

Sammenføj to heltalsliste ved hjælp af map()-funktionen

Heltalsliste: Den samler alle heltal i en liste kaldet heltalslisten, og vi kan ikke forbinde to heltalslister i Python ved hjælp af join()-funktionen. Derfor bruger vi en kort() funktion, der konverterer en heltalsliste til en streng. Derefter bruger vi en join()-funktion til at forbinde map()-funktionsresultater med passende skilletegn.

Syntaks:

 map(str, list_name) 

I ovenstående syntaks har en map() funktion to parametre, listenavn og str. Hvor listenavn er navnet på heltallisten og str repræsenterer strengen. En map()-funktion konverterer listenavnet til strengen (str).

java cast int til streng

Program til at bruge en map()-funktion og join()-funktion i listen

Lad os oprette et program til at konvertere den givne heltalliste til en streng ved hjælp af map()-funktionen og derefter join()-funktionen for at slutte sig til listen.

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

Produktion

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

Program til at forbinde to lister i Python ved hjælp af for loop og append() funktion

An Tilføj ()-funktionen bruges til sekventielt at tilføje eller forbinde hvert element i en iterabel liste i slutningen af ​​en anden liste ved hjælp af for loop. Lad os oprette et simpelt program til at tilføje elementer af en liste i slutningen af ​​en anden liste ved hjælp af append()-funktionen.

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) 

Produktion

 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 til at forbinde flere lister ved hjælp af itertools.chain()-metoden

Lad os oprette et simpelt program i Python til at sammenkæde flere lister ved hjælp af kæde () metode ved at importere itertools pakke.

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

Produktion

 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 til at slutte sig til to lister ved hjælp af + operator

Lad os overveje et eksempel for at forbinde to lister i Python ved hjælp af (+) plus-operatoren.

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

Produktion

c
 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 til at slutte sig til to-lister ved hjælp af (*) multipliceringsoperator

Overvej et eksempel for at forbinde to lister i Python ved hjælp af * operator.

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

Produktion

 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 til at forbinde to lister i Python ved hjælp af extend()-metoden

Lad os skrive et simpelt program til at forbinde to lister ved hjælp af extend()-metoden i 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)) 

Produktion

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