* 运算符如何在 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,然后我们用重复运算符 (*) 将其乘以三次,并将其保存在另一个列表(输入列表 2)中。当我们更改第一个列表的值时,我们可以看到第二个列表元素发生了变化,而第二个列表(输入列表 2)没有改变。这意味着序列/列表中的项目/元素被多次引用,而不是复制。
使用 * 运算符解包函数
当以原始格式(没有任何逗号和括号)打印数据时,此方法非常方便。许多程序员试图通过聚合函数来删除逗号和括号,因此这个简单的前缀星号可以解决您在解包时遇到的问题。
算法(步骤)
以下是执行所需任务需要遵循的算法/步骤 -
创建一个变量来存储输入列表并为其赋予一些随机值。
要打印用空格分隔的列表元素(不使用括号 []),首先我们将 str 和 list 作为参数传递给 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 作为重复值时,我们会得到一个空列表。如果有任何负值,它就无法将它们相乘,因此返回一个空列表。
结论
本文涵盖了列表中涉及 * 重复运算符的所有情况。我们还讨论了它在各种情况下的表现。我们学习了如何使用 * 运算符打印由空格分隔的列表元素。这在各种情况下都很有用,例如编程竞赛,可以节省时间,而不必编写大量函数。
相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

