logo

Python 조인 목록

이 주제에서는 Python의 서로 다른 기능을 사용하여 두 개 이상의 목록을 결합하는 방법에 대해 논의합니다. 개념을 살펴보기 전에 Python 목록에 대해 간략하게 소개하겠습니다. ㅏ 파이썬 목록 동일한 이름으로 그룹화된 여러 항목의 모음입니다. (,) 쉼표로 구분된 대괄호 [] 안에 다양한 데이터 유형(정수, 문자열, 부동 소수점 등) 항목을 저장할 수 있습니다.

Python 조인 목록

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) 

산출

 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] 

두 개 이상의 목록을 하나로 합칠 때 파이썬 프로그램에서는 결합된 목록을 제공합니다. 그리고 이 과정을 목록의 구성 또는 결합이라고 합니다.

Python에서 두 개 이상의 목록을 결합하는 다양한 방법에 대해 논의해 보겠습니다.

  • Join() 함수와 구분 기호를 사용하여 Python에서 목록 조인
  • 구분 기호 없이 Join() 함수를 사용하여 Python에서 목록에 합류
  • map() 함수를 사용하여 Python에서 두 개의 정수 목록을 결합합니다.
  • for 루프와 추가() 함수를 사용하여 Python에서 두 목록을 결합합니다.
  • itertools.chain() 메서드를 사용하여 Python에서 여러 목록에 가입하세요.
  • (+) 더하기 연산자를 사용하여 Python에서 두 목록을 결합합니다.
  • (*) 곱하기 또는 별표 연산자를 사용하여 Python에서 두 목록을 결합합니다.
  • Extension() 함수를 사용하여 Python에서 두 목록을 결합합니다.

Join() 함수를 사용하여 Python에서 목록 조인

가입하다() 함수는 쉼표, 기호, 하이픈 등과 같은 지정된 구분 기호로 구분된 반복 가능한 목록을 다른 목록에 결합하는 데 사용됩니다.

통사론

 str_name.join( iterable) 

문자열_이름: 반복 가능한 목록을 구분하는 구분 기호의 이름입니다.

반복 가능: 요소 집합을 포함하고 구분 기호로 결합된 목록입니다.

반환 값: 지정된 구분 기호로 구분된 연결된 목록을 반환합니다.

참고: 반복 가능한 목록에 문자열이 아닌 값이나 항목이 포함되어 있으면 TypeError 예외가 발생합니다.

Join() 함수와 구분 기호를 사용하여 두 목록을 결합하는 프로그램

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) 

산출

 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 

구분 기호를 사용하지 않고 목록에 참여하는 프로그램

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) 

산출

 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 

map() 함수를 사용하여 두 정수 목록 결합

정수 목록: 정수 목록이라는 목록의 모든 정수를 수집하며 Python에서는 Join() 함수를 사용하여 두 정수 목록을 결합할 수 없습니다. 그러므로 우리는 지도() 정수 목록을 문자열로 변환하는 함수입니다. 그런 다음, Join() 함수를 사용하여 map() 함수 결과를 적절한 구분 기호로 결합합니다.

통사론:

 map(str, list_name) 

위 구문에서 map() 함수에는 list_name과 str이라는 두 개의 매개변수가 있습니다. 여기서 list_name은 정수 목록의 이름이고 str은 문자열을 나타냅니다. map() 함수는 list_name을 문자열(str)로 변환합니다.

업캐스팅

리스트에서 map() 함수와 Join() 함수를 사용하는 프로그램

map() 함수와 Join() 함수를 사용하여 주어진 정수 목록을 문자열로 변환하는 프로그램을 만들어 목록을 결합해 보겠습니다.

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) 

산출

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

for 루프와 추가() 함수를 사용하여 Python에서 두 목록을 결합하는 프로그램

추가 () 함수는 for 루프를 사용하여 반복 가능한 목록의 각 요소를 다른 목록의 끝에 순차적으로 추가하거나 결합하는 데 사용됩니다. 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) 

산출

 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] 

itertools.chain() 메소드를 사용하여 여러 목록을 결합하는 프로그램

Python에서 다음을 사용하여 여러 목록을 연결하는 간단한 프로그램을 만들어 보겠습니다. 체인 () 메소드를 임포트하여 itertools 패키지.

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

산출

 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] 

+ 연산자를 사용하여 두 목록을 결합하는 프로그램

(+) 더하기 연산자를 사용하여 Python에서 두 목록을 결합하는 예를 고려해 보겠습니다.

Java에서 문자열을 정수로 변환하는 방법

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

산출

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

(*) 곱셈 연산자를 사용하여 두 목록을 결합하는 프로그램

* 연산자를 사용하여 Python에서 두 개의 목록을 결합하는 예를 고려하십시오.

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

산출

 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] 

Extension() 메서드를 사용하여 Python에서 두 목록을 결합하는 프로그램

Python에서 extend() 메서드를 사용하여 두 개의 목록을 결합하는 간단한 프로그램을 작성해 보겠습니다.

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

산출

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