# 19.5.15 python list tuples dictionary collection

Data structure in Python
Reference: https://www.cnblogs.com/myworld7/p/8449614.html


- list list: Format: list = [ '1', '2']

https://www.runoob.com/python/python-lists.html

  1. Each element in the list change. list additions and deletions to change search

_> Means that you can modify and delete each element.

  1. The list is ordered, the location of each element are determined, you can use the index to access each element.
  2. Elements in the list can be any object in Python.

[1,2,3]+[4,5,6] = [1,2,3,4,5,6]

Adding a new element at the end of list.append

List.count a number of elements that appear in the list

list.insert (index, obj) Method

index - the index object obj to be inserted position.
obj - object to be inserted in the list

Reverse arranged in order:
Positive and negative sequence arrangementIt means any object may be a string element, integer tuple may be a list of objects and the like Python.

It means any object may be a string element, integer tuple may be a list of objects and the like Python.

list function

list(“Hello,world”)
[‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘,’ , ‘w’, ‘o’, ‘r’, ‘l’, ‘d’]

You can create a list by list sequence.


- tuple tupple format: tuple = ( '1', '2')
tuple can be understood as a fixed list, once the elements which will not modify the initialization (including deletion), only elements of the query.

example:

test=(‘a’,‘b’,[‘A’,‘B’])
print(test)
(‘a’, ‘b’, [‘A’, ‘B’])
test[2][0]=‘x’
test[2][1]=‘y’
test
(‘a’, ‘b’, [‘x’, ‘y’])

Here seemingly 3A elements changed, but under careful analysis, tuple third element is a list.

3.4 lines of code changed is the value in the list, tuples within the meaning of this element in the list has not changed, you need to pay attention to this point!

It can be converted to a tuple by tuple sequence, usage, and list the same!

- dictionary dict = { 'Lu': 'ccc'}

Dictionary Dictionary This concept is based on the prototype in real life, the life of the use of the name - the contents of the data constructs, Python use key (key) - the value (value) storage, which is in C ++ map.

Dict the salient features:

  1. Data dictionary must be present in the form of key-value pairs
  2. Key is not repeated, the value may be repeated
  3. If only the dictionary key note repeat the last value corresponding to the key

Dictionary key (key) is immutable, immutable objects can not be changed; the value (value) can be modified, it can be any object.
In dict is calculated according to the key value storage location, if different from the same key the results of each calculation, and that the internal dict completely confused.

dict deletions check changes

  1. It can be a "key-on" method, and update () method to add an element to the dictionary
  2. You can use keywords and delete del pop () method

Query takes a query such as indexing elements of the list, use the key as an index lookup value

If the element is not present will complain, before proceeding to find, you can determine whether the key exists in the following two ways:

① Membership operators -in operator

② get () method (returns a NULL value is not present, the return value can also be specified)

对值得修改可以采用直接覆盖原值的方法

dict中的元素是无序的,不可以采用分片。

Set set: Format: test = set ([1,2,3])

Each element of the set is unordered, do not overlap any object.
Can be set to judge membership data, may be set by the data structure elements lose repeated. Collection of set operations do, you can add and delete elements.

Removing elements need to use when creating the list as a set of input set, you can add () method add elements, remove () method

test=set([1,2,3])
test
{1, 2, 3}
test.add(3)
test
{1, 2, 3}
test.add(6)
test
{1, 2, 3, 6}
test.remove(3)
test
{1, 2, 6}

Set operation
set Python may be performed between the set of & pay, and | other operations

SET = S1 ([1,2])
s2 = SET ([2,3])
S1 and S1 & # s2 s2
{2}
S1 | s2 s2 # S1 ORed with
{1, 2, 3}


Summary:
List = [ '', ''] change check deletions; ordered
tuple = ( '', '' ) unchanged; ordered
dict = { '': '' } key-value; check deletions change; disordered
[(2)] analogy mathematical set s1 = set

Guess you like

Origin blog.csdn.net/weixin_44719417/article/details/90261450