day 9 - 2 function exercise

Keywords: Python encoding

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

def func(lis):
    print(lis[1::2])    #Even number
    return lis[::2]     #Count
    
f=[1,2,3,4,5,6]
print(func(f))

 

2. Write a function to determine whether the length of the value (string, list, tuple) passed in by the user is greater than 5.

#In two lines
def func2(lis):
    return len(lis)>5 #If it is greater than 5, it will return automatically True

f=[1,2,3,4,5,6]
print(func2(f))
#compare low Method
def func2(lis):
    k = len(lis)
    if k > 5:
        print("Greater than 5")
    else:
        print("Up to 5")

f=[1,2,3,4,5,6]
func2(f)

 

3. Write a function to check the length of the incoming list. If it is greater than 2, only the first two lengths of content will be retained and the new content will be returned to the caller.

#It is not necessary to judge whether the list length is greater than 2. Slice will automatically intercept the value of the corresponding position
def func3(lis):
    return lis[0:2]

f=[1,2,3,4,5,6]
print(func3(f))

 

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

#Using dictionaries to store and call values
def func4(str):
    dic = {'letter':0,"num":0,"space":0,'other':0}
    for i in str:
        if i.isalpha():
            dic['letter'] +=1
        elif i.isdigit():
            dic['num'] += 1
        elif i ==" ":
            dic['space'] += 1
        else:
            dic["other"] += 1
    return dic

f="%123abc   qwe789%"
print(func4(f))
#compare low Of idea
def func4(str):
    letter = 0
    num = 0
    space = 0
    other = 0
    for i in str:
        if i.isalpha():
            letter +=1
        elif i.isdigit():
            num += 1
        elif i ==" ":
            space += 1
        else:
            other += 1
    return(letter,num,space,other)

f="%123abc   qwe789%"
print(func4(f))

 

5. Write a function to check whether each element of the object (string, list, tuple) passed in by the user contains empty content and return the result.

def func5(space):
    if type(space) is str:    #Parameter is a string
        for i in space:
            if i == " ":
                return True
    if type(space) is list or type(space) is tuple: #Parameter is list or ancestor
        for i in space:
            if not i:
                return True
    
f="%123abc   qwe789%"
print(func5(f))

f2 = [1,2,3]
print(func5(f2))

f3 = (1,2,"",3)
print(func5(f3))

 

6. Write a function to check the length of each value of the incoming dictionary. If it is greater than 2, only the first two lengths of content will be retained and the new content will be returned to the caller.

dic = {"k1": "v1v1", "k2": [11,22,33,44]}
def func6(d):
    for i in d:
        if len(d[i]) > 2:
            d[i] = d[i][:2]
    return d

print(func6(dic))
print(func6({'name':'ysg','age':'1234'}))

 

7. Write a function to receive two numeric parameters and return the larger one.

#Three element operation
def func7(a,b):
    return a if a>b else b
   
print(func7(56,23))
#compare low Of idea
def func7(a,b):
    if a>b:
        return a
    else:
        return b
print(func7(23,56))

 

8. Write function. The user passes in the modified file name and the content to be modified. Execute the function to complete the batch modification operation (Advanced) of the whole file.

#In fact, there are three parameters in the incoming function
#Contents before file name modification contents after modification
def func8(filename,a,b):
    with open(filename,encoding='utf-8') as f,open('%s.bak'%filename,'w',encoding='utf-8')as f2:
        for line in f:
            if a in line:
                line = line.replace(a,b)
            f2.write(line)

    import os
    os.remove(filename)     # Delete files
    os.rename('%s.bak'%filename,filename)   # rename file
                
print(func8('D:/py/log/test.txt','in','IN'))

Posted by sweetstuff2003 on Mon, 02 Dec 2019 17:37:36 -0800