当前位置:网站首页>Exercise: even sum, threshold segmentation and difference (two basic questions of list object)
Exercise: even sum, threshold segmentation and difference (two basic questions of list object)
2022-04-23 17:29:00 【Dream spirit_ cq】
Python Official website : https://www.python.org/
here , only python the front . Unfortunately, it's the original English version . therefore , I want to practice English reading .🧐🧐
Self study is not a mysterious thing , A person's self-study time is always longer than that in school , There are always more times when there are no teachers than when there are teachers .
—— Hua Luogeng
note : Even sum 、 Threshold segmentation and subtraction ( list Two basic questions of the object )
One 、 Even sum

Code
def sum_of_evens(*evens):
''' Find the sum of even numbers in the input vector '''
tem_lis = []
for i in evens:
if str(i).isdigit() is False:
tem_lis.extend(list(i)) # Cluster collection .
else:
tem_lis.append(i) # Single parameter collection .
tem = []
for i in tem_lis:
if i % 2 == 0: # Remove the odd number .
tem.append(i)
return sum(tem) # Return and value .
print(cut_line(), color(1, 'f_green')) # Print split lines .
print(f"{
'':>8} Pass it on directly , Output :{
sum_of_evens(1, 3, 6, 9)}") # Pass it on directly .
s = sum_of_evens # Alias function .
print(f"\n\n{
'':>8}list Cluster transmission parameters , Output :{
s([1, 9, 8, 10, 13])}") # Cluster transmission parameters .
print(f"\n\n{
'':>8}tuple The ginseng , Output :{
s((1, 9, 8, 5, 12, 10, 13))}") # Cluster transmission parameters .
print(f"\n\n{
'':>8}list Mixed transmission , Output :{
s(1, 2, [1, 8, 10, 13], 3, 8)}") # Single 、 Cluster mixed transmission .
print(f"\n\n{
'':>8}tuple Mixed transmission , Output :{
s(1, 2, (1, 8, 13), 3, 8)}") # Single 、 Cluster mixed transmission .
print(f"{
color(0)}{
cut_line()}") # Print split lines .
wait()
Running effect

Two 、 Threshold segmentation and subtraction

Code
def difference_between_L(n, L=0):
''' Returns the absolute value of the difference between the sum of the new set divided by the threshold , If there are two threshold elements in the input set , Split with first out .'''
tem, tem2 = [], [] # Initialize new collection .
flag = False # The initial value of the partition ID is false .
for i in n:
if i == L:
flag = True # Segmentation threshold encountered , Set the division mark to true .
print(f"\n Segmentation threshold :{
L}")
L = '' # Set null character , Avoid not collecting the segmentation threshold of the second occurrence .
continue # End this cycle , To avoid collecting segmentation thresholds .
if flag is True:
tem2.append(i)
else:
tem.append(i)
print(f"\n Split set :\n\n{
tem},{
tem2}")
return abs(sum(tem) - sum(tem2))
db_L = difference_between_L # Alias function .
n = 2, 5, 2, 6, 0, 2, 7, 6, 0, 17, 3
print(cut_line())
print(f"{
cut_line()}{
'':>4} Input :{
n}\
\n\n{
'':>16} Output :{
color(db_L(n, 7), 'f_green')}{
cut_line()}")
wait()
Running effect


complete Python Code
My solution to the problem , Code comments have been incorporated , I won't repeat it in my blog .
( If you can't make clear the function from the statement annotation , Please leave a message in the comment area for advice and discussion .)
#!/sur/bin/env python
# coding: utf-8
''' filename: /sdcard/qpython/tem.py The dream spirit _cq Code yard '''
from mypythontools import color, wait, cut_line # Load the function to be used in the required code module from the self code tool module .
def sum_of_evens(*evens):
''' Find the sum of even numbers in the input vector '''
tem_lis = []
for i in evens:
if str(i).isdigit() is False:
tem_lis.extend(list(i)) # Cluster collection .
else:
tem_lis.append(i) # Single parameter collection .
tem = []
for i in tem_lis:
if i % 2 == 0: # Remove the odd number .
tem.append(i)
return sum(tem) # Return and value .
print(cut_line(), color(1, 'f_green')) # Print split lines .
print(f"{
'':>8} Pass it on directly , Output :{
sum_of_evens(1, 3, 6, 9)}") # Pass it on directly .
s = sum_of_evens # Alias function .
print(f"\n\n{
'':>8}list Cluster transmission parameters , Output :{
s([1, 9, 8, 10, 13])}") # Cluster transmission parameters .
print(f"\n\n{
'':>8}tuple The ginseng , Output :{
s((1, 9, 8, 5, 12, 10, 13))}") # Cluster transmission parameters .
print(f"\n\n{
'':>8}list Mixed transmission , Output :{
s(1, 2, [1, 8, 10, 13], 3, 8)}") # Single 、 Cluster mixed transmission .
print(f"\n\n{
'':>8}tuple Mixed transmission , Output :{
s(1, 2, (1, 8, 13), 3, 8)}") # Single 、 Cluster mixed transmission .
print(f"{
color(0)}{
cut_line()}") # Print split lines .
wait()
def difference_between_L(n, L=0):
''' Returns the absolute value of the difference between the sum of the new set divided by the threshold , If there are two threshold elements in the input set , Split with first out .'''
tem, tem2 = [], [] # Initialize new collection .
flag = False # The initial value of the partition ID is false .
for i in n:
if i == L:
flag = True # Segmentation threshold encountered , Set the division mark to true .
print(f"\n Segmentation threshold :{
L}")
L = '' # Set null character , Avoid not collecting the segmentation threshold of the second occurrence .
continue # End this cycle , To avoid collecting segmentation thresholds .
if flag is True:
tem2.append(i)
else:
tem.append(i)
print(f"\n Split set :\n\n{
tem},{
tem2}")
return abs(sum(tem) - sum(tem2))
db_L = difference_between_L # Alias function .
n = 2, 5, 2, 6, 0, 2, 7, 6, 0, 17, 3
print(cut_line())
print(f"{
cut_line()}{
'':>4} Input :{
n}\
\n\n{
'':>16} Output :{
color(db_L(n, 7), 'f_green')}{
cut_line()}")
wait()
Last one : Chinese character extraction of packet ( character string , no need re , use in )
my HOT Bo :
- use pandas Solve a small problem (1913 read )
- Iteratable object and four functions (1042 read )
- “ Happy number ” Judge (1201 read )
- Roman digital converter ( Construct element module )(1894 read )
- Rome digital ( converter | Luo )(2529 read )
- Hot: Give Way QQ Group nickname color change code (15718 read )
- Fibonacci sequence ( recursive | for )(3233 read )
- The largest rectangle in the histogram (1623 read )
- Repeat start and end of sorting array elements (1215 read )
- Telephone dialing keyboard letter combination (1277 read )
- Password strength detector (1730 read )
- Find the balance point of the list (1787 read )
- Hot: String statistics (3471 read )
- Hot: Nim game ( Smart version starts )(3327 read ) Nim game ( Optimized version )(898 read )
Recommended conditions
Click to read a thousand
Excellent articles :
- A good writer recommends :《python Complete self study course 》 Qi Wei manuscript free Serial
- OPP The three major characteristics : In the package property
- Understand through built-in objects python'
- Regular expressions
- python in “*” The role of
- Python A complete self-study manual
- Walrus operators
- Python Medium `!=` And `is not` Different
- The right way to learn programming
source : Laoqi classroom
Python Getting started 【Python 3.6.3】
A good writer recommends :
- 【8 Application fields of large programming languages 】 Don't worry about choosing Linguistic Programming , Let's see what they can do first
- Good habits of reliable programmers
CSDN Practical skills blog :
版权声明
本文为[Dream spirit_ cq]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231722007760.html
边栏推荐
- Preliminary understanding of promse
- Webapi + form form upload file
- 【WPF绑定3】 ListView基础绑定和数据模板绑定
- Further optimize Baidu map data visualization
- Websocket (basic)
- In ancient Egypt and Greece, what base system was used in mathematics
- Customize my_ Strcpy and library strcpy [analog implementation of string related functions]
- Generating access keys using JSON webtoken
- 基于51单片机红外无线通讯仿真
- Milvus 2.0 質量保障系統詳解
猜你喜欢
Compare the performance of query based on the number of paging data that meet the query conditions
![[ES6] promise related (event loop, macro / micro task, promise, await / await)](/img/69/ea3ef6063d373f116a44c53565daa3.png)
[ES6] promise related (event loop, macro / micro task, promise, await / await)

超分之TDAN

Solution architect's small bag - 5 types of architecture diagrams

Qt 修改UI没有生效

Use between nodejs modules

If you start from zero according to the frame
![Using quartz under. Net core - [1] quick start](/img/80/b99417e88d544ca6e3da4c0c1625ce.png)
Using quartz under. Net core - [1] quick start

Use of todesk remote control software

Deep understanding of control inversion and dependency injection
随机推荐
Abnormal resolution of Xiaomi camera
[registration] tf54: engineer growth map and excellent R & D organization building
Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]
Manually implement simple promise and its basic functions
Baidu Map 3D rotation and tilt angle adjustment
Shell-cut命令的使用
How to manually implement the mechanism of triggering garbage collection in node
嵌入式系统中,FLASH中的程序代码必须搬到RAM中运行吗?
Manually implement call, apply and bind functions
Use between nodejs modules
Shell script -- shell programming specification and variables
If you start from zero according to the frame
Customize my_ Strcpy and library strcpy [analog implementation of string related functions]
C dapper basically uses addition, deletion, modification and query transactions, etc
C listens for WMI events
Error in v-on handler: "typeerror: cannot read property 'resetfields' of undefined"
Understanding of RPC core concepts
Seven cattle upload pictures (foreground JS + background C API get token)
Use of shell cut command
STM32 entry development board choose wildfire or punctual atom?