Compared to the old Ziko python list and str _python

Source: Internet
Author: User
Tags stdin python list

Same point

Data that is part of the sequence type

The so-called sequence type of data, that is, each of its elements can be specified by a number, the jargon is called "offset" in the way, and to get more than one element at a time, you can use slices. The offset starts at 0, and the total element number minus 1 ends.

For example:

>>> Welcome_str = "welcome You"
>>> welcome_str[0]
' W '
>>> welcome_str[1]
' e '
>>> welcome_str[len (WELCOME_STR)-1]
' u '
>>> welcome_str[:4]
' WELC '
>>> a = "python"
>>> a*3
' Pythonpythonpython '

>>> git_list = ["Qiwsir "," GitHub "," io "]
>>> git_list[0]
' Qiwsir '
>>> git_list[len (git_list)-1]
' Io '
>>> git_list[0:2]
[' Qiwsir ', ' GitHub ']
>>> b = [' Qiwsir ']
>>> b*7
[' Qiwsir ', ' qiwsir ', ' qiwsir ', ' qiwsir ', ' qiwsir ', ' qiwsir ', ' Qiwsir ']

For this type of data, some of the following actions are similar:

>>> a = "Hello,world"
>>> welcome_str
' welcome you '
>>> first+ "," +welcome _str  #用 + number is connected
to str ' hello,world,welcome you '
>>> welcome_str       #原来的str没有受到影响, i.e. above + The number of connections from the freshman into a string
' Welcome you '
>>> ' Hello,world '

>>> language = [' Python ']
>>> git_list
[' Qiwsir ', ' GitHub ', ' io ']
>>> language + git_list   #用 + number connection list, get a new list
[' Python ', ' qiwsir ', ' GitHub ', ' io ']
>> > git_list
[' Qiwsir ', ' GitHub ', ' io ']
>>> language
[' python ']

>>> len ( WELCOME_STR)  #得到字符数
>>> len (git_list)    #得到元素数
3

Difference

The biggest difference between list and STR is that the list can be changed in the same place, and STR is immutable. How do you understand this?

First look at these operations on the list, which is characterized by modifications to the list in situ:

>>> git_list
[' Qiwsir ', ' GitHub ', ' io ']

>>> git_list.append ("python")
>>> Git_list
[' Qiwsir ', ' GitHub ', ' io ', ' python ']

>>> git_list[1]        
' GitHub '
>>> git_ List[1] = ' github.com '
>>> git_list
[' Qiwsir ', ' github.com ', ' io ', ' python ']

>>> git_ List.insert (1, "algorithm")
>>> git_list
[' Qiwsir ', ' Algorithm ', ' github.com ', ' io ', ' python ']

>>> git_list.pop ()
' python '

>>> del git_list[1]
>>> git_list
[' Qiwsir ', ' github.com ', ' io ']

These operations, if used on STR, will have an error, such as:

>>> welcome_str
' welcome '

>>> welcome_str[1] = ' E '
traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: ' str ' object does not support item assignment

>>> del welcome_str[1]
traceback (most recent CA LL last):
File "<stdin>", line 1, in <module>
typeerror: ' str ' object doesn ' t support item DELETION
   >>> welcome_str.append ("E")
Traceback (most recent call last):
File ' <stdin> ', line 1, in & lt;module>
attributeerror: ' str ' object has no attribute ' append '

If you want to modify a str, you have to.

>>> welcome_str '
welcome you '
>>> welcome_str[0] + "E" + welcome_str[2:] #从新生成一个str
' WElcome '
>>> welcome_str             #对原来的没有任何影响
' WElcome you '

In fact, in this practice, the equivalent of a new form of Str.

Multidimensional List

This should be regarded as the difference between the two, although a bit far-fetched. In Str, each element inside can only be a character, in the list, the element may be any type of data. See above is a number or character, in fact, can also be like this:

>>> matrix = [[1,2,3],[4,5,6],[7,8,9]]
>>> matrix = [[1,2,3],[4,5,6],[7,8,9]]
>>> MATRIX[0][1]
2
>>> mult = [[1,2,3],[' A ', ' B ', ' C '], ' d ', ' e ']
>>> mult
[[1, 2, 3], [' A ' , ' B ', ' C '], ' d ', ' e ']
>>> mult[1][1] '
B '
>>> mult[2]
' d '

The above shows the multidimensional list and how to access it. In multidimensional cases, the list inside is treated like a front element.

List and STR conversions

Str.split ()

This built-in function implements the conversion of STR to list. where str= "" is a separator.

Before looking at the example, please reader the following in interactive mode:

>>>help (Str.split)
A complete description of this built-in function is obtained. Special emphasis: This is a very good way to learn

Split (...)
S.split ([Sep [, Maxsplit]])-> list of strings return
a list of the words in the string S, using Sep as the Delimite R string. If Maxsplit is given, at most maxsplit splits do. If Sep is not specified or are None, any whitespace string is a separator and empty strings are removed.

Whether you read the above paragraph or not, you can see the example. Still hope reader can understand the above content.

>>> line = "hello.i am qiwsir." Welcome you. " 

>>> line.split (".")   #以英文的句点为分隔符, get list
[' Hello ', ' I am qiwsir ', ' Welcome you ', ']

>>> line.split (".", 1)  #这个1, is to express the above: If Maxsplit is given and at most maxsplit splits do.
[' Hello ', ' I am qiwsir. Welcome you. ']    

>>> name = "Albert Ainstain"  #也有可能用空格来做为分隔符
>>> name.split ("")
[' Albert ', ' Ainstain ']
[Sep]. Join (list)

Join can be said to be a split inverse operation, for example:

>>> name
[' Albert ', ' Ainstain ']
>>> '. Join (name)    #将list中的元素连接起来, but there is no connector, indicating that one is adjacent to the
' Albertainstain '
>>> '. Join (name)   #以英文的句点做为连接分隔符
' albert.ainstain '
>>> ' ". Join (name)   #以空格做为连接的分隔符
' Albert Ainstain '

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.