Comparison with the old Ziko python list and str

Source: Internet
Author: User
Tags python list
same Point

Data that belong to 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" way to get, and to get more than one element at a time, you can use the slice. The offset starts at 0 and ends with the total number of elements minus 1.

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:

>>> first = "Hello,world" >>> welcome_str ' welcome you ' >>> first+ "," +welcome_str  #用 + Connect str ' hello,world,welcome you ' >>> welcome_str       #原来的str没有受到影响, that is, the above + number is connected from the Renaissance into a string ' Welcome you ' > >> first ' hello,world ' >>> language = [' python ']>>> git_list[' qiwsir ', ' GitHub ', ' io ']>> > language + git_list   #用 + Connect list, get a new list[' Python ', ' qiwsir ', ' GitHub ', ' io ']>>> git_list[' Qiwsir ', ' GitHub ', ' io ']>>> language[' python ']>>> len (welcome_str)  #得到字符数11 >>> 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 does that make sense?

First look at these operations on the list, which is characterized by modifying the list in the same place:

>>> 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 be an error, such as:

>>> welcome_str ' welcome you ' >>> welcome_str[1] = ' E ' Traceback (most recent call last): File "
 
  
    c2/> ", line 1, in 
  
   
    
   TypeError: ' str ' object does not the support item assignment>>> del Welcome_str[1]trac Eback (most recent): File "
   
    
     
    ", line 1, in 
    
     
      
     TypeError: ' str ' object doesn ' t support ite M deletion>>> welcome_str.append ("E") Traceback (most recent call last): File "
     
      
       
      ", line 1, IN
       
       
         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 you ' > >> welcome_str             #对原来的没有任何影响 ' welcome you '

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

Multidimensional List

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

>>> 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 previous element.

List and STR conversions

Str.split ()

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

Before you look at the example, crossing do the following in interactive mode:

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

Split (...) S.split ([Sep [, Maxsplit]]), List of Stringsreturn a list of the words in the string S, using Sep as the delimiter str Ing. If Maxsplit is given, at most maxsplit splits be done. If Sep is no specified or is None, any whitespace string is a separator and empty strings be removed from the result.

Whether or not you read the above paragraph, you can look at the example. Still want crossing to understand the above content.

>>> line = "hello.i am qiwsir. Welcome ">>> line.split (". ")   #以英文的句点为分隔符, get list[' Hello ', ' I am qiwsir ', ' Welcome you ', ']>>> line.split ('. ', 1)  #这个1, which is expressed in the above: If Maxsplit is given, at most Maxsplit splits was done. [' Hello ', ' I am qiwsir. Welcome you. "    >>> name = "Albert Ainstain"  #也有可能用空格来做为分隔符 >>> name.split ("") [' Albert ', ' Ainstain '] "[Sep]". Join (list)

Join can be said to be the inverse of split, for example:

>>> name[' Albert ', ' ainstain ']>>> '. Join (name)    #将list中的元素连接起来, but without a connector, Represents a "Albertainstain" >>> ".". Join (name)   #以英文的句点做为连接分隔符 ' Albert. Ainstain ' >>> '. Join (name)   #以空格做为连接的分隔符 ' Albert ainstain '
  • 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.