15.Pythonic与python杂记
switcher ={
:'sunday',
:'monday',
:'thuesday'
}
day =
day_name=switcher.get(day,'Unknow')
print(day_name) # sunday
def get_sunday():
return 'sunday' def get_monday():
return 'monday' def get_thuesday():
return 'thuesday' def get_default():
return 'Unknow' switcher ={
:get_sunday,
:get_monday,
:get_thuesday
} day =
day_name=switcher.get(day,get_default)()
print(day_name) # sunday
# 列表推到式
# 集合推到式
# map filter
# set
# dict
a = [,,,,,,,]
# b = [i*i for i in a]
b = [i** for i in a]
print(b) # [, , , , , , , ]
c = [i** for i in a]
print(c) # [, , , , , , , ]
d = [i** for i in a if i > ]
print(d) # [, , ] e =(,,,,,,,)
f = [i** for i in a if i > ]
print(f) # [, , ]
g = {i** for i in a if i > }
print(g) # {, , }
students = {
'喜小乐':,
'石敢当':,
'张三':
}
# 字典
b = {key for key,value in students.items()}
print(b) # {'石敢当', '喜小乐', '张三'}
b = {value for key,value in students.items()}
print(b) # {, , }
b = {value:key for key,value in students.items()}
print(b) # {: '喜小乐', : '石敢当', : '张三'}
# 元组
b = (key for key,value in students.items())
for x in b:
print(x)
# 喜小乐
# 石敢当
# 张三
# None 空
# 空字符串 空的列表 False
a = ''
b = False
c =[]
print(a==None) # False
print(b==None) # False
print(c==None) # False
print(a is None) # False
print(type(None)) # <class 'NoneType'> a = []
a =''
a = None # 不存在
a = False # 真假 判断为空的方式
if a:
if not a:
class Test():
def __len__(self):
return
# return
# return True test = Test()
if test:
print('S') # True/
else:
print('F') # print(len(Test())) #
print(bool(Test())) # False
class Test():
def __bool__(self):
return False def __len__(self):
return True test = Test()
if test:
print('S')
else:
print('F') # F
15.Pythonic与python杂记的更多相关文章
- Python(十二) Pythonic与Python杂记
一.导言 二.用字典映射代替switch case语句 # 字典代替 switch 语句 # switch () # { # case 0 : # dayName= 'a'; # break; # ...
- Python3(十二) Pythonic与Python杂记
一.用字典映射代替switch case语句 if/else可以代替switch但是非常不合适. 用字典代替switch: day = 5 switcher = { 0:'Sunday', 1:'Mo ...
- Be Pythonic ,Google Python Style Guide
为了更规范的写代码,变得更专业 分号 1 不在句末添加分号,不用分号在一行写两句代码 行长度 2 每行不超过80字符,python会隐式行连接圆括号,中括号,花括号中的字符,如多参数方法调用可以写为多 ...
- python杂记二
1. 写文件可以直接使用print函数 file_name = open("file_name.txt","w") print("file conta ...
- PAT 1031 查验身份证(15)(C++&Python)
1031 查验身份证(15)(15 分) 一个合法的身份证号码由17位地区.日期编号和顺序编号加1位校验码组成.校验码的计算规则如下: 首先对前17位数字加权求和,权重分配为:{7,9,10,5,8, ...
- python 杂记-unittest
介绍单元测试的好文:https://mp.weixin.qq.com/s/njxc8GXSlc3z_RibK70ROg setUpModule/tearDownModule:在整个模块的开始和结束时被 ...
- python杂记-4(迭代器&生成器)
#!/usr/bin/env python# -*- coding: utf-8 -*-#1.迭代器&生成器#生成器#正确的方法是使用for循环,因为generator也是可迭代对象:g = ...
- python杂记-3(购买商品)
#!/usr/bin/env python# -*- coding: utf-8 -*-#如下是一个购物程序:#先输入工资,显示商品列表,购买,quit退出,最后格式化输出所买的商品.count = ...
- python杂记-1(os模块)
os模块说明:python os模块包含普遍的操作系统功能 os.access(path, mode) # 检验权限模式 os.chdir(path) # 改变当前工作目录os.chflags(pat ...
随机推荐
- 第四周之Hadoop学习(四)
上周已经成功完成了Hadoop的学习,这周则是搭建好Hadoop的安卓编程环境 今天的学习根据这篇博客:https://blog.csdn.net/HcJsJqJSSM/article/details ...
- SpringBoot与Mybatis-plus整合,代码生成mvc层
一.添加pom依赖 <!-- mysql驱动 --> <dependency> <groupId>mysql</groupId> <artifac ...
- fiddler 保存请求数据并发送到自己的服务器接口
通过Rules菜单打开 Customize Rules 搜索 OnBeforeResponse 方法,再方法后面添加如下代码: if (oSession.fullUrl.Contains(" ...
- Pytorch本人疑问(2)model.train()和model.eval()的区别
我们在训练时如果使用了BN层和Dropout层,我们需要对model进行标识: model.train():在训练时使用BN层和Dropout层,对模型进行更改. model.eval():在评价时将 ...
- C++打开文件夹
https://bbs.csdn.net/topics/392055617?page=1 见2楼 system("start \"\" \"文件夹路径\&q ...
- C++面试常见问题——04链表的逆序与合并
链表的逆序与合并 链表的逆序 已知一个链表的头指针为head,将该链表逆序. #include<iostream> using namespace std; struct Node{ in ...
- windows下pycharm连接vagrant的python环境
- 别再写getter,setter方法了,用Lombok来简化你的代码吧
前言 在实际开发中,有些代码是重复的.IDE一键生成的,不写不行,写了又觉得代码太臃肿,不美观.如果你也有这种体会,那么,请使用Lombok插件吧,真的非常好用.Lombok的使用也非常简单,都是各种 ...
- pytest+allure(pytest-allure-adaptor基于这个插件)设计定制化报告
一:环境准备 1.python3.6 2.windows环境 3.pycharm 4.pytest-allure-adaptor 5.allure2.8.0 6.java1.8 pytest-allu ...
- hdu 2838 Cow Sorting 树状数组求所有比x小的数的个数
Cow Sorting Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...