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

发表回复

登录后才能评论