python列表连接列表_用Python连接列表的6种方法

python列表连接列表

In this tutorial, we will unveil different methods to concatenate lists in Python. Python Lists serve the purpose of storing homogeneous elements and perform manipulations on the same.

在本教程中,我们将揭示在Python中串联列表的不同方法。 Python列表用于存储同类元素并对其进行操作。

In general, Concatenation is the process of joining the elements of a particular data-structure in an end-to-end manner.

通常,串联是指以端到端的方式连接特定数据结构的元素的过程。

The following are the 6 ways to concatenate lists in Python.

以下是在Python中串联列表的6种方法。

  • concatenation (+) operator

    串联(+)运算符
  • Naive Method

    天真的方法
  • List Comprehension

    清单理解
  • extend() method

    extend()方法
  • ‘*’ operator

    '*'运算符
  • itertools.chain() method

    itertools.chain()方法


1.用于列表串联的串联运算符(+) (1. Concatenation operator (+) for List Concatenation)

The '+' operator can be used to concatenate two lists. It appends one list at the end of the other list and results in a new list as output.

'+' operator可用于连接两个列表。 它在另一个列表的末尾追加一个列表,并产生一个新列表作为输出。

Example:

例:


list1 = [10, 11, 12, 13, 14] 
list2 = [20, 30, 42] 


res = list1 + list2 


print ("Concatenated list:\n" + str(res)) 

Output:

输出:


Concatenated list:
[10, 11, 12, 13, 14, 20, 30, 42]


2.列表连接的朴素方法 (2. Naive Method for List Concatenation)

In the Naive method, a for loop is used to traverse the second list. After this, the elements from the second list get appended to the first list. The first list results out to be the concatenation of the first and the second list.

在朴素方法中,使用for循环遍历第二个列表。 之后,第二个列表中的元素将附加到第一个列表中。 第一个列表结果是第一个列表和第二个列表的串联。

Example:

例:


list1 = [10, 11, 12, 13, 14] 
list2 = [20, 30, 42] 

print("List1 before Concatenation:\n" + str(list1))
for x in list2 : 
    list1.append(x) 


print ("Concatenated list i.e. list1 after concatenation:\n" + str(list1)) 

Output:

输出:


List1 before Concatenation:
[10, 11, 12, 13, 14]
Concatenated list i.e. list1 after concatenation:
[10, 11, 12, 13, 14, 20, 30, 42]


3.列表理解以连接列表 (3. List Comprehension to concatenate lists)

Python List Comprehension is an alternative method to concatenate two lists in Python. List Comprehension is basically the process of building/generating a list of elements based on an existing list.

Python List Comprehension是在Python中串联两个列表的替代方法。 列表理解基本上是基于现有列表构建/生成元素列表的过程。

It uses for loop to process and traverses the list in an element-wise fashion. The below inline for-loop is equivalent to a nested for loop.

它使用for循环以元素方式处理和遍历列表。 下面的内联for循环等效于嵌套的for循环。

Example:

例:


list1 = [10, 11, 12, 13, 14] 
list2 = [20, 30, 42] 

res = [j for i in [list1, list2] for j in i] 

print ("Concatenated list:\n"+ str(res)) 

Output:

输出:


Concatenated list:
 [10, 11, 12, 13, 14, 20, 30, 42]


4,用于列表串联的Pythonextend()方法 (4.Python extend() method for List Concatenation)

Python’s extend() method can be used to concatenate two lists in Python. The extend() function does iterate over the passed parameter and adds the item to the list thus, extending the list in a linear fashion.

Python的extend()方法可用于连接Python中的两个列表。 extend()函数确实对传递的参数进行迭代,并将该项添加到列表中,从而以线性方式扩展列表。

Syntax:

句法:


list.extend(iterable)

Example:

例:


list1 = [10, 11, 12, 13, 14] 
list2 = [20, 30, 42] 
print("list1 before concatenation:\n" + str(list1))
list1.extend(list2) 
print ("Concatenated list i.e ,ist1 after concatenation:\n"+ str(list1)) 

All the elements of the list2 get appended to list1 and thus the list1 gets updated and results as output.

list2的所有元素都附加到list1上,因此list1得到更新,结果作为输出。

Output:

输出:


list1 before concatenation:
[10, 11, 12, 13, 14]
Concatenated list i.e ,ist1 after concatenation:
[10, 11, 12, 13, 14, 20, 30, 42]


5.用于列表串联的Python'*'运算符 (5. Python ‘*’ operator for List Concatenation)

Python’s '*' operator can be used to easily concatenate two lists in Python.

Python的'*' operator可用于轻松连接Python中的两个列表。

The ‘*’ operator in Python basically unpacks the collection of items at the index arguments.

Python中的'*'运算符基本上将索引参数处的项目集合解压缩

For example: Consider a list my_list = [1, 2, 3, 4].

例如:考虑一个列表my_list = [1、2、3、4]。

The statement *my_list would replace the list with its elements at the index positions. Thus, it unpacks the items of the lists.

语句* my_list会将列表替换为其索引位置的元素 。 因此,它解压缩了列表中的项目。

Example:

例:


list1 = [10, 11, 12, 13, 14] 
list2 = [20, 30, 42] 

res = [*list1, *list2] 
  
print ("Concatenated list:\n " + str(res)) 

In the above snippet of code, the statement res = [*list1, *list2] replaces the list1 and list2 with the items in the given order i.e. elements of list1 after elements of list2. This performs concatenation and results in the below output.

在上面的代码片段中,语句res = [* list1,* list2]用给定顺序的项(即list1的元素在list2的元素之后替换list1和list2。 这将执行串联并产生以下输出。

Output:

输出:


Concatenated list:
 [10, 11, 12, 13, 14, 20, 30, 42]


6. Python itertools.chain()方法来串联列表 (6. Python itertools.chain() method to concatenate lists)

Python itertools modules’ itertools.chain() function can also be used to concatenate lists in Python.

Python itertools模块的itertools.chain()函数还可用于连接Python中的列表。

The itertools.chain() function accepts different iterables such as lists, string, tuples, etc as parameters and gives a sequence of them as output.

itertools.chain()函数接受列表,字符串,元组等不同的可迭代对象作为参数,并将它们的序列作为输出。

It results out to be a linear sequence. The data type of the elements doesn’t affect the functioning of the chain() method.

结果是线性序列。 元素的数据类型不会影响chain()方法的功能。

For example: The statement itertools.chain([1, 2], [‘John’, ‘Bunny’]) would produce the following output: 1 2 John Bunny

例如:语句itertools.chain([1,2],['John','Bunny'])将产生以下输出: 1 2 John Bunny

Example:

例:


import itertools
list1 = [10, 11, 12, 13, 14] 
list2 = [20, 30, 42] 

res = list(itertools.chain(list1, list2)) 
   
  
print ("Concatenated list:\n " + str(res)) 

Output:

输出:


Concatenated list:
 [10, 11, 12, 13, 14, 20, 30, 42]


结论 (Conclusion)

Thus, in this article, we have understood and implemented different ways of Concatenating lists in Python.

因此,在本文中,我们已经了解并实现了Python中串联列表的不同方式。

翻译自: https://www.journaldev.com/35927/concatenate-lists-python

python列表连接列表

你可能感兴趣的:(列表,字符串,数据结构,python,java)