-
Write a function to realize the function of maketrans and convert two strings into a dictionary. The characters in the first string are keys and the characters in the second string are values
First string: 'abcmn' second string: 'one, two, three, four, five'
Results: {'a': 'one', 'b': 'two', 'c': 'three','m ':' four ',' n ':' five '}
def str_1(str1: str, str2: str): dict1 = {} for i in range(len(str1)): dict1[str1[i]] = str2[i] return dict1 dict1 = str_1('abcmn', 'one two three four five') print(dict1)
-
Write your own join function to connect the elements in any sequence with the specified string into a new string
Sequence: [10, 20, 30, 'abc'] string: '+' result: '10+20+30+abc'
Sequence: 'abc' string: '–' result: 'a – b--c'
Note: the elements in the sequence can not be strings
def new_join(list1, str1): str2 = '' for i in list1[:-1]: str2 += str(i) + str1 return str2 + list1[-1] list1 = [10, 20, 30, 'abc'] str1 = '+' str3 = new_join(list1, str1) print(str3)
-
Write an input own upper function to judge whether the specified string is a pure uppercase string
'AMNDS' -> True
'amsKS' -> False
'123asd' -> False
def new_upper(str1): for i in str1: if not 'A' <= i <= 'Z': return False else: return True str1 = 'AMNDS' str2 = new_upper(str1) print(str2)
-
Write a clear function to clear the specified list.
Note: the function is to clear the original list without generating a new list
def new_list(list1): for i in list1[0:]: del list1[0] return list1 list1 = [10, 20, 30, 'abc'] list1 = new_list(list1) print(list1)
-
Write a reverse function to reverse the elements in the list
Two methods: 1 Generate a new list 2 The order of the elements in the original list is directly modified without generating a new list
def new_list(list1): list2 = [] for i in list1[::-1]: list2.append(i) return list2 list1 = [10, 20, 30, 'abc'] list1 = new_list(list1) print(list1)
-
Write a replace function to replace the specified substring in the string with a new substring
Original string: 'abc123abc ha ha uui123' old substring: '123' new substring: 'AB'
Results: 'abcbabc ha ha uuiAB'
def new_replace(str1, str2, str3): str4 = str1.split(str2) str5 = str3.join(str4) return str5 str1 ='abc123abc Ha ha ha uui123' str2 ='123' str3 ='AB' str = new_replace(str1, str2, str3) print(str)
-
Write a function that can get the ten digits of any integer
123 -> 2
82339 -> 3
9 -> 0
-234 -> 3
def num_1(num1): x = num1 // 10 % 10 return x num = num_1(123) print(num)
-
Write a function to realize the function of mathematical set operator & and find the common part of two sets:
Set 1: {1, 2, 3} set 2: {6, 7, 3, 9, 1}
Result: {1, 3}
def set_1(set1, set2): set3 = set() for i in set1: if i in set2: set3.add(i) return set3 set1 = {1, 2, 3} set2 = {6, 7, 3, 9, 1} s = set_1(set1, set2) print(s)
-
Write a function to implement the function of its own dictionary update method, and add all the key value pairs in one dictionary to another dictionary
Dictionary 1: {a ': 10,' b ': 20} dictionary 2: {name': Zhang San, 'age': 18} - > the result turns Dictionary 1 into: {a ': 10,' b ': 20, name': Zhang San, 'age': 18}
Dictionary 1: {a ': 10,' b ': 20} dictionary 2: {a': 100, 'c': 200} - > the result makes Dictionary 1: {a ': 10,' b ': 20,' c ': 200}
def new_update(dict1, dict2): for key in dict2: dict1.setdefault(key, dict2[key]) return dict1 dict1 = {'a': 10, 'b': 20} dict2 = {'name': 'Zhang San', 'age': 18} a = new_update(dict1, dict2) print(a)
-
Write a function to judge whether the specified number is the number of daffodils
12321 -> True
2332 -> True
9876789 -> True
1232 -> False
def new_num(num): n = str(num) if n[:] == n[::-1]: return True else: return False num = 1232 a = new_num(num) print(a)
-
Write a function to judge whether the specified number is complete (more difficult)
Perfect: the sum of the true factors is equal to the number itself
For example, 6 is a perfect number, the truth factors of 6 are 1, 2 and 3, and 1 + 2 + 3 equals 6, so 6 is a perfect number
28 is the perfect number. The true factors of 28 are 1, 2, 4, 7 and 14. 1 + 2 + 4 + 7 + 14 equals 28, so 28 is the perfect number
12 is not perfect: the true factor of 12: 1, 2, 3, 4, 6, 1 + 2 + 3 + 4 + 6 is not equal to 12
def new_num(num): j = 0 for i in range(1, num): if num % i == 0: j += i if j == num: return True else: return False num = 21 print(new_num(num))
day11 basic operation of function
Posted by henrygao on Tue, 14 Dec 2021 03:40:06 +0100