python序列化反序列化_Python中的序列

python序列化反序列化

Sequence is something that used to store data in it in very simple words. Let us just create a list first.

序列是用来以非常简单的语言在其中存储数据的东西。 让我们先创建一个列表。

To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed by square braces as like below:

首先要创建列表,我们需要给列表起一个名字,我将其命名为“ COURSE”,后跟等号,最后用方括号括起来,如下所示:

Python 2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> COURSE = []

Now we have to add all the elements within the square brackets. So now let us add the elements within it.

现在,我们必须在方括号内添加所有元素。 现在让我们在其中添加元素。

Python 2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> COURSE = ['PYTHON', 'PERL','ORACLE','MYSQL','BASH']

After keeping the entire element that we want to be added within the list hit “enter”.

保留要添加到列表中的整个元素后,单击“输入”。

Now we have stored our list of elements with in a variable i.e. COURSE but internally python will assign numbers to each element of the list which are been stored starting with zero.

现在我们将元素列表存储在一个变量中,即COURSE,但是在内部python将为列表中的每个元素分配数字,这些数字从零开始存储。

It assigns as like below order:

它分配如下命令:

  • PYTHON  as 0 “zero”

    PYTHON为0“零”
  • PERL  as 1 “one”

    PERL为1“一个”
  • ORACLE as 2 “two”

    ORACLE作为2个“两个”
  • MYSQL as 3 “three”

    MYSQL为3“三”
  • BASH as 4 “four”

    BASH为4“四”

Sequence plays an important role because we can access member or element of the list by indexing.

序列扮演重要角色,因为我们可以通过索引访问列表的成员或元素。

So if we now want to access only Oracle from my list i.e. “COURSE” then I can simply call it by using indexing as like below:

因此,如果我们现在只想从列表(即“ COURSE”)中访问Oracle,那么我可以通过使用索引来简单地调用它,如下所示:

Python 2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> COURSE = ['PYTHON', 'PERL','ORACLE','MYSQL','BASH']
>>> COURSE[2]
'ORACLE'

But the list COURSE is case sensitive.  For example if I want to access as below then I get list is undefined.

但是列表COURSE是区分大小写的。 例如,如果我想按以下方式访问,那么我得到的列表是未定义的。

>>> course[2]
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    course[2]
NameError: name 'course' is not defined

Now in python they made the indexing in reverse way too which starts with negative (–ve) from end to beginning.  So in reverse way if we want to access then it will be like as below:

现在,在python中,它们也以相反的方式进行了索引编制,从头到尾都以负(–ve)开头。 因此,如果要访问,则采用相反的方式,如下所示:

  • PYTHON  as -5 “negative five”

    PYTHON为-5“负五”
  • PERL  as -4 “negative four”

    PERL为-4“负四”
  • ORACLE as -3 “negative three”

    ORACLE为-3“负三”
  • MYSQL as -2 “negative two”

    MYSQL为-2“负二”
  • BASH as -1 “negative one”

    BASH为-1“负数”
>>> COURSE[-1]
'BASH'
>>> COURSE[-2]
'MYSQL'
>>> COURSE[-3]
'ORACLE'
>>> COURSE[-4]
'PERL'
>>> COURSE[-5]
'PYTHON'
>>> 

So list in python is indexed like two types i.e. forward starting from zero and other from backward starting with negative numbers. Stings in python can also be sequences  for example if I take a sting “sloba” then this will be indexed similarly what we have seen on index on our list “COURSE” which we have created. 

因此,python中的list的索引类似于两种类型,即从零开始的正向索引和从负数开始的向后反向索引。 python中的字符串也可以是序列,例如,如果我使用字符串“ sloba”,则其索引将类似于我们在已创建的列表“ COURSE”上的索引中看到的索引。

Example if we want to know what is the index 4 will be for string “sloba” :

例如,如果我们想知道字符串“ sloba”的索引4是什么:

>>> 'sloba' [4]
'a'

We see “a” because starting with zero the 4th index will be “a” for string “sloba”.

我们看到“ a”,因为从零开始,第四索引对于字符串“ sloba”将为“ a”。

Now let us see what we are going to set when we say negative 4 for the same string.

现在,让我们看看当对相同字符串说负4时将要设置的内容。

>>> 'sloba' [-4]
'l'

We are getting letter “L” because if we start counting backward starting from negative one then -4 indexed on “L”.

我们收到字母“ L”,因为如果我们从负数开始倒数,那么-4将在“ L”上索引。

For more information on sequence and list in Python visit https://www.python.org/doc/

有关Python中的序列和列表的更多信息,请访问https://www.python.org/doc /

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

python序列化反序列化

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值