Python full stack development-list, Ganso, Dictionary, collection-7.13

Source: Internet
Author: User

List, Ganso, Dictionary, Collection list:

Definition: is a collection of data, can hold any type of data, can be added to the collection of deletions and other operations.

Create an empty list l[], non-empty list: l[' A ', ' B ', ' C ',. 4] Its index number is 0,1,2,3. Creating nested lists: l[' A ', ' B ', [' C ', ' d ']

List of Queries l[2]: By index number, l[-1] from the right index, -1 is the first one on the right. L.index[' A '], returns the index number of a, l.count[' a ']: View the number of ' a '.

List of slices: set column N2 = [' Shanshan ', ' longting ', ' Alex ', 1,2,3,4,4,5,6,7,7,8,8,9,9,0,0,2,3,4,4]. Use N2[0:3] to remove [' Shanshan ', ' longting ', ' Alex '], Gu Tou regardless of the tail of the way, but also can use negative, but also must be from left to right cut, such as n2[-1:-5], slices can also specify the step size, such as n2[0:5:2] step is 2 , the result is [' Shanshan ', ' Alex ', 2].

List additions and deletions: l.append (' A '), add A,l.insert (3, ' B ') at the end of the list: Insert B where the index number is 3. L.pop (): Delete the last element, L.remove (' Shanshan '), delete the first Shanshan indexed in the list, and if there are multiple Shanshan, you can use the method of global deletion: Del l[2], you can also delete more than one element at a time: Del L [2:4].

Loop for list: For I in L:

Print (i)

Sort of list: L.sort (), which is sorted by ASCLL table, can also be reversed in reverse, l.reverse ().

List extension: L.extend ([2,1,1]), you can add some elements to the tail of the list in L.

Emptying of the list: L.clear ()

Copy of the list: L.copy (), which is completely independent after copying.

Exercises:

Existing products

[[' Iphone8 ', 6888],[' MacPro ', 14800],[' Xiaomi 6 ', 2499],[' Coffee ', 31],[' book ', 80],[' Nike Shoes ', 799]], write a loop and constantly ask users what they want to buy, The user selects a product number, adds the item as a shopping cart, and when the user enters Q, prints out the shopping
Car Merchandise and exit:
Products = [['Iphone8', 6888],['MacPro', 14800],['Xiaomi 6', 2499],['Coffee', 31],[' Book', 80],['Nike Shoes', 799]]shop_cart= [] whileTrue:Print("-------------Product List------------")     forIndex,pinchEnumerate (Products):#You can hit the index number.        Print('%s. %s%s'% (Index,p[0],p[1]))#The fixed format output used hereChoice= Input ("Choice:")    ifChoice.isdigit ():#determine whether a numberChoice =int (choice)ifChoice >= 0 andChoice < Len (products):#determine if the selected item is correctshop_cart.append (Products[choice])Print("Add%s to the Shop_cart"%(Products[choice]))Else :            Print("no such product")    elifChoice = ='Q':        ifLen (Shop_cart) >0:Print("---------------Shopping Cart------------")             forIndex,iinchEnumerate (shop_cart):Print("%s. %s%s"% (index,i[0],i[1]))         Break                                

String usage:

What is a string: A string is a collection of ordered characters, storing and representing basic textual information with a literal, and a string of text in a pair of quotation marks. Strings are characterized by an immutable order, and if modified, new memory addresses are re-opened.

How to view Source: Ctrl + click the string command.

Some common uses:

S.captalize (): Initial capital/s.casefold (): All lowercase/s.center (50, ' * '): string in the middle, next to fill with the specified character/s.count (' O '): The number of O in the string/s.endwith ('! ') : Judge whether to! End/s.find (' O '): Look for the index of O/s3.format: Format the input string, such as S3 = ' My name is {0},i am {1} years old! ', using S3.format (' Spencer ', 22) to format the input.

Ganso Type:

Ganso is actually similar to the list, but also to save a group of numbers, but once it is created can not be modified, can not be added or deleted, so called a read-only list, mainly for the display of some configuration information. But he can do slices, syntax is parentheses, inside can be nested list. Although the meta-ancestor is immutable, the variable elements nested within the ancestor are mutable, such as lists.

Dictionaries and Features:

A dictionary is a type of key-value data. Syntax: curly braces, which first write the key and then write Value,key after adding: Each piece of information needs to be separated. The first feature is the KEY-VALUE structure, the value of key needs to be hashed, so key needs to be guaranteed unique immutable, can hold multiple values, can be modified, can not be unique, no, can be quickly found. The reason for a quick look is to hash the key into a string of numbers, indexed by the dichotomy, and so on.

View: dic[' key ', this type of view if it does not exist, will be an error. Dic.get (' key '), this way to see if it does not exist will show none.

Add/Modify: dic[' key ' = ' value '

Determine if it exists: ' Key ' in dic

Nesting of dictionaries:

See all Value/key:info.values ()/info.keys ()

Dictionary deletion: Info.pop (' key ')/info.popitem () This method is randomly deleted/del dic[' key ']

The cycle of the dictionary:

 for inch dic:     Print (Key,dic[key])

Python full stack development-list, Ganso, Dictionary, collection-7.13

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.