Add the small to the max sequence, add big to the min sequence, recalculate the new sequence, and large for max, small for Min.
Python code
def mean (sorted_list):
If not sorted_list:
Return (([],[]))
Big = Sorted_list[-1]
small = sorted_list[-2]
Big_list, small_list = mean (Sorted_list[:-2])
Big_list.append (small)
Small_list.append (BIG)
Big_list_sum = SUM (big_list)
Small_list_sum = SUM (small_list)
If Big_list_sum > Small_list_sum:
Return ((Big_list, small_list))
Else
Return ((Small_list, big_list))
Tests = [? [1,2,3,4,5,6,700,800],
[10001,10000,100,90,50,1],
Range (1, 11),
[12312, 12311, 232, 210, 30, 29, 3, 2, 1, 1]]
For L in tests:
L.sort ()
Print
Print "Source list:\t", l
L1,L2 = Mean (l)
Print "Result list:\t", L1, L2
Print "Distance:\t", ABS (SUM (L1)-sum (L2))
print '-'40
Output results
Python code
Source List:?? [1, 2, 3, 4, 5, 6, 700, 800]
Result List:?? [1, 4, 5, 800] [2, 3, 6, 700]
Distance:??? 99
----------------------------------------
Source List:?? [1, 50, 90, 100, 10000, 10001]
Result List:?? [50, 90, 10000] [1, 100, 10001]
Distance:??? 38
----------------------------------------
Source List:?? [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Result List:?? [2, 3, 6, 7, 10] [1, 4, 5, 8, 9]
Distance:??? 1
----------------------------------------
Source List:?? [1, 1, 2, 3, 29, 30, 210, 232, 12311, 12312]
Result List:?? [1, 3, 29, 232, 12311] [1, 2, 30, 210, 12312]
Distance:??? 21st
----------------------------------------
15: When using Python to match HTML tag, <> and <.What's the difference between?>?
When a regular expression is repeatedly matched, for example <The maximum matching value is returned when the program executes a match
For example:
Import re
s = 'Title’
Print (Re.match (' <.> ', s). Group ())
will return a match<title>Title</title>Instead of
and
Import re
s = '<title>Title</title>’
Print (Re.match (' <.?> ', s). Group ())
It will return
<.> This match is called greedy match <?> is called a non-greedy match.
16:python the difference between search () and match ()?
The match () function only detects if the re is matched at the start of the string, and search () scans the entire string lookup match, which means that match () is returned only if the 0-bit match succeeds, and if the match is not successful, match () Just return to None
For example:
Print (Re.match (' super ', ' superstition '). span ()) will return (0, 5)
Print (Re.match (' super ', ' insuperable ') returns none
Search () scans the entire string and returns the first successful match
For example: Print (Re.search (' super ', ' superstition '). span ()) return (0, 5)
Print (Re.search (' super ', ' insuperable '). span ()) return (2, 7)
17: How do I use Python to query and replace a text string?
You can use the sub () method to query and replace the sub method in the format: Sub (replacement, string[, count=0])
Replacement is replaced with text
String is the text that needs to be replaced
Count is an optional parameter that refers to the maximum number of replacements
Example:
Import re
p = re.compile (' (blue|white|red) ')
Print (P.sub (' Colour ', ' blue socks and red shoes ')
Print (P.sub (' Colour ', ' blue socks and red shoes ', count=1))
Output:
Colour socks and colour shoes
Colour socks and Red shoes
The Subn () method performs the same effect as a sub (), but it returns a two-dimensional array, including the replacement of the new string and the total number of replacements
For example:
Import re
p = re.compile (' (blue|white|red) ')
Print (P.SUBN (' Colour ', ' blue socks and red shoes ')
Print (P.SUBN (' Colour ', ' blue socks and red shoes ', count=1))
Output
(' Colour socks and colour shoes ', 2)
(' Colour socks and Red shoes ', 1)
18: Introduce the usage and function of except?
Python's except is used to catch all exceptions, because each error in Python throws an exception, so each program's error is treated as a run-time error.
Here is an example of using except:
Try
Foo = opne ("file") #open被错写为opne
Except
Sys.exit ("Could not open file!")
Because this error is caused by the spelling of open Opne and then captured by except, it's easy to know what's wrong with the debug program.
The following example is a better point:
Try
Foo = opne ("file") # this time except only captures IOError
Except IOError:
Sys.exit ("Could not open file")
What is the function of the pass statement in 19:python?
Pass statement does nothing, generally as a placeholder or create a placeholder, the pass statement will not perform any action, such as:
While False:
Pass
Pass is often used to create the simplest class:
Class Myemptyclass:
Pass
Pass is also often used as a todo in the software design phase, reminding you to implement the corresponding implementation, such as:
Def initlog (args):
Pass #please Implement this
20: Describe the use of the range () function under Python?
If you need to iterate over a sequence of numbers, you can use the range () function, and the range () function can generate arithmetical progression.
As an example:
For I in range (5)
Print (i)
This code will output 0, 1, 2, 3, 45 digits
A range (10) produces 10 values, or you can have range () start with another number, or define a different increment, or even a negative increment
Range (5, 10) five digits from 5 to 9
Range (0, 10, 3) increments of three, including 0,3,6,9 four digits
Range (-10,-100,-30) increments of-30, including-10,-40,-70
You can use range () and Len () together to iterate over an index sequence
For example:
A = [' Nina ', ' Jim ', ' Rainman ', ' Hello ']
For I in range (Len (a)):
Print (I, a[i])