Python 重复和扩展列表
Python是一种非常常用的编程语言,用于许多不同的目的,如Web开发、数据科学、机器学习以及执行许多不同的自动化过程。程序员经常遵循的一个常见过程是对列表数据进行更改或向已有列表添加更多数据。在本文中,我们将学习如何重复和扩展列表。
重复元素
在这种方法中,通过重复列表中存在的先前数据来扩展列表中的数据。重复列表数据的不同方法如下:
列表推导式
我们将使用列表推导式,它将检查列表中的每个元素,并重复我们想要的次数。该方法的语法如下:
def repeat_data_in_list(data, value): # In the function repeat_data_in_list we will provide different parameters.
# Data will represent the list from which data is to be taken and no. will represent the number of times the data is to be repeated
new_list = [item for item in data for _ in range(value)] # Each element of the list is checked to repeat the data
return new_list
示例
上述方法的示例如下:
def repeat_data_in_list(data, value): # In the function repeat_data_in_list we will provide different parameters.
# Data will represent the list from which data is to be taken and no. will represent the number of times the data is to be repeated
new_list = [item for item in data for _ in range(value)] # Each element of the list is checked to repeat the data
return new_list
names = ['John', 'Sam', 'Daniel'] #The input to be taken is defined
new_name_list = repeat_data_in_list(names, 3) #We provide the argument to define the input and the number of time data is to be repeated
print(new_name_list) # The new repeated data is to be printed
输出
上面示例的输出将如下所示:
['John', 'John', 'John', 'Sam', 'Sam', 'Sam', 'Daniel', 'Daniel', 'Daniel']
运算符的使用
这个运算符通常用于表示乘法符号。在这种方法中,我们将把数据乘以它需要重复的次数。以上方法的语法如下所示:
def repeat_data_in_list(data, value): # The data of the list and the number of times it is to be repeated is given as input to the function repeat_data_in_list
new_list = data * value # It gives the information about the number of times the data is to be repeated by multiplication
return new_list
示例
上述代码的示例如下:
def repeat_data_in_list(data, value): # The data of the list and the number of times it is to be repeated is given as input to the function repeat_data_in_list
new_list = data * value # It gives the information about the number of times the data is to be repeated by multiplication
return new_list
Countries = ["India", "Germany", "Israel", "Canada"] # The input is given
new_name_list = repeat_data_in_list(Countries, 3) #The argument is provided of how many times to repeat the data
print(new_name_list) #Print the new repeated list
输出
以上代码的输出结果如下:
['India', 'Germany', 'Israel', 'Canada', 'India', 'Germany', 'Israel', 'Canada', 'India', 'Germany', 'Israel', 'Canada']
使用Itertools
Itertools用于创建正确循环数据的迭代器。我们将使用itertools的repeat函数来重复数据。该方法的语法如下:
import itertools # Do not forget to import itertools or else error might occur
def repeat_data_in_list(data, value): #The data in the list and the number of time it is to be repeated is given as the input
new_list = list(itertools.repeat(data, value)) # We will then use the itertools repeat to repeat the data required number of times
return new_list
示例
上述方法的示例如下:
import itertools # Do not forget to import itertools or else error might occur
def repeat_data_in_list(data, value): #The data in the list and the number of time it is to be repeated is given as the input
new_list = list(itertools.repeat(data, value)) # We will then use the itertools repeat to repeat the data required number of times
return new_list
Countries = ["India", "Germany", "Israel", "Canada"] # The input is given
new_name_list = repeat_data_in_list(Countries, 3) # The argument is given to take countries as the input and the number of times it is to be repeated
print(new_name_list)
输出
以上示例的输出将如下所示:
['India', 'Germany', 'Israel', 'Canada', 'India', 'Germany', 'Israel', 'Canada', 'India', 'Germany', 'Israel', 'Canada']
乘法元素
在这种方法中,将列表中的数据重复。根据需要,将列表中的内容进行乘法运算。乘法元素的不同方法有:
列表推导式
这种方法中,我们将检查列表中的每个元素,并使用定义的因子进行乘法运算。以上方法的语法如下:
def multiply_data_in_list(data, values): # The input of data of the list and the factor(no.) is given into the function
new_list = [item * values for item in data] # The list comprehension will check each element in the list and multiply it with the factor provided
return new_list
示例
该方法的示例如下:
def multiply_data_in_list(data, values): # The input of data of the list and the factor(no.) is given into the function
new_list = [item * values for item in data] # The list comprehension will check each element in the list and multiply it with the factor provided
return new_list
Names = ['John', 'Sam', 'Daniel', 'Jack'] # The input of name is given into the program
new_names = multiply_data_in_list(Names, 3) #The argument is provided to multiply it three times
print(new_names) # The output after printing will be visible
以上示例的输出将如下所示:
['JohnJohnJohn', 'SamSamSam', 'DanielDanielDaniel', 'JackJackJack']
Numpy Python的NumPy库是一个强大的数值计算工具。它提供了许多数组操作,包括对数组进行逐元素相乘的能力。这种方法主要适用于数值。上述方法的语法如下:
import numpy as np #Do not forget to import numpy or else error might occur
def multiply_data_in_list(data, values): # The input of the data in the list and number of times to be repeated is as follows
new_list = np.array(data) * values # The data is converted into array and then the array is multiplied the number of times
return new_list.tolist() # Once the multiplication is done, array is again converted into list
示例
以上方法的示例如下:
import numpy as np #Do not forget to import numpy or else error might occur
def multiply_data_in_list(data, values): # The input of the data in the list and number of times to be repeated is as follows
new_list = np.array(data) * values # The data is converted into array and then the array is multiplied the number of times
return new_list.tolist() # Once the multiplication is done, array is again converted into list
#The example of the above method is as follows: -
Names = [1, 2, 3, 4, 5, 6] # The input of name is given into the program
new_names = multiply_data_in_list(Names, 3) #The argument is provided to multiply the array three times
print(new_names) # The output after printing will be visible
输出
以上示例的输出如下:
[3, 6, 9, 12, 15, 18]
结论
编辑和添加列表中的数据是Python程序员经常进行的一种常见过程,因此程序员有关于不同方法的知识非常必要。了解所有这些不同的方法将帮助程序员变得高效,并且他/她将能够知道何时使用哪种方法。上述文章描述了许多不同的方法来重复和扩增列表中的数据。