Data Structureis a collection of data elements that are organized in a way that can be numbers, characters, and even other data structures. Python's most basic data structure is sequence (sequence): 6 built-in sequences:lists, tuples, strings, Unicode strings, buffer objects, and Xrange objects. Comparison: List of the differences between the tuples in Erlang and Python:
1. General Operation:The Python list is more like an array in C, but more flexible than that, and the list of Erlang is much different. The 1.1 Python list index starts at 0 and can be negative. can use
>>>num=[0,1,2,3,4]>>>num[1]1>>>num[-1]4
Only a majority of the operations of Erlang are performed using the functions inside the lists, and the index starts at 1 and cannot be negative
>num=[0,1,2,3,4]. >lists:nth (num,2).
1.2 Python can be fragmented: Use an index to access a single element, and use shards to access elements within a range:
>>>tag='<a href= "http://www.google.com" >google Web site</a>'> >> tag[9:30'http://www.google.com'>>> tag[32:-4] ' Google Web site '
The Shard operation needs to provide two indexes as the boundary, the first indexed element what is contained within the Shard, and the second index element is not included in the Shard. The last few elements you want to take are even more concise:
>>> tag[-20:-4]'>google Web site'
1.2.1 can also set the step size: (The default size is 1)
>>>1, 2, 3, 4, 5, 6, 7, 8, 9, ten]>>> num[0:11:22, 4, 6, 8, 10]
1.2.2 method to get all elements in the list: 3 elements are not filled
>>>1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Erlang does not have a direct function to Shard (get a sub-list): There is no concept of step size. 1.3 Python can be added in sequence: 1.3.1 uses + directly, which does not change the value of the original list.
>>> num+[1,2,31, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3]
1.3.2 can use the expand function, which does not return the most recent list, only modifies the value of the original list.
>>> Num.extend ([5])>>>1, 2, 3, 4, 6, 7, 8, 9, 1, ten, 2, 3,]
Erlang similar operation: using + + or LISTS:APPEND/2
> [1,2,3]++[4,5,6].[ 1,2,3,4,5,6]> Lists:append ([1,2,3],[4,5,6]). [ 1,2,3,4,5,6]
1.4 Python can use *n to generate more than one new sequence, and in the new sequence, the original sequence is repeated n times
>>> [1]*4[1, 1, 1, 1]
An empty list can be expressed using [],
None is a built-in value for Python, representing null values, meaning that no elements are placed inside, and if you want to initialize a 10-length list, you can:
>>> [None]*10[None, none, none, none, none, none, none, none, none, none]
Erlang * is just a normal math operator
> lists:duplicate (4,1). [1,1,1,1]
The Erlang empty list is also represented by [], but there are no variables similar to none present (because it does not need to) 1.5 Python to check whether a value is in a sequence, you can use the In operator in to check if a condition is true, FALSE returns False if True is true, This is called a Boolean operator.
>>>1, 2, 3, 4, 5, 6, 7, 8, 9, ten, 1, 2, 3] in numtrue in numfalse< /c5> in num False"aaa"in"aaatest " True
You can see that the list and string in Python are not of the same type, but you can convert the string to a list by using the list function
>>> list ( test " " [ T , E , s , T " " >>> list ( " test " ) == test "
The string in Erlang is also a list, and there is no Boolean operator, and True,false essence is just a normal atom, you can use the lists function to determine the true and false
> Lists:member (21,[21,2,3,41,1]). True> Lists:member (22,[21,2,3,41,1]). False
1.6 Python's len,max,min corresponds to Erlang's length,max,min, which is very useful
2. List operations:Because Elrang variables are immutable, most common operations are encapsulated in the lists module. Python variables are variable, so there are some basic built-in Operations: 2.1 change list, Element assignment:
>>> num[1, 2, 3]>>> num[2]=100>>> num[1, 2, 100]
You cannot assign a value to an element that does not exist in a location:
>>> num[100]traceback (most recent ):"<stdin>" in <module>indexerror:list index out of range
2.2 Deleting elements:
>>> num[1, 2,[]del num[2]>>> num[1, 2] del num[10]traceback (most recent ):"<stdin>" in <module>indexerror:list Assignment index out of range
2.3 Shard Assignment: It's a very powerful feature.
>>> Name=list ("Test")>>>name['T','e','s','T']>>> name[2:]=['C','h']>>>name['T','e','C','h']>>> name[2:]=['C','h','e','R']>>>name['T','e','C','h','e','R']
If the index is filled with the same value, it becomes inserted:
>>> name[1:1]=['T','e','s','T']>>>name['T','T','e','s','T','e','C','h','e','R']
can also become deleted OH:
>>> name[1:4]=[]>>>name['T','T','e','C','h','e','R']>>> name[1:4]=[]>>>name['T','h','e','R']
List method: Python is called in the following way:object. Method (Parameter)A few common basic methods in the list are:
Append |
Append a new object to the end of the list |
Count |
Count the number of occurrences of an element in a list |
Extend |
Multiple values of another sequence can be appended at the end of the list at once |
Index |
Find the index position of the first occurrence of a value from the list |
Insert |
Used to insert an object into the list |
Pop |
Removes an element from the list (the default is the last) and returns the value of the element: it is the only list method that can modify the list and return the element value (none else) |
Remove |
To remove the first occurrence of a value in a list |
Reverse |
To store elements in the list in reverse |
Sort |
Sort, you can customize the sort function |
In contrast, Python's tuples and Erlang tuples are more like, but slightly different: the only difference between a Python tuple and a list is that the tuple is not modifiable. Basic syntax:
>>>(1, 2, 3)
Building a tuple of an element
>>> (1,) (1,)>>> (1)1
As with the list function, you can use the tuple function to convert a list to a tuple:
>>> tuple ([+]) (1, 2, 3)
How to feel the oppressive power of a strong high-level language for Erlang in the finishing process ...
The little girl realized that her eyebrows can move freely, and the music blossoms!
[Python01] python list, tuple vs Erlang difference Summary