Python Learning Note Six: Lists, tuples, dictionaries

Source: Internet
Author: User
Tags delete key

Learning other languages with little attention to arrays is how the structure is going to be used directly. Python defines three data structures for lists, tuples, and dictionaries, and is the basis for our use of Python, so it is necessary to understand what they mean, and I think this is about the idea of everything that Python does. Python's data structure is more flexible.

1. list [lists]

The sequence is the most basic data structure in Python. Each element in the sequence is assigned a number-its position, or index, the first index is 0, the second index is 1, and so on.

Python has 6 built-in types of sequences, but the most common are lists and tuples.

Sequences can be performed by operations including indexing, slicing, adding, multiplying, and checking members.

In addition, Python has built-in methods for determining the length of a sequence and determining the maximum and minimum elements.

A list is the most commonly used Python data type and can appear as a comma-separated value within a square bracket.

Data items for a list do not need to have the same type

access ([1:3], not including tail small), update, delete del list1[2]

List operations:

Python Expressions Results Description
Len ([1, 2, 3]) 3 Length
[1, 2, 3] + [4, 5, 6] [1, 2, 3, 4, 5, 6] Combination
[' hi! '] * 4 [' hi! ', ' hi! ', ' hi! ', ' hi! '] Repeat
3 in [1, 2, 3] True Whether the element exists in the list
For x in [1, 2, 3]: print x, 1 2 3 Iteration
L = [' spam ', ' spam ', ' spam! ']
Python Expressions Results Description
L[2] ' spam! ' Read the third element in the list
L[-2] ' Spam ' Reads the second-to-last element in a list
L[1:] [' Spam ', ' spam! '] To intercept a list starting with the second element

List functions

1 CMP (List1, List2)
Compare two elements of a list
2 Len (list)
Number of list elements
3 Max (list)
Returns the maximum value of a list element
4 MIN (list)
Returns the minimum value of a list element
5 List (seq)
Convert a tuple to a list

1 List.append (obj)
Add a new object at the end of the list
2 List.count (obj)
Count the number of occurrences of an element in a list
3 List.extend (seq)
Append multiple values from another sequence at the end of the list (extend the original list with a new list)
4 List.index (obj)
Find the index position of the first occurrence of a value from the list
5 List.insert (index, obj)
Inserting an object into a list
6 List.pop (Obj=list[-1])
Removes an element from the list (the last element by default), and returns the value of the element
7 List.remove (obj)
To remove the first occurrence of a value in a list
8 List.reverse ()
Reverse List of elements
9 List.sort ([func])
Sort the original list

2. Tuples (TUP)

A python tuple is similar to a list, except that the elements of a tuple cannot be modified .

When you include only one element in a tuple, you need to add a comma after the element, Tup1 = (50,);

Tup1+tup2

Del Tup1

Python Expressions Results Description
Len ((1, 2, 3)) 3 Count the number of elements
(1, 2, 3) + (4, 5, 6) (1, 2, 3, 4, 5, 6) Connection
[' hi! '] * 4 (' hi! ', ' hi! ', ' hi! ', ' hi! ') Copy
3 in (1, 2, 3) True Whether the element exists
For x in (1, 2, 3): Print x, 1 2 3 Iteration
L = (' spam ', ' spam ', ' spam! ')
Python Expressions Results Description
L[2] ' spam! ' Reading a third element
L[-2] ' Spam ' Read the second last element of the inverse
L[1:] [' Spam ', ' spam! '] Intercepting elements

Tuple functions

Serial Number Method and Description
1 CMP (Tuple1, Tuple2)
Compares two tuples of elements.
2 Len (tuple)
Counts the number of tuple elements.
3 Max (tuple)
Returns the element's maximum value in a tuple.
4 MIN (tuple)
Returns the element minimum value in a tuple.
5 Tuple (SEQ)
Converts a list to a tuple.

3. Dictionary {}

A dictionary consists of a pair of keys and corresponding values . A dictionary is also called an associative array or hash table, and the keys must be unique, but the values do not. The value can take any data type, but it must be immutable , such as a string, number, or tuple.

Dict = {' Alice ': ' 2341 ', ' Beth ': ' 9102 ', ' Cecil ': ' 3258 '}
Print dict[' Name '] #访问
Add new content to a dictionary by adding new key/value pairs, modifying or deleting existing key/value pairs
dict[' Beth ']=100 #update
dist[' age ']=7 #add
Del dict[' Name ']; # Delete key is ' Name ' entry dict.clear ();     # Empty dictionary all Entries del dict;        # Delete Dictionary
1) do not allow the same key to appear two times
2) The key must be immutable, so it can be used as a number, a string or a tuple, so the list does not work,

Dictionary functions

1 CMP (Dict1, DICT2)
Compares two dictionary elements.
2 Len (dict)
Calculates the number of dictionary elements, that is, the total number of keys.
3 STR (DICT)
The output dictionary is a printable string representation.
4 Type (variable)
Returns the type of the variable entered and returns the dictionary type if the variable is a dictionary.

1 radiansdict.clear ()
Delete all elements in the dictionary
2 radiansdict.copy ()
Returns a shallow copy of a dictionary
3 radiansdict.fromkeys ()
Create a new dictionary , the key for the dictionary in sequence seq, Val is the initial value corresponding to all keys for the dictionary
4 radiansdict.get (Key, Default=none)
Returns the value of the specified key, if the value Default value is not returned in the dictionary
5 radiansdict.has_key (key)
returns False if the key is true in the dictionary dict
6 radiansdict.items ()
Returns an array of traversed (key, value) tuples in a list
7 Radia Nsdict.keys ()
Returns a dictionary of all keys in a list
8 radiansdict.setdefault (Key, Default=none)
and get ( ) similar, but if the key does not already exist in the dictionary, the key is added and the value is set to default
9 radiansdict.update (dict2)
Update the key/value pairs of the dictionary Dict2 To Dict
radiansdict.values ()
Returns all values in the dictionary in a list

Python Learning Note six: Lists, tuples, dictionaries

Related Article

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.