One of the basics of Python self-learning: lists and tuples

Source: Internet
Author: User

I have been engaged in python for many years, have a lot of small partners added me after said: There is no relatively simple point of the tutorial, you write some of the major items we do not understand the small!! Today I will send a small whiteFoundation for one of the introductory studies, list and Ganso, welcome to subscribe to the public number: Python from the program Ape to the program ape, or Add, Mountain King: 49130.8659, code: Cauchy ... Code: Cauchy, I will share the crawler or Web from 0 to the project in a series of video materials, to ensure that you can self-taught.

List

first , the list belongs to the sequence, then the sequence type can be built as follows--

  1. List (ITER): Converts an Iterative object to a list.

  2. STR (obj): Converts the Obj object to a string, that is, the object is represented by a string.

  3. Tuple (ITER): Converts an iterative object to a tuple.

  4. Unicode (obj): Converts an object to a Unicode string.

  5. Basestring (): Abstract factory function that simply provides the parent class for STR and Unicode functions, so it cannot be instantiated or invoked.

  6. Enumerate (ITER): Accepts an iterative object as a parameter that returns a enumerate object that generates a tuple of index and item values for each element of ITER.

  7. Len (seq): Returns the length of the seq.

  8. Max (Iter,key=none), Max (Arg0,arg1...,key=none): Return to ITER or (Arg0,arg1 ...) Maximum value, if key is specified, the key must be a callback function that can be passed to the sort () method for comparison.

  9. Min (iter,key=none), Min (arg0,arg1...,key=none): Return to ITER or (Arg0,arg1 ...) Minimum value, if key is specified, the key must be a callback function that can be passed to the sort () method for comparison.

  10. Reversed (seq): Takes a sequence as a parameter, returning an iterator that is accessed in reverse order.

  11. Sorted (Iter,cmp=none,key=none,reverse=false): Accepts an iterative object as a parameter, returning an ordered list, with optional parameters like CMP, key and reverse and list.sort () built-in functions.

  12. SUM (seq,init=0): Returns the sum of the SEQ and optional parameter init, which has the same effect as reduce (operator.add,seq,init).

  13. Zip ([it0,it1 ...]) : Returns a list whose first element is It0, it1 ... The first element of these elements consists of a tuple, and the other elements in turn.

Second, the list is like a linear container, but much more than the C + + lis t extension

The elements in the list can be of the same type, or they can contain various types, such as a list nested in another table

The list can be sliced, and the slice operation is similar to a call to a function that returns a new list of values

Slice l1[x:y: z] is a semi-open interval (z is usually not written), such as L1[1:3] returns a list from the end of l1[1] to l1[2, and does not contain l1[3]

X does not write to start from scratch, y does not write until the end of the list, z is used to represent the step, the default is 1, can be considered in this interval each z element takes one (take the first), can be negative, indicating from the back to the front traversal

Lists can do addition, multiply, and strings can also be seen as a list of characters

In statement to determine whether an object is in a string/list/tuple

The not statement represents the negation of the following

Len can detect the number of elements in a string/list/meta-ancestor/dictionary

Max can return the largest element, Min returns the smallest element

Base down to this, will not give the code, know a train of thought, you can manually achieve the following actions:

  1. Assign value

  2. Delete

  3. Shard Assignment

  4. Add an element to the end of the list

  5. Merge List

  6. Returns the coordinates in the list

  7. Removes the element from the location and returns it

  8. Reverse the list

    Ganso

tuples are also part of a sequence, but tuples are not modifiable lists. Therefore the tuple does not have the above sequence common method to be available!

A tuple of an element is represented as (1,)

Tuples can be converted to lists, and vice versa.

The built-in tuple () function takes a list parameter and returns a tuple containing the same element, while the list () function takes a tuple parameter and returns a list.

From the effect, the tuple () freezes the list, and the list () melts the tuple.

Multiple assignments can be made using a list or tuple


One of the basics of Python self-learning: lists and tuples

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.