python中的元组_Python中的元组

python中的元组

Tuples:

元组:

Tuples are one of the built-in types of sequences out of six and just like lists. There is another article published on how to use Lists in Python. We want to store the values for the rest of our program then we can use Tuples.  Now for an example, if we want to store the name of some category of any Test plan when comes to compliance like "Audit, SOX, Risk Management or ORM" for continuing some program. 

元组是六个序列中的内置序列之一,就像列表一样。 还有另一篇有关如何使用的文章   Python中的列表 我们要存储程序其余部分的值,然后可以使用元组。 现在举一个例子,如果我们想要存储任何测试计划的某个类别的名称,例如“审计,SOX,风险管理或ORM”,以便继续执行某些程序。

First, we need to declare a tuple and give it a name, the initial values will be used within parentheses "()". All the values will be separated by a comma

首先,我们需要声明一个元组并为其命名,初始值将在括号“()”中使用 。 所有值将以逗号分隔。

my_category = ('Audit', 'SOX', 'Risk Management', 'ORM')
print my_category
print my_category[0]
print my_category[2] 

The result from the above code will be as below :

以上代码的结果如下:

  ('Audit', 'SOX', 'Risk Management', 'ORM')
   Audit 
   Risk Management 

Sometimes we need to remove or delete the entire Tuple, so to delete the entire tuple we can use the command "del", below is the example on how to use del or delete then entire tuple. 

有时我们需要删除或删除整个元组,因此要删除整个元组,我们可以使用命令“ del” ,下面是有关如何使用del或删除然后删除整个元组的示例。

my_category = ('Audit', 'SOX', 'Risk Management', 'ORM')
print my_category
#print my_category[0]
#print my_category[2]
del my_category
print my_category 
  ('Audit', 'SOX', 'Risk Management', 'ORM')
Traceback (most recent call last):  File "python", line 7, in <module> NameError: name 'my_category' is not defined
    

We can see from the above result we are getting that the tuple which was created is not defined because after creating it we immediately deleted the entire tuple. 

从上面的结果我们可以看到,未定义创建的元组,因为创建完后我们立即删除了整个元组。

So if we want to find or verify if a particular item is present in a tuple then we can use "in". In the example below, we are trying to see if SOX is present in the sample tuple created or not. 

因此,如果我们要查找或验证元组中是否存在特定项目,则可以使用“ in” 。 在下面的示例中,我们试图查看在创建的示例元组中是否存在SOX

     my_category = ('Audit', 'SOX', 'Risk Management', 'ORM')
      print my_category
   ('Audit', 'SOX', 'Risk Management', 'ORM')
   'SOX' in my_category
   => True    

We can see that the result is "True" if the item is present in a tuple. When an item is not present then the result will be false. 

我们可以看到,如果元组中存在该项目,则结果为“ True” 。 如果没有商品,则结果为假。

  'Compliance' in my_category
   => False    

Arithmetic Operators in Tuples. 

元组中的算术运算符。

Using concatenation in Tuple.

在Tuple中使用串联。

To concatenate the tuples we need to use plus operator "+".  The below example shows how to use concatenation in tuples. 

连接元组,我们需要使用加号运算符“ +”。 下面的示例显示如何在元组中使用串联。

my_category = ('Audit', 'SOX', 'Risk Management', 'ORM')
print my_category
print (my_category+ ('Compliance','Others')) 
 ('Audit', 'SOX', 'Risk Management', 'ORM') ('Audit', 'SOX', 'Risk Management', 'ORM', 'Compliance', 'Others')
    

Using Multiplication Operator in Tuple. 

在元组中使用乘法运算符。

If in case we need to duplicate the values in a tuple then we can use multiplication operator to achieve. Below example will show how to use multiplication operator in a tuple. 

如果需要在元组中复制值,则可以使用乘法运算符来实现。 下面的示例将显示如何在元组中使用乘法运算符。

my_category = ('Audit', 'SOX', 'Risk Management', 'ORM')
print my_category*2
print ("# Below is the output of the actual tuple")
print my_category 
 ('Audit', 'SOX', 'Risk Management', 'ORM', 'Audit', 'SOX', 'Risk Management', 'ORM') 

# Below is the output of the actual tuple 
 ('Audit', 'SOX', 'Risk Management', 'ORM')
    

We can see the using addition or multiplication do not modify the actual tuple. 

我们可以看到使用加法或乘法不会修改实际的元组。

Assignments are not possible in tuples how we usually do it on a list in Python.  The example below will show how we use the assignment in List and the error on applying the same rule on Tuple.  

在元组中不可能进行分配 ,通常情况下我们是如何在Python的列表中进行分配的 。 下面的示例将显示我们如何使用List中的赋值以及在Tuple上应用相同规则的错误。

list =[1,2,3,4]
print list
print list[0]
list[0]=9
print list 
[1, 2, 3, 4] 
1
[9, 2, 3, 4]
    

In the above example, we can see we have changed the value from 1 to 9 by assigning it. Now let us try to assign zero back to the list, for doing this I am converting the list into a tuple and will try to assign. 

在上面的示例中,我们可以看到通过将其值从1更改为9。 现在让我们尝试将零分配回列表,为此,我将列表转换为元组并将尝试分配。

list =[1,2,3,4]
print ('Below is the output of a list ')
print list
print list[0]
list[0]=9
print ('Below is the output of a list after assigning a value 9')
print list
print ('Below is the output of a tuple by converting a list into a tuple ')
print tuple(list)
print ('Get the first value from the tuple')
print tuple(list)[0]
print ('Assign back 0 in place of 9 in a Tuple')
tuple(list)[0]=0
print tuple(list) 

The result from the above code. 

上面代码的结果

Below is the output of a list 
[1, 2, 3, 4]
1 
Below is the output of a list after assigning a value 9 
[9, 2, 3, 4] 
Below is the output of a tuple by converting a list into a tuple 
(9, 2, 3, 4) 
Get the first value from the tuple
9
Assign back 0 in place of 9 in a Tuple
Traceback (most recent call last):  File "python", line 13, in <module> TypeError: 'tuple' object does not support item assignment
    

 We can see the error saying 'tuple' object does not support item assignment

我们可以看到错误,指出“ 元组 ”对象没有   支持项目分配

Now we know how to use Tuple in Python programming. This is a basic explanation on how to use Tuple in Python programming language

现在我们知道了如何在Python编程中使用Tuple 。 这是有关如何在Python编程语言中使用Tuple的基本说明。

Thank you for reading my article, please feel free to leave me some feedback or to suggest any future topics.

感谢您阅读我的文章,请随时给我一些反馈或提出任何未来的话题。

I'll be looking forward to hearing from you – Swadhin Ray (Sloba)

我期待着您的回音-Swadhin Ray(Sloba)

For more information about me, please check out my Experts Exchange Profile page.

有关我的更多信息,请查看我的Experts Exchange个人资料页面。

翻译自: https://www.experts-exchange.com/articles/32871/Tuples-in-Python.html

python中的元组

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值