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 程序中將兩個或多個列表連接在一起時,它會給出一個連接的列表。這個過程叫做列表的組合或連接。

讓我們討論在 Python 中連接兩個或多個列表的不同方法:

  • Python 中使用 Join()函數和分隔符連接列表
  • 使用不帶分隔符的 Join()函數在 Python 中連接列表
  • 使用 map()函數連接 Python 中的兩個整數列表
  • 在 Python 中使用 for循環和 append()函數連接兩個列表
  • 使用 itertools.chain()方法在 Python 中連接多個列表
  • 在 Python 中使用(+)加號運算符連接兩個列表
  • 在 Python 中使用(*)乘法或星號運算符連接兩個列表
  • 使用 extend()函數連接 Python 中的兩個列表

Python 中使用 Join()函數連接列表

join() 函數用於將一個可列表連接到另一個列表,由指定的分隔符分隔,如逗號、符號、連字符等。

語法


str_name.join( iterable)

str_name: 是分隔可列表的分隔符的名稱。

可迭代的:它是包含一組元素並用分隔符連接的列表。

返回值:返回由指定分隔符分隔的串聯列表。

注意:如果可迭代列表包含任何非字符串值或項,它將引發類型錯誤異常。

使用 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

不使用分隔符連接列表的程序

程序


# 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()函數連接兩個整數列表。因此,我們使用 map() 函數將整數列錶轉換為字符串。之後,我們使用 join()函數將 map()函數結果與適當的分隔符連接起來。

語法:


map(str, list_name)

在上面的語法中,map()函數有兩個參數,list_name 和 str。其中 list_name 是整數列表的名稱,str 表示字符串。map()函數將 list_name 轉換為字符串。

程序在列表中使用 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循環和 append()函數在 Python 中連接兩個列表的程序

一個追加()函數用於使用 for循環在另一個列表的末尾順序添加或連接一個可迭代列表的每個元素。讓我們創建一個簡單的程序,使用 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 中連接兩個列表的示例。

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]

使用 extend()方法在 Python 中連接兩個列表的程序

讓我們編寫一個簡單的程序,使用 Python 中的 extend()方法連接兩個列表。

程序


# 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( "\n Adding two lists of integers in Python using the extend() function: ", str(List1))
print( "\n 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']

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/237759.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-12 12:07
下一篇 2024-12-12 12:07

相關推薦

  • Python計算陽曆日期對應周幾

    本文介紹如何通過Python計算任意陽曆日期對應周幾。 一、獲取日期 獲取日期可以通過Python內置的模塊datetime實現,示例代碼如下: from datetime imp…

    編程 2025-04-29
  • Python列表中負數的個數

    Python列表是一個有序的集合,可以存儲多個不同類型的元素。而負數是指小於0的整數。在Python列表中,我們想要找到負數的個數,可以通過以下幾個方面進行實現。 一、使用循環遍歷…

    編程 2025-04-29
  • Python中引入上一級目錄中函數

    Python中經常需要調用其他文件夾中的模塊或函數,其中一個常見的操作是引入上一級目錄中的函數。在此,我們將從多個角度詳細解釋如何在Python中引入上一級目錄的函數。 一、加入環…

    編程 2025-04-29
  • Python周杰倫代碼用法介紹

    本文將從多個方面對Python周杰倫代碼進行詳細的闡述。 一、代碼介紹 from urllib.request import urlopen from bs4 import Bea…

    編程 2025-04-29
  • 如何查看Anaconda中Python路徑

    對Anaconda中Python路徑即conda環境的查看進行詳細的闡述。 一、使用命令行查看 1、在Windows系統中,可以使用命令提示符(cmd)或者Anaconda Pro…

    編程 2025-04-29
  • Python字典去重複工具

    使用Python語言編寫字典去重複工具,可幫助用戶快速去重複。 一、字典去重複工具的需求 在使用Python編寫程序時,我們經常需要處理數據文件,其中包含了大量的重複數據。為了方便…

    編程 2025-04-29
  • Python程序需要編譯才能執行

    Python 被廣泛應用於數據分析、人工智能、科學計算等領域,它的靈活性和簡單易學的性質使得越來越多的人喜歡使用 Python 進行編程。然而,在 Python 中程序執行的方式不…

    編程 2025-04-29
  • python強行終止程序快捷鍵

    本文將從多個方面對python強行終止程序快捷鍵進行詳細闡述,並提供相應代碼示例。 一、Ctrl+C快捷鍵 Ctrl+C快捷鍵是在終端中經常用來強行終止運行的程序。當你在終端中運行…

    編程 2025-04-29
  • 蝴蝶優化算法Python版

    蝴蝶優化算法是一種基於仿生學的優化算法,模仿自然界中的蝴蝶進行搜索。它可以應用於多個領域的優化問題,包括數學優化、工程問題、機器學習等。本文將從多個方面對蝴蝶優化算法Python版…

    編程 2025-04-29
  • Python清華鏡像下載

    Python清華鏡像是一個高質量的Python開發資源鏡像站,提供了Python及其相關的開發工具、框架和文檔的下載服務。本文將從以下幾個方面對Python清華鏡像下載進行詳細的闡…

    編程 2025-04-29

發表回復

登錄後才能評論