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 程式設計師經常執行的過程,因此程式設計師必須瞭解不同的方法。瞭解所有這些不同的方法將有助於程式設計師提高效率,並且能夠知道何時使用哪種方法。以上文章描述了許多重複和擴充套件列表中資料的不同方法。

更新於: 2023年8月1日

65 次檢視

啟動你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.