Compared with list and str of Python, pythonstr
Similarities
All data belongs to the sequence type
The so-called sequence data means that each element of a sequence can be obtained by specifying a number and the line is called an offset. To get multiple elements at a time, you can use slices. The offset starts from 0 and the total number of elements decreases by 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 such data, the following operations are similar:
>>> First = "hello, world" >>> welcome_str 'Welcome you' >>> first + "," + welcome_str # connect str 'hello, world, welcome you '>>> welcome_str # The original str is not affected, that is, after the plus sign is connected, a new string 'Welcome you' is generated. >>> first 'hello, world '>>>> language = ['python'] >>> git_list ['qiwsir', 'github ', 'IO ']> language + git_list # connect to the list with the + sign to obtain a new list ['python', 'qiwsir', 'github ', 'io'] >>> git_list ['qiwsir ', 'github', 'io'] >>> language ['python'] >>> len (welcome_str) # Get the number of characters 11 >>> len (git_list) # Get the number of elements 3
Differences
The biggest difference between list and str is that list can be changed from the original position, while str cannot be changed from the original position. How can this be understood?
First, let's take a look at these operations on the list. The feature is that the list is modified in the original 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']
If the preceding operations are used on str, an error is reported, for example:
>>> welcome_str'Welcome you'>>> 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 call 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 <module>AttributeError: 'str' object has no attribute 'append'
If you want to modify a str, you have.
>>> Welcome_str 'Welcome you' >>> welcome_str [0] + "E" + welcome_str [2:] # generate a new str 'Welcome you' >>> welcome_str # It has no effect on the original 'Welcome you'
In fact, in this way, it is equivalent to a new str.
Multi-dimensional list
This is also the difference between the two, although a little far-fetched. In str, each element can only be a character. In list, an element can be any type of data. We can see numbers or characters as follows:
>>> 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 multi-dimensional list and access method are shown above. In multi-dimensional scenarios, the list is treated like a previous element.
List and str Conversion
Str. split ()
This built-in function converts str to list. Str = "" is the separator.
Before looking at the example, you can see that the officer performs the following operations in interactive mode:
>>> Help (str. split)
Complete descriptions of this built-in function are provided. Special emphasis: This is a very good learning method.
split(...)S.split([sep [,maxsplit]]) -> list of stringsReturn a list of the words in the string S, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done. If sep is not specified or is None, any whitespace string is a separator and empty strings are removed from the result.
Whether or not you have understood the above paragraph, you can refer to the example. I still hope the viewer can understand the above content.
>>> Line = "Hello. I am qiwsir. welcome you. ">>> line. split (". ") # use the English period as the separator to obtain the list ['hello', 'I am qiwsir', 'Welcome you',''] >>> line. split (". ", 1) # This 1 represents: If maxsplit is given, at most maxsplit splits are done. ['hello', 'I am qiwsir. welcome you. '] >>> name = "Albert Ainstain" # space may also be used as the separator >>> name. split ("") ['Albert ', 'ainstain'] "[sep]". join (list)
Join can be called the inverse operation of split. For example:
>>> Name ['Albert ', 'ainstain'] >>> "". join (name) # concatenate the elements in the list, but there is no connector, indicating that the elements in the list are one by one adjacent to 'Albert tainstain '>>> ". ". join (name) # Use English periods as the separator 'Albert. ainstain '> "". join (name) # use space as the separator for connection 'Albert Ainstain'
In python33, how does one convert a str to a list?
Does it mean no space?
Then
List (my_str.replace ("", "").
List (my_str)
Python list comparison Size Problem
I think a. sort (cmp = lambda x, y: cmp (x [3], y [3]) is in place sorting.
The following is the content of the online document:
The sort () and reverse () methods modify the list in place for economy
Space when sorting or reversing a large list. To remind you that they operate
Side effect, they don't return the sorted or reversed list.
The sort () method takes optional arguments for controlling
Comparisons. cmp specifies a custom comparison function of two arguments (list
Items) which shoshould return a negative, zero or positive number depending on
Whether the first argument is considered smaller than, equal to, or larger
The second argument: cmp = lambdax, y: cmp (x. lower (), y. lower ().
Default value is None. key specifies a function of one argument that is used to extract
Comparison key from each list element: key = str. lower. The default value is None. reverse is a boolean value. If set to True, then the list
Elements are sorted as if each comparison were reversed. In general, the key and reverse conversion processes are
Much faster than specifying an equivalent cmp function. This is because
Cmp is called multiple times for each list element while key
And reverse touch each element only once. Use functools. cmp_to_key () to convert an old-style
Cmp function to a key function. Changed in version 2.3:
Support for None as an equivalent to omitting cmp was
Added. Changed in version 2.4:
Support for key and reverse was added.
Starting with Python 2.3, the sort () method is
Guaranteed to be stable. A sort is stable I ...... the remaining full text>