在这个主题中,我们将讨论如何使用Python的不同函数将两个或多个列表拼接在一起。在深入了解概念之前,让我们简要介绍一下Python列表。Python列表是多个项目的集合,它们以相同的名称分组在一起。它可以在方括号[]内存储不同的数据类型(整数、字符串、浮点数等),这些项目之间用逗号(,)分隔。

233.png

打印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中拼接两个或多个列表的不同方法:

  • 使用join()函数和分隔符在Python中拼接列表
  • 使用join()函数在Python中拼接列表而不使用分隔符
  • 使用map()函数在Python中拼接两个整数列表
  • 使用for循环和append()函数在Python中拼接两个列表
  • 使用itertools.chain()方法在Python中拼接多个列表
  • 使用(+)运算符在Python中拼接两个列表
  • 使用(*)运算符在Python中拼接两个列表
  • 使用extend()函数在Python中拼接两个列表

使用join()函数在Python中拼接列表

join()函数用于将一个可迭代的列表与另一个列表拼接在一起,以指定的分隔符分隔,如逗号、符号、连字符等。

语法

str_name.join( iterable) 

str_name: 这是用于分隔可迭代列表的分隔符的名称。

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',  'i', 'k', 'u' ]  
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','i', 'k', 'u']
 Display the List without using delimiters j  a  v  a  t  i  k  u

使用map()函数拼接两个整数列表

整数列表: 它将所有整数收集到一个名为整数列表的列表中,我们不能使用join()函数在Python中拼接两个整数列表。因此,我们使用map()函数将整数列表转换为字符串。然后,我们使用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循环和append()函数在Python中拼接两个列表

append()函数用于使用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中拼接多个列表

让我们创建一个简单的Python程序,通过导入itertools包,使用chain()方法拼接多个列表。

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中拼接两个列表

让我们考虑一个示例,在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中拼接两个列表

考虑一个示例,在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中拼接两个列表

让我们编写一个简单的程序,使用extend()函数在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( "\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']

标签: Tkinter教程, Tkinter安装, Tkinter库, Tkinter入门, Tkinter学习, Tkinter入门教程, Tkinter, Tkinter进阶, Tkinter指南, Tkinter学习指南, Tkinter进阶教程, Tkinter编程