Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Mini Program with Python

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "how Python achieves Mini Program", which is easy to understand and clear. I hope it can help you solve your doubts. Let the editor lead you to study and learn how to achieve Mini Program with Python.

① 2D list

Returns a two-dimensional list based on the given length and width, as well as initial values:

Def initialize_2d_list (w, h, val=None): return [[val for x in range (w)] for y in range (h)]

For example:

> > initialize_2d_list (2Jue 2) [[None, None], [None, None] > initialize_2d_list (2Jing 2 Jing 0) [[0,0], [0,0]] ② function cut array

Use a function to apply to each element of an array so that the array is cut into two parts. If the value returned by the function applied to the element is True, the element is cut to the first part, otherwise it is divided into the second part:

Def bifurcate_by (lst, fn): return [[x for x in lst if fn (x)], [x for x in lst if not fn (x)]]

For example:

> bifurcate_by (['beep',' boop', 'foo',' bar'], lambda x: X [0] = ='b') [['beep',' boop', 'bar'], [' foo']] ③ intersection

After two arrays are applied by a function, the original elements of the common elements are extracted from the first array to form a new array:

Def intersection_by (a, b, fn): _ b = set (map (fn, b)) return [item for item in an if fn (item) in _ b]

For example:

> from math import floor > intersection_by ([2.1,1.2], [2.3,3.4], floor) [2.1] ④ maximum subscript

Returns the subscript of the maximum value in the array:

Def max_element_index (arr): return arr.index (max (arr))

For example:

> max_element_index ([5, 8, 9, 7, 10, 3, 0]) 4 ⑤ array symmetry difference

Find out the different elements in the two arrays and combine them into a new array:

Def symmetric_difference (a, b): _ a, _ b = set (a), set (b) return [item for item in an if item not in _ b] + [item for item in b if item not in _ a]

For example:

> number of symmetric_difference ([1,2,3], [1,2,4]) [3,4] ⑥ clips

Return num if num falls within a range of numbers, otherwise return the nearest boundary to that range:

Def clamp_number (num,a,b): return max (min (num, max (aMaginb)), min (aMaginb))

For example:

> > clamp_number (2 and 3) 3 > > clamp_number (7 and 3) 7 > > clamp_number (124 and 3) 10 ⑦ key value mapping

Recreate the object using the object's key and run the function to create a value for each object's key

Use dict.keys () to traverse the key of the object and generate a new value through the function

Def map_values (obj, fn): ret = {} for key in obj.keys (): ret [key] = fn (obj [key]) return ret

For example:

> > users = {... 'fred': {'user':' fred', 'age': 40},... 'pebbles': {' user': 'pebbles',' age': 1}.} > map_values (users, lambda u: U ['age']) {' fred': 40, 'pebbles': 1} > map_values (users, lambda u: U [' age'] + 1) {'fred': 41,' pebbles': 2} ⑧ case conversion

Change the first letter of an English word from uppercase to lowercase

Upper_rest parameter: sets whether to convert the case of letters other than the first letter

Def decapitalize (s, upper_rest=False): return s [: 1] .lower () + (s [1:] .upper () if upper_rest else s [1:])

For example:

> decapitalize ('FooBar')' fooBar' > decapitalize ('FooBar', True)' fOOBAR' ⑨ summation with the same bond

Sum the objects with the same key value in each dictionary in the list:

Def sum_by (lst, fn): return sum (map (fn,lst))

For example:

> sum_by ([{'n': 4}, {'n': 2}, {'n': 8}], lambda v: v ['n']) 14 ⑩ lines to find the number of occurrences

Find the number of times a number appears in the list and:

Def count_occurrences (lst, val): return len ([x for x in lst if x = = val and type (x) = = type (val)])

For example:

>

Subdivide a list according to the desired size:

From math import ceildef chunk (lst, size): return list (lambda x:lst [x * size:x * size + size], list (range (0, int (ceil (len (lst) / size))

The effect is as follows:

Chunk ([1, 2, 3, 4, 5], 2) # [1, 2, 4], 5]

In return, the second argument to map is a list, and map uses each element in the list to call the function function of the first parameter, returning a new list of values returned by each function function.

⑫ digital rotation array

It is also an application about map that splits shaping numbers into arrays:

Def digitize (n): return list (map (int, str (n)

The effect is as follows:

Digitize (123) # [1,2,3]

After it converts the integer n to a string, it automatically serializes and splits the string, and finally applies the element to the first parameter of map, which is converted to plastic and returned.

⑬ non-recursive Fibonacci

The sum of the first two numbers is the value of the third number, such as 0, 1, 1, 2, 3, 5, 8, 13.

If we use recursion to implement this algorithm, it is very inefficient, so we implement it in a non-recursive way:

Def fibonacci (n): if n

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 280

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report