The acquaintance Python function (day08 job Problem)

1. 
Write function, check all get the odd bit index corresponding elements of the incoming list or tuple object, and returns it to the caller as a new list.

the conf DEF (L):
Lisl = L [ . 1 :: 2]
IF type (Lisl) =! List:
Lisl = List (Lisl)
return Lisl
Print (the conf ([ . 1, 2, . 3, . 4]))
Print ( ' =========== ')
Print (conf (( 1, 2, 3, 4)))

experience:
by this question, we can find a lot of issues, such as whether the underlying data type, or in function, when the list of strings, tuples, the index icon appears next
time to get parity index by index, we should first think of the index slice

2.
write a function to determine the value of the incoming user (string , lists, tuples) length is greater than 5.

the conf DEF (L):
IF len (L)> . 5:
Print ( 'value is greater than the length of the input. 5')
the else:
Print ('Value of length of the input is not more than. 5')
the conf ( 'ABCDEFG')
the conf ([ . 1, 2, . 3, . 4, . 5, . 6])
the conf (( . 1, 2, . 3, . 4, . 5, . 6))

The title summary : While the custom function when we can solve specific problems, but we still do not forget our previous method of
these methods not only reduce the time we write code execution efficiency is also higher than the code we wrote it myself, eg.len ()


3. write a function to check the incoming length of the list, if more than 2, then only preserve the contents of the first two lengths, and returns the new content to the caller.

DEF the conf (L):
IF len (L)> 2:
L = L [ 0: 2]
return L
Print (the conf ([ . 1, 2, . 3, . 4])) [ . 1, 2]
Print (the conf ([ . 1 , 2])) [ . 1, 2]
Print (the conf ([. 1])) [ . 1]
Print (the conf ([ 0, 0, . 1, 2, . 3, . 4, . 5])) [ 0, 0]


4. Write function, calculate the incoming string [Digital], [letter ], [space] and the number of [other], and returns the result.


FUNC DEF (L):
DICL = { 'digit for': 0, 'Word': 0, 'Space': 0, 'OTHER': 0}
for I in L:
IF i.isdigit ():
DICL [ 'digit for' ] = DICL [ 'digit for'] + . 1 elif i.isalpha (): DICL [ 'Word'] = DICL [ 'Word'] + . 1 elif i.isspace (): DICL [ 'Space'] = DICL [ 'Space '





DICL [ 'OTHER'] = DICL [ 'OTHER'] + 1 return DICL Print (FUNC ( 'abc123 #%!')) this question Summary: The main shows two things: First, the first question I had in mind confuse the issue when put side of isdigit: to determine whether the string of digits isalpha: to determine whether a string of letters isspace: to determine whether a string whitespace before I think isdigit () are: to determine whether or not a number, but in fact is a judge whether the string of digits, which is very different, the former using it to determine whether the character is not a number, it must be numeric character itself and determine whether the string of digits, but it is the original character is a character string, but judge the content of which is not a string of numbers in this regard, we should bear in mind, because on this issue has eaten a loss, then that is isspace () to determine whether whitespace, the way to write down second when one is encountered when many variables, such as this question is, is composed of four variables, we have to try to put it there in the array, the array should be a corresponding relationship, key-> values total Body problem is to do less or no experience, just like the first question, and even when the method mentioned in the list of questions but you can not think of a slice 5. Write a function to check incoming user objects (strings, lists, tuples ) whether each element has an empty content, and returns the result. FUNC DEF (L): IF of the type (L) IS str



















and l :
for i in l :
if i.isspace():
return True
if type(l) is list or type(l) is tuple and l :
for i in l :
if not i :
return True
if not l:
return False

print(func(' 123'))
print(func('123'))
print(func([[],1,2]))
print(func([1,2]))
print(func([1,2,[]]))
print(func((1,2,'',())))
True
None
True
None
True
True

This problem Summary: On the question the basic framework and ideas on the whole I think is the same, but some details are handled is not perfect, and even ignored some of the key knowledge points logically
For example, in the beginning I just think because the string and a list of Ganso empty form is not the same, the string space is a space, and lists and tuples empty are (), [].
but the problem we have neglected to determine whether or not the parameter itself problematic, because [], () are also a list of types and Ganso type, if not to determine whether they exist,
they will enter the for loop, so no matter how the final reading, the return is True
on problem solving, knowledge and learning time, I ignored and and knowledge are not, but by this question, I have seen some problems, and, and not the function is very powerful,
sometimes we are doing the project, will certainly determine whether or not it exists, and L can be resolved, l bit true value will continue, at the same time learn how to use not. Many feelings, previously ignored
a knowledge point can actually be a key solution of this question.
At the same time, also saw the return of the role, it is not only able to return value, while the end of the cycle function in a function, return gradually realize the role, I feel like a break before use


6. Write a function to check incoming dictionary the length of each value, if greater than 2, then only preserve the contents of the first two lengths, and returns the new content to the caller.

FUNC DEF (X):
for I in X:
IF type (X [I]) IS STR and X [I]:
X [I] = X [I] [ 0: 2]
IF type (X [I]) IS int and X [I]:
X [I] = STR (X [I])
X [I] = X [I ] [ 0: 2]
IF Not X [I]:
return False
return X
Print (FUNC ({ 'Alex': '1234', 'lisir': 123}))

of the present title summary: this question is perfectly done, simply invincible, first of all let this question when you pass arguments dictionary, when there is a dictionary, we need to consider the issue of keys and values,
for the cycle, the default value of the print key, we all know, when we want to modify the dictionary format it is generally: the dictionary name [key], we can use to modify its value by key,
in this question which I give themselves a little more difficult, for example, I pass the value when the second surgery, plastic surgery can not be directly used len () method, I turn them strong, and
while this time I use slices to achieve this function, reflects the characteristics of learning and using of

this is the standard answer this question:

DEF FUNC (dic):
for k in DIC:
IF len (DIC [K])> 2:
DIC [K] = DIC [K] [: 2]
return DIC
DIC = { "K1": "v1v1", "K2": [ . 11, 22 is, 33 is, 44 is]}
Print (FUNC (DIC))

Absolute their Code consideration is not very comprehensive, such as its code on the argument takes into account the list, but when I slice is a direct cut card,
and it is judged slices, if and when you list or string, then my code is not enough 2 It may be error, but logically it would be more rigorous code


7. write a function to receive two digital parameters, return to the relatively large numbers.

DEF FUNC (A, b):
return A IF A> b the else b
Print (FUNC ( 1, 2))

This question Summary: In doing this question when, based on experience learning the C language, my first thought is the ternary operator solving: a> b a:? b . Of course, this is the C language, then Python it?
A if A> b . else b are the same meaning if a> b, returns a, b otherwise

ternary operator:
variable = True condition results returned if condition else return False condition results
must be the result
If and else must have
only the simplest case


8. Write a function, the user modify the incoming file name you want to modify the contents of the function is executed to complete the entire batch file modification operations (advanced).

DEF the conf (name, context):
with Open (name, 'R & lt', encoding = 'UTF-. 8') AS F1, Open ( 'homework02', 'W', encoding = 'UTF-. 8') AS F2:
for Line in f1:
IF 'the Hello' in Line:
Line = line.replace ( 'the Hello', context)
f2.write (Line)
conf ( name = 'homework01', context = 'today's job has some difficulty')
Import OS
The os.remove ( 'homework01')
os.rename ( 'homework02',

This problem Summary: file modification or unskilled, written when the idea is not clear, even in the IF 'the Hello' in order in the line backwards, we can see the idea of chaos, spare no practice!

Analysis of ideas: first do file modification time we need to know, the file itself can not be changed, so our idea is to read the file information. Use for traversal, read line by line.
When confronted with the information you want to modify that information when the line is to be replaced, each row of data types is confident str type, we create a new empty file, write function,
will be revised and write information to the newly created file and then until the for loop is completed, we transferred os, beginning first output file, and then rename the file,
so it looks like a file has been modified as, in fact, the operation is not the case.

Guess you like

Origin www.cnblogs.com/if-it-is-possible/p/11461552.html