Lambda, list-generated, dictionary-to-list ordering

Source: Internet
Author: User

1. function returns multiple values:

1) If the function returns multiple values, the returned values are placed in a tuple

def say ():     = 1    = 2    = 3    return= say ()print( RES)>>> (1, 2, 3)

2) function if you return multiple values, you can use multiple variables to receive each

def say ():     = 1    = 2    = 3    return= say ()print(res1)   #>>>1print(res2)   # >>>2 Print (RES3)   # >>>3

2, Lambda: anonymous function, function is simple, use only once, do not need to define

Lambda x:x+1  # before the colon is the return value, the colon is followed by the function body, and the function's processing logic Print(res (1))> >> 2

3. List-generated (list)

 for  in range (1,101,2-inif i%2==1]  # or write like this  print(l)>>>[1, 3, 5, 7, 9 ...]

if the form outside of the list generation is ():L is not a list , but a generator

 for inch if i%2==1)print(l)  #L is a generator #print (l.__next __ ())   #生成器调用一次取一个值for in  L:       print(i)>  >> <generator Object <genexpr> at 0x00bfd8c8>>>>1 3 5 7 9 ...

# each time the generator loops, it calculates only one element per rule, in memory, than the list save memory

#list is to put all the elements in memory.

4, three-dimensional expression

A = 5= 4ifelse b   # ternary expression print= 5  = 4if a>B:    = aelse:    = b  Print(c)

5, the dictionary is unordered, directly to the dictionary sort does not exist. First, turn the dictionary into a list and sort

#sorted (): Automatically loops through the elements inside a two-dimensional array

D = {'a'75A'b': 2,'C': 3}Print(D.items ())#get a two-dimensional arrayres = sorted (D.items (), key=LambdaX:x[0])#The key value of the key= dictionary, sortedRes= Sorted (D.items (), key=LambdaX:X[1])#sort based on the value of the dictionaryPrint(RES)>>> Dict_items ([('b', 2), ('a', 5), ('C', 3)])>>> [('a', 5), ('b', 2), ('C', 3)]

To loop, take multiple:

L = [[1,2,3],[3,4,5],[5,6,7]] for in L:  #a,b,c is 3, Each of the above small list contains 3 elements?     Print(a,b,c)

6, when defining the function, tell others what type of argument should be passed

def is_float (S:STR):     Pass

Lambda, list-generated, dictionary-to-list ordering

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.