Python Learning Note four: list, shopping cart program examples

Source: Internet
Author: User
Tags shallow copy

List

Slice

Brackets, comma-delimited, multiple elements can be removed at once, starting position includes, end position not included (Gu Tou regardless of tail)

If you take the last one and don't know the length of the list, you can use a negative number (-1 is the last one, and so on)

If you take the last few, remember to count the values from left to right, Gu Tou regardless of the tail, so if the last two should be [-2:]

If you start from 0, you can omit it.

Append element

A_list.append (value)

Insert element to any position

A_list.insert (Index,value)

modifying elements

A_list[index]=value

Delete Element

A_list.remove (value)

Del a_list[index]20

A_list.pop ()--delete last

A_list.pop (Index)

Find element

A_list.index (value)

Statistical elements

A_list.count (value)

Invert element

A_list.reverse ()

Sort elements

A_list.sort ()

Special symbols, numbers, lowercase, uppercase (ASCII)

Extended List

A_list.extend (A_LIST2)

Delete List

Del a_list

Copy List

A_list.copy ()

a_list2=a_list[:]

a_list3= List (a_list)

Change the value of one list, the other unchanged

But a dimension that changes one value, and the one-dimensional list becomes a two-dimensional list, the two list changes at the same time

This is a shallow copy, cloning only the first layer list

The second list is a reference to the first list

Based on the shallow copy, modify the copy out of the list, you can implement the concept of similar inheritance

Deep copy

Import Copy

Copy.copy (a_list): shallow copy, same as A_list.copy ()

Copy.deepcopy (a_list): Deep copy

Don't use it. Large amount of data memory consumption

Two-dimensional list

Each element in a one-dimensional list can be a multidimensional list, but one-dimensional element encoding cannot be set to a two-dimensional element

Error: ' str ' object does not support item assignment

String is an object that cannot be modified

List loops

For I in A_list:

Print (i)

Jump Value (step slice)

a_list[0:-1:2],2 as step size

a_list[:], print from head to tail, weird!

Meta-group

Read-only list, cannot be modified, only queries, parentheses

A_list = ("Alex", "Jack", "Eric")

Method: Count, Index

Shopping Cart Program Example

Use nested lists to store product names and prices, parentheses cannot be modified, brackets can be modified

Flow chart:

Output list index and content:

For Index,item in Enumerate (product_list):

Print (Index,item)

Get the length of the list: Len (a_list)

Change output color: \033[31;1m%s\033[0m,31 font red, 32 font green, 41 background red

Exit program, exit ("message")

Python Learning Note four: list, shopping cart program examples

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.