当前位置:网站首页>练习:求偶数和、阈值分割和求差( list 对象的两个基础小题)
练习:求偶数和、阈值分割和求差( list 对象的两个基础小题)
2022-04-23 17:22:00 【梦幻精灵_cq】
Python 官网: https://www.python.org/
这里,才 python 前沿。可惜是英文原版。所以,我要练习英文阅读。🧐🧐
自学并不是什么神秘的东西,一个人一辈子自学的时间总是比在学校学习的时间长,没有老师的时候总是比有老师的时候多。
—— 华罗庚
笔记:求偶数和、阈值分割和求差( list 对象的两个基础小题)
一、求偶数和
代码
def sum_of_evens(*evens):
'''求输入向量中偶数和'''
tem_lis = []
for i in evens:
if str(i).isdigit() is False:
tem_lis.extend(list(i)) # 集群收集。
else:
tem_lis.append(i) # 单参收集。
tem = []
for i in tem_lis:
if i % 2 == 0: # 摘除奇数。
tem.append(i)
return sum(tem) # 返回和值。
print(cut_line(), color(1, 'f_green')) # 打印分割线。
print(f"{
'':>8}直接传参,输出:{
sum_of_evens(1, 3, 6, 9)}") # 直接传参。
s = sum_of_evens # 函数取别名。
print(f"\n\n{
'':>8}list 集群传参,输出:{
s([1, 9, 8, 10, 13])}") # 集群传参。
print(f"\n\n{
'':>8}tuple 传参,输出:{
s((1, 9, 8, 5, 12, 10, 13))}") # 集群传参。
print(f"\n\n{
'':>8}list 混传,输出:{
s(1, 2, [1, 8, 10, 13], 3, 8)}") # 单个、集群混传。
print(f"\n\n{
'':>8}tuple 混传,输出:{
s(1, 2, (1, 8, 13), 3, 8)}") # 单个、集群混传。
print(f"{
color(0)}{
cut_line()}") # 打印分割线。
wait()
运行效果
二、阈值分割和求差
代码
def difference_between_L(n, L=0):
'''返回按阈值分割后新集合和之差绝对值, 如果输入集合中有两个阈值元素,以先出来的分割。'''
tem, tem2 = [], [] # 初始化新集合。
flag = False # 分割标识初值为假。
for i in n:
if i == L:
flag = True # 遇到分割阈值,分割标识置真。
print(f"\n分割阈值:{
L}")
L = '' # 置空字符,避免不收集第二个出现的分割阈值。
continue # 结束本次循环,就是为了不收集分割阈值。
if flag is True:
tem2.append(i)
else:
tem.append(i)
print(f"\n分割集合:\n\n{
tem},{
tem2}")
return abs(sum(tem) - sum(tem2))
db_L = difference_between_L # 函数取别名。
n = 2, 5, 2, 6, 0, 2, 7, 6, 0, 17, 3
print(cut_line())
print(f"{
cut_line()}{
'':>4}输入:{
n}\
\n\n{
'':>16}输出:{
color(db_L(n, 7), 'f_green')}{
cut_line()}")
wait()
运行效果
完整 Python 代码
我的解题思路,已融入代码注释,博文中就不再赘述。
(如果从语句注释不能清楚作用,请评论区留言指教和探讨。)
#!/sur/bin/env python
# coding: utf-8
''' filename: /sdcard/qpython/tem.py 梦幻精灵_cq的炼码场 '''
from mypythontools import color, wait, cut_line # 从自码工具模块加载需用代码模块中要使用的函数。
def sum_of_evens(*evens):
'''求输入向量中偶数和'''
tem_lis = []
for i in evens:
if str(i).isdigit() is False:
tem_lis.extend(list(i)) # 集群收集。
else:
tem_lis.append(i) # 单参收集。
tem = []
for i in tem_lis:
if i % 2 == 0: # 摘除奇数。
tem.append(i)
return sum(tem) # 返回和值。
print(cut_line(), color(1, 'f_green')) # 打印分割线。
print(f"{
'':>8}直接传参,输出:{
sum_of_evens(1, 3, 6, 9)}") # 直接传参。
s = sum_of_evens # 函数取别名。
print(f"\n\n{
'':>8}list 集群传参,输出:{
s([1, 9, 8, 10, 13])}") # 集群传参。
print(f"\n\n{
'':>8}tuple 传参,输出:{
s((1, 9, 8, 5, 12, 10, 13))}") # 集群传参。
print(f"\n\n{
'':>8}list 混传,输出:{
s(1, 2, [1, 8, 10, 13], 3, 8)}") # 单个、集群混传。
print(f"\n\n{
'':>8}tuple 混传,输出:{
s(1, 2, (1, 8, 13), 3, 8)}") # 单个、集群混传。
print(f"{
color(0)}{
cut_line()}") # 打印分割线。
wait()
def difference_between_L(n, L=0):
'''返回按阈值分割后新集合和之差绝对值, 如果输入集合中有两个阈值元素,以先出来的分割。'''
tem, tem2 = [], [] # 初始化新集合。
flag = False # 分割标识初值为假。
for i in n:
if i == L:
flag = True # 遇到分割阈值,分割标识置真。
print(f"\n分割阈值:{
L}")
L = '' # 置空字符,避免不收集第二个出现的分割阈值。
continue # 结束本次循环,就是为了不收集分割阈值。
if flag is True:
tem2.append(i)
else:
tem.append(i)
print(f"\n分割集合:\n\n{
tem},{
tem2}")
return abs(sum(tem) - sum(tem2))
db_L = difference_between_L # 函数取别名。
n = 2, 5, 2, 6, 0, 2, 7, 6, 0, 17, 3
print(cut_line())
print(f"{
cut_line()}{
'':>4}输入:{
n}\
\n\n{
'':>16}输出:{
color(db_L(n, 7), 'f_green')}{
cut_line()}")
wait()
上一篇: 骚包的中文字符提取操作(字符串,不用 re ,用 in )
我的HOT博:
- 用 pandas 解一道小题(1913阅读)
- 可迭代对象和四个函数(1042阅读)
- “快乐数”判断(1201阅读)
- 罗马数字转换器(构造元素取模)(1894阅读)
- 罗马数字(转换器|罗生成器)(2529阅读)
- Hot:让QQ群昵称色变的代码(15718阅读)
- 斐波那契数列(递归| for )(3233阅读)
- 柱状图中最大矩形(1623阅读)
- 排序数组元素的重复起止(1215阅读)
- 电话拨号键盘字母组合(1277阅读)
- 密码强度检测器(1730阅读)
- 求列表平衡点(1787阅读)
- Hot: 字符串统计(3471阅读)
- Hot:尼姆游戏(聪明版首发)(3327阅读)尼姆游戏(优化版)(898阅读)
推荐条件
点阅破千
精品文章:
- 好文力荐:《python 完全自学教程》齐伟书稿免费连载
- OPP三大特性:封装中的property
- 通过内置对象理解python'
- 正则表达式
- python中“*”的作用
- Python 完全自学手册
- 海象运算符
- Python中的 `!=`与`is not`不同
- 学习编程的正确方法
来源:老齐教室
Python 入门指南【Python 3.6.3】
好文力荐:
CSDN实用技巧博文:
版权声明
本文为[梦幻精灵_cq]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_57158496/article/details/124318955
边栏推荐
- Use of five routing guards
- matlab如何绘制已知公式的曲线图,Excel怎么绘制函数曲线图像?
- Excel quickly and automatically fills the contents of a row on a blank cell
- Aiot industrial technology panoramic structure - Digital Architecture Design (8)
- Error in v-on handler: "typeerror: cannot read property 'resetfields' of undefined"
- Shell - introduction, variables, and basic syntax
- Solution of Navicat connecting Oracle library is not loaded
- [ES6] promise related (event loop, macro / micro task, promise, await / await)
- Using quartz under. Net core - calendar of [6] jobs and triggers
- Grpc gateway based on Ocelot
猜你喜欢
线性代数感悟之1
Shell script -- shell programming specification and variables
Scope and scope chain in JS
How to change input into text
Solution architect's small bag - 5 types of architecture diagrams
Using quartz under. Net core -- operation transfer parameters of [3] operation and trigger
ClickHouse-表引擎
Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]
ASP. Net core JWT certification
If you start from zero according to the frame
随机推荐
Aiot industrial technology panoramic structure - Digital Architecture Design (8)
Advantages and disadvantages of several note taking software
Lock锁
stm32入门开发板选野火还是正点原子呢?
El date picker limits the selection range from the current time to two months ago
1-2 characteristics of nodejs
El cascade and El select click elsewhere to make the drop-down box disappear
Understanding of RPC core concepts
Using quartz under. Net core - calendar of [6] jobs and triggers
VLAN advanced technology, VLAN aggregation, super VLAN, sub VLAN
MySQL modify master database
Using quartz under. Net core -- general properties and priority of triggers for [5] jobs and triggers
freeCodeCamp----prob_ Calculator exercise
[PROJECT] small hat takeout (8)
ASP. Net core reads the configuration file in the class library project
Clickhouse - data type
Generation of barcode and QR code
Deep understanding of control inversion and dependency injection
How does matlab draw the curve of known formula and how does excel draw the function curve image?
On lambda powertools typescript