Python 中 * 運算子在列表上的作用是什麼?
在 Python 中,列表是一個有序序列,可以包含多種物件型別,例如整數、字元或浮點數。在其他程式語言中,列表相當於陣列。
重複運算子 (*)
序列資料型別(可變和不可變)都支援重複運算子 *。* 重複運算子建立該物件的多個副本並將它們連線在一起。當與整數一起使用時,* 執行乘法運算,但當與 列表、元組 或 字串 一起使用時,它執行重複操作。
在本文中,我們將向您展示 * 運算子在 Python 列表上的作用。以下是瞭解 * 運算子如何在 Python 列表上執行的不同示例:
列表項的重複運算子 (*)
* 運算子作為引用時的工作方式
使用 * 運算子解包函式。
重複值為 0 時
列表項的重複運算子 (*)
Python 列表也包含 * 運算子,允許您建立一個新列表,其中元素重複指定次數。
下面的程式使用 * 運算子將列表重複指定的次數:
# input list inputList = [5, 6, 7] # Repeating the input list 2 times using the * operator print(inputList * 2)
輸出
執行上述程式後,將生成以下輸出:
[5, 6, 7, 5, 6, 7]
在這裡,我們取了一個隨機值的列表,並用 * 運算子將其乘以兩次,以便輸出包含重複兩次的給定列表。
* 運算子作為引用時的工作方式
序列/列表中的項不會被複制,而是被多次引用。
inputList_1=[[3]] inputList_2= inputList_1*3
示例
列表 inputList_2 的元素對應於列表 inputList_1 中的相同元素。因此,更改列表 inputList_1 中的任何元素都將更改列表 inputList_2 中的元素。
# Input list inputList_1=[4] # Multiplying the whole input list_1 three times using the Repetition Operator inputList_2= inputList_1*3 print('The Input list 1 without modification : ',inputList_1) print('The Input list 2 without modification : ',inputList_2) # Modifying the first element value of the input list with 20 inputList_1[0]= 20 #printing the output print('The Input list 1 after modification : ',inputList_1) print('The Input list 2 after modification : ',inputList_2)
輸出
The Input list 1 without modification : [4] The Input list 2 without modification : [4, 4, 4] The Input list 1 after modification : [20] The Input list 2 after modification : [4, 4, 4]
第一個列表只有一個元素 4,然後我們用重複運算子 (*) 將其乘以三倍並將其儲存在另一個列表 (input list 2) 中。當我們更改第一個列表的值時,我們可以看到第二個列表的元素會發生更改,而無需更改第二個列表 (input list 2)。這意味著序列/列表中的專案/元素被多次引用而不是被複制。
使用 * 運算子解包函式
此方法在以原始格式(沒有任何逗號和括號)列印資料時非常方便。許多程式設計師試圖透過收斂函式來去除逗號和括號,因此這個簡單的星號字首可以解決您在解包它們時遇到的問題。
演算法(步驟)
以下是執行所需任務的演算法/步驟:
建立一個變數來儲存輸入列表併為其賦予一些隨機值。
要列印用空格分隔的列表元素而沒有括號 [],我們首先透過將 str 和列表作為引數傳遞給 map() 函式來將列表轉換為字串。它將列表的每個元素轉換為字串型別並返回生成的專案列表。join() 函式 (join() 用於連線用字串分隔符分隔的序列元素) 用於將結果列表轉換為字串。
我們可以使用星號運算子 (*) 來列印用空格分隔的列表,而不是使用以前的方法。
示例
# input list inputList = ['TutorialsPoint', 'Python', 'Codes', 'hello', 5, 'everyone', 10, 5.3] # Converting list elements to string using map() # Applying join() function to convert list to string # Without using the asterisk (*) operator print('Without Using * Operator :') print(' '.join(map(str,inputList))) # Using the asterisk (*) operator print('Using * operator : ') print (*inputList)
輸出
Without Using * Operator : TutorialsPoint Python Codes hello 5 everyone 10 5.3 Using * operator : TutorialsPoint Python Codes hello 5 everyone 10 5.3
兩種方法的輸出都相同。
重複值為 0 時
當提供小於或等於 0 的值時,將返回相同型別的空序列。
示例 1
當輸入乘以 0 時,以下程式返回一個空列表:
# input list inputList = [5, 6, 7] # returning an empty list when repetition Value is given 0 print(inputList * 0)
輸出
執行上述程式後,將生成以下輸出
[]
我們在這裡使用 0 作為重複值,因此我們得到一個空列表,因為某些東西乘以 0 等於 0(空)。
示例 2
當輸入乘以任何小於 0 的數字時,以下程式返回一個空列表。
# input list inputList = [5, 6, 7] # returning an empty list when repetition Value is given -4 print(inputList * -4)
輸出
執行上述程式後,將生成以下輸出
[]
因為 * 運算子只接受正值,所以當我們傳遞 -4 作為重複值時,我們會得到一個空列表。如果存在任何負值,則無法將它們相乘,因此返回一個空列表。
結論
本文涵蓋了列表上 * 重複運算子的每種情況。我們還討論了它在各種情況下將如何執行。我們學習瞭如何使用 * 運算子列印用空格分隔的列表元素。這在各種情況下都很有用,例如程式設計競賽,可以節省時間,而不是編寫許多函式。