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 程序员经常执行的过程,因此程序员必须了解不同的方法。了解所有这些不同的方法将有助于程序员提高效率,并且能够知道何时使用哪种方法。以上文章描述了许多重复和扩展列表中数据的不同方法。
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP