The data in the tuple cannot be changed.
Access another tuple from one tuple
>>> a = ("First", "second", "third")
>>> B = (A, "B ' s second element")
>>> B[0]
(' First ', ' second ', ' third ')
>>> B[0][1]
' Second '
The data in the list can be changed or appended, as follows:
>>> breakfast = ["Coffee", "Tee", "Coca Cola"]
>>>breakfast.append ("Juice") #只追加一个element
>>>breakfast.extend (["Milk", "sprit"]) #追加多个element
Dictionary
There are two ways to create a dictionary:
>>>dic_list={} #创建一个空的字典
>>>dic_list["Key1"]= "value1" #向字典中赋值
>>>dic_list["Key2"]= "value2"
Or:
>>>dic_list={"Key1": "Value1", "Key2": "Value2"}
Gets the keys in the dictionary:
>>>keys=dic_list.keys ()
>>>print (List (Keys))
[' Key1 ', ' Key2 ']
Gets the value in the dictionary:
>>>values=dic_list.values ()
>>>Print (list Values)
[' value1 ', ' value2 ']
In the dictionary, the values may be the same, but the keys cannot be the same, and the keys are unique. What happens when a key is duplicated?
>>>dic_list={"key1": "Value1", "key1": "value2", "Key2": "Value3"}
>>>Print (Dic_list.keys ())
[' Key1 ', ' Key3 ']
>>>Print (dic_list["Key1"])
value2
As you can see, if you have a key with the same name, the latter one will replace the value of the previous key.
Python can treat a string as if it were a list of individual characters.
For example:
>>>some_str= "This is a test!"
>>>Print ("The first and last char is%s and%s"% (Some_str[0],some_str[-1])
The first and last char are T and!
Other common properties of a sequence
1. Referencing the last element
>>> tuple_list= ("A", "B", "C")
>>> Tuple_list[-1]
C
>>> list=["A", "B", "C"]
>>> List[-1]
C
2. Range of sequences
>>> slice_me= ("Please", "slice", "Me", "!")
>>> Slice_me[0:3]
(' Please ', ' slice ', ' me ')
>>> slice_list=["Please", "slice", "This", "list"]
>>> Slice_list[0:2]
[' Please ', ' slice ']
3. List by additional sequence growth
You cannot use the Append method to append a sequence to the end of a sequence, and the result is to add a divided list to the list. As follows:
>>> Tuple_str = ("This", "is", "a", "tuple")
>>> list = []
>>> List.append (TUPLE_STR)
>>> List
[(' This ', ' is ', ' a ', ' tuple ')]
If you need to create a new list based on the contents of a tuple, you can use the Extend method to insert elements from the tuple into the list
>>> Tuple_str = ("This", "is", "a", "tuple")
>>> list = []
>>> List.extend (TUPLE_STR)
>>> List
[' This ', ' is ', ' a ', ' tuple ']
4. Working with collections (Removing duplicate elements )
>>> company = ["Baidu", "Ali", "Sina", "Baidu", "Tencent", "Sina")
>>> Set (company)
{' Sina ', ' Tencent ', ' Baidu ', ' Ali '}
5. Elements in the popup list
You can use Pop to remove an element from the list after processing one of the elements in the list, and when the reference is deleted, its original position in the list is populated with subsequent elements.
>>> list= ["Baidu", "Goole", "Cisco", "Ali", "Tencent", "Sina"]
>>> List.pop (1)
' Goole '
>>> List.pop (1)
' Cisco '
>>>list
[' Baidu ', ' Ali ', ' Tencent ', ' Sina ']
The foreign has been killed ~.~
Tuples, lists, dictionaries in Python