Python bubbling algorithm, lambda expression

Source: Internet
Author: User

======================================================================== Bubble Algorithm (personal understanding is similar to the IP interchange scheme, or the teacher becomes a small fish bubble bubble ) How do I sort the numbers in a list? Method 1 (recursion required)Li =[5,4,3,2,6] #定义一个列表Print Li #调试打印出列表For m in range (4): #通过循环的方式获得一个从0开始的序列, a few numbers will be written a few times, and here the numbers are all over again .NUM1 = li[m] #开始定义数值对比, starting from After, Li[m] that's the No. 0 value, 5 .num2 = li[m+1] #第一个数字和谁进行对比呢? And the second one, because it's a cycle so long li[m+1], this value is 4if NUM1 > num2: #开始进行判断了, first number 5, greater than the second number 4 ?temp = li[m] #5大于4 need to change position more than 4 of this 5 save to temp variable temp = 5 Li[m] = num2 #第一个数原来是5, but after judging it to redefine it is 4, this step is in the order of Exchangeli[m+1] = temp # First redefined in order to 4, where is the second position replaced by 5,5? In a temporary variable, but don't go around, we're going to loop it 5 times, so the number behind will be a little bit more .Print Li #再重新看一下列表, the nearest two numbers for a change results obtained [4, 3, 2, 5, 6] #这个结果就是, but this is obviously not the end of the job, need recursion, that is, the range will need to subtract 1 per 1 .   Method 2Li = [66,55,33,44,11,77]for N in Range (1,len (LI)): #制造出一个循环, starting from 1, Len total length of the example is read from 1 to 5 exactly, meaning to not read the first number 0 bitsFor M in range (Len (LI)-N): #循环每次都让它减少一次变成造数5次, build several 4 times, build number 3, recursion ...NUM1 = Li[m] #第一个被定义的数字就是0位, 66 cycles away num2 = li[m+1] #第二个被定义的每次加1, that is, starting from 1 bits, 55 down.               If NUM1 > num2: #开始进行判断了必须条件, first number 66, greater than the second number 55? temp = Li[m] #66大于55 need to change position more than 55 of this 66 save to temp variable temp = li[m] = num2 #第一个数原来是66, but after judging It's going to have to be redefined. It is 55, this step is in the Exchange order li[m+1] = temp # The first number 66, change the second position to 66,66 where? In a temporary variable, but don't go around, we're going to loop it 5 times, so the number behind will be a little bit more . results obtained [one, one, one, one, one, one, one, one] Method 3 This is similar to Method 2li = [66,55,22,11,33]For m in range (Len (LI)-1): #定义一个循环减去1, that is, 0123 4 numbers are cycledFor N in range (M+1,len (LI)): #循环长度m每次加1到列表长度5NUM1 = li[m] #第一个数等于0123 Each value but decreases one number at a timenum2 = li[n] #第二个数等于1234 value each time remember to reduce a number each time
if NUM1 > num2: #开始进行判断了必须条件, first number 66, greater than the second number 55 ?temp = li[n] #66大于55 need to change position more than 55 of this 66 save to temp variable temp = Li[n] = li[m] #再次赋值, is directly the second number 55 equals the first value Li[m] = temp # First number 66, change the second position to 66,66 where? In the temporary variable, we have a For loop 5 times, so the number behind will be a bit more Print Li results obtained [one, one, one, one, and one]  =============================================================== lambda expression When learning the condition operation, for the seen if Else statement, you can use what we call a tri-wood operation or ternary operation to represent: #1. General examplesif = =:name = ' Leyno 'Else:name = ' leyno100 ' #2. Ternary operationsname = ' Leyno ' if + = ' leyno100 'Print name  for simple functions, there is also a simple representation of the lambda expression #1. General normal way to define functionsdef func (leyno):return Leyno + 1 #执行函数使用的是return return is the meaning of the returned value, when a return appears representing the end of the function life cycle result = Func (+)   #2. How lambda expressions are defined#处理简单逻辑 Auto ReturnMy_lambda = lambda Arg:arg + 1 #定义lambda表达式result = MY_LAMBDA (123)Print ResultWhat is the point of lambda existence? is the concise representation of the function. use built-in functions to demonstrate lambda 1.mapIterates through the sequence, operates on each element of the sequence, and finally obtains a new sequence.#需求给列表内每个数值加10Li = [11,22,33]new_list = map (lambda a:a+100, li) print new_list results obtained [111, 122, 133]  #需求两个列表内的值相加Li = [11,22,33]S1 = [1,2,3]new_list = map (lambda a,b:a + B, Li, s1) print New_list results obtained [[in]  #lambda加函数需求方式The #复杂格式 operates on each element of the sequence and eventually obtains a new sequence. Li = [11,22,33,44]S1 = [1,2,3,4]def func3 (A, B): return a + bnew_list = map (func3,li,s1) Print new_list results obtained [[in]  #简单格式can simplify the steps to a single line and return automatically Li = [11,22,33,44]S1 = [1,2,3,4]print map (Lambda a,b:a + B, Li, S1) results obtained [[in]   2.filterFilter the elements in a sequence, and finally get a sequence that matches the criteria#需求filter Filter The elements in the sequence, and finally get the qualifying sequence filter gets the specified element collection operation#多一步操作实现方式Li = [11,22,33,44,55]new_list = filter (lambda abc:abc>22, li) print new_list results obtained [from]  #简便一步的实现方式Li = [11,22,33,44,55]print filter (Lambda abc:abc>22, Li) results obtained [from]   3.reduceTo accumulate all elements within a sequence #reduce对于序列内所有元素进行累计操作 cumulative action # The first parameter of reduce, the function must have two parameters is a precondition # reduce the second argument, the sequence to loop is 2,3,4,5,5,6,7,8# The third parameter of reduce, the initial value is the small plus sign#需求要求列表内数值相加#多一步操作实现方式Li = [11,22,33,44]result = reduce (lambda arge1,arge2:arge1 + arge2,li) Print result the results obtained #简便一步的实现方式Li = [11,22,33,44]print reduce (lambda arge1,arge2:arge1 + arge2,li) the results obtained

Python bubbling algorithm, lambda expression

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.