【375】COMP 9021 相关笔记
1. Python 中的逻辑否定用 not
2. 对于下面的代码直邮输入整数才能运行,无论字符串或者浮点型都会报错
int(input('How many games should I simulate? '))
可以通过 try 来修改,同时注意 raise 的使用
while True:
try:
nb_of_games = int(input('How many games should I simulate? '))
if nb_of_games <= 0:
raise ValueError
print('Ok, will do!')
break
except ValueError:
print('Your input is incorrect, try again.')
3. set 与 dict 都是大括号
# Checking for membership in a list
'y' in ['yes', 'y', 'no', 'n']
'Y' in ['yes', 'y', 'no', 'n']
# Checking for membership in a set
'y' in {'yes', 'y', 'no', 'n'}
'Y' in {'yes', 'y', 'no', 'n'} '''
Curly braces are used for both literal dictionaries and literal sets.
There is no potential conflict, except for empty set versus empty dictionary;
{} denotes an empty dictionary, not an empty set:
''' # Singleton dictionary and set, respectively
type({'one': 1})
type({'one'})
# Empty dictionary and set, respectively
type({})
type(set())
4. random.choice() 可以随机选择列表里面的元素
random.randrange(),在 0 与 n 之间随机产生一个数
from random import choice
doors = ['A', 'B', 'C']
for i in range(12):
choice(doors)
5. list.pop() 默认删除最后一个,否则按照索引删除
6. format
To output information about the game as it is being played, it is convenient to use formatted strings; they are preceded with f
and can contain pairs of curly braces that surround expressions meant to be replaced with their values. Also, though strings can be explicitly concatenated with the +
operator, they can also be implicitly concatenated when they are separated with nothing but space characters, including possibly new lines:
x = 10
u = 4.5
v = 10
print(f'x is equal to {x}.'
' That is not all: '
f'{u} divided by {v} equals {u / v}.'
)
x = 123 / 321
f'{x}'
f'{x:.0f}'
f'{x:.1f}'
f'{x:.2f}'
f'{x:.3f}'
f'{x:.4f}'
f'{x:.30f}'
7. 神奇的 * 号,乘号,可以扩展字符串,可以扩展列表
[1, 2, 3]*3
"abc"*3
8. 输出格式,0补全
# A field width of 3 at least, padding with spaces if needed
f'{90:3}', f'{90:3b}', f'{90:3o}', f'{90:3x}', f'{90:3X}'
# A field width of 3 at least, padding with 0's if needed
f'{90:03}', f'{90:03b}', f'{90:03o}', f'{90:03x}', f'{90:03X}'
# A field width of 8 at least, padding with spaces if needed
f'{90:8}', f'{90:8b}', f'{90:8o}', f'{90:8x}', f'{90:8X}'
# A field width of 8 at least, padding with 0's if needed
f'{90:08}', f'{90:08b}', f'{90:08o}', f'{90:08x}', f'{90:08X}'
(' 90', '1011010', '132', ' 5a', ' 5A')
('090', '1011010', '132', '05a', '05A')
(' 90', ' 1011010', ' 132', ' 5a', ' 5A')
('00000090', '01011010', '00000132', '0000005a', '0000005A')
sorted
Let us still not "hardcode" the sequence of bits as (s[7], s[3], s[5], s[1], s[6], s[2], s[4], s[0])
, but generate it. Let us first examine the sorted()
function. By default, sorted()
returns the list of members of its arguments in their default order:
sorted([2, -2, 1, -1, 0])
# Lexicographic/lexical/dictionary/alphabetic order
sorted({'a', 'b', 'ab', 'bb', 'abc', 'C'})
sorted(((2, 1, 0), (0, 1, 2), (1, 2, 0), (1, 0, 2)))
[-2, -1, 0, 1, 2]
['C', 'a', 'ab', 'abc', 'b', 'bb']
[(0, 1, 2), (1, 0, 2), (1, 2, 0), (2, 1, 0)]
sorted()
accepts the reverse
keyword argument:
sorted([2, -2, 1, -1, 0], reverse = True)
sorted({'a', 'b', 'ab', 'bb', 'abc', 'C'}, reverse = True)
sorted(((2, 1, 0), (0, 1, 2), (1, 2, 0), (1, 0, 2)), reverse = True)
[2, 1, 0, -1, -2]
['bb', 'b', 'abc', 'ab', 'a', 'C']
[(2, 1, 0), (1, 2, 0), (1, 0, 2), (0, 1, 2)]
sorted()
also accepts the key
argument, which should evaluate to a callable, e.g., a function. The function is called on all elements of the sequence to sort, and elements are sorted in the natural order of the values returned by the function:
sorted([2, -2, 1, -1, 0], key = abs)
sorted({'a', 'b', 'ab', 'bb', 'abc', 'C'}, key = str.lower)
sorted({'a', 'b', 'ab', 'bb', 'abc', 'C'}, key = len)
[0, 1, -1, 2, -2]
['a', 'ab', 'abc', 'b', 'bb', 'C']
['C', 'b', 'a', 'ab', 'bb', 'abc']
We can also set key
to an own defined function: 按照自定义的顺序进行排序
def _2_0_1(s):
return s[2], s[0], s[1] def _2_1_0(s):
return s[2], s[1], s[0] sorted(((2, 1, 0), (0, 1, 2), (1, 2, 0), (1, 0, 2)), key = _2_0_1)
sorted(((2, 1, 0), (0, 1, 2), (1, 2, 0), (1, 0, 2)), key = _2_1_0)
[(1, 2, 0), (2, 1, 0), (0, 1, 2), (1, 0, 2)]
[(2, 1, 0), (1, 2, 0), (1, 0, 2), (0, 1, 2)]
So we could generate the sequence (0, 4, 2, 6, 1, 5, 3, 7) as follows:
def three_two_one(p):
return p % 2, p // 2 % 2, p % 4 for p in sorted(range(8), key = three_two_one):
p, f'{p:03b}'
(0, '000')
(4, '100')
(2, '010')
(6, '110')
(1, '001')
(5, '101')
(3, '011')
(7, '111')
lambda 表达式
There is a better way, using a lambda expression. Lambda expressions offer a concise way to define functions, that do not need to be named:
# Functions taking no argument, so returning a constant
f = lambda: 3; f()
(lambda: (1, 2, 3))()
3
(1, 2, 3)
# Functions taking one argument, the first of which is identity
f = lambda x: x; f(3)
(lambda x: 2 * x + 1)(3)
3
7
# Functions taking two arguments
f = lambda x, y: 2 * (x + y); f(3, 7)
(lambda x, y: x + y)([1, 2, 3], [4, 5, 6])
20
[1, 2, 3, 4, 5, 6]
【375】COMP 9021 相关笔记的更多相关文章
- 【376】COMP 9021 相关笔记(二)
Note_01 zip() itertools.zip_longest() %time Note_02 for 循环单行输出 list 技巧 迭代器 生成器 map() zip() from path ...
- HTTPS证书申请相关笔记
申请免费的HTTPS证书相关资料 参考资料: HTTPS 检测 苹果ATS检测 什么是ECC证书? 渠道2: Let's Encrypt 优点 缺点 Let's Encrypt 的是否支持非80,44 ...
- JNI相关笔记 [TOC]
JNI相关笔记 目录 JNI相关笔记 1 生成native code所需要的头文件 2 JNI提供的一些函数和方法 3 局部引用,全局引用,全局弱引用. 4 异常 1 生成native code所需要 ...
- Hadoop相关笔记
一. Zookeeper( 分布式协调服务框架 ) 1. Zookeeper概述和集群搭建: (1) Zookeeper概述: Zookeeper 是一个分布式 ...
- redis相关笔记(二.集群配置及使用)
redis笔记一 redis笔记二 redis笔记三 1.配置:在原redis-sentinel文件夹中添加{8337,8338,8339,8340}文件夹,且复制原8333中的配置 在上述8333配 ...
- redis相关笔记(三.redis设计与实现(笔记))
redis笔记一 redis笔记二 redis笔记三 1.数据结构 1.1.简单动态字符串: 其属性有int len:长度,int free:空闲长度,char[] bur:字符数组(内容) 获取字符 ...
- Windows API 进程相关笔记
0. 前言 最近做了一个进程信息相关的项目,整理了一下自己做项目时的笔记,分享给大家 1. 相关概念 1.1 HANDLE 概念 HANDLE(句柄)是Windows操作系统中的一个概念. 在Wind ...
- PHP相关笔记
扩展包(相关链接):https://packagist.org/: 插件postman主要应用于web开发时get.post请求时查看其响应:
- lua相关笔记
--[[ xpcall( 调用函数, 错误捕获函数 ); lua提供了xpcall来捕获异常 xpcall接受两个参数:调用函数.错误处理函数. 当错误发生时,Lua会在栈释放以前调用错误处理函数,因 ...
随机推荐
- CyclicBarrier循环屏障相关
简介 CyclicBarrier 的字面意思是可循环使用(Cyclic)的屏障(Barrier).它要做的事情是,让一组线程到达一个屏障(也可以叫同步点)时被阻塞,直到最后一个线程到达屏障时,屏障才会 ...
- Java基础知识_毕向东_Java基础视频教程笔记(26 反射)
Java反射机制: 是在运行状态中,对于任意一个类(class)文件,都能够知道这个类的所有属性和方法,对于任意一个对象,都能够调用它的任意一个方法和属性.这种动态获取的信息以及动态调用对象的方法的功 ...
- SQL按分隔符拆分字段串
CREATE VIEW [dbo].[Split_BusinessUnit] AS WITH tt AS ( SELECT BusinessUnit.BusinessUnitId , Business ...
- 01-配置java开发环境
JDK 1.8软件下载地址 (Oracle公司的官方网站) http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads ...
- javasript-for循环
先来个for循环的例子: var i=0,j=0; for(;i<10,j<6;i++,j++){ k=i+j; } console.log(k) 想知道会输出什么,首先得知道完整循环了多 ...
- 递归锁,event事件和信号量
锁通常被用来实现对共享资源的同步访问.为每一个共享资源创建一个Lock对象,当你需要访问该资源时,调用acquire方法来获取锁对象(如果其它线程已经获得了该锁,则当前线程需等待其被释放),待资源访问 ...
- 使用FPM打包工具打rpm包
使用FPM打包工具打rpm包 一:安装ruby环境和gem命令 fpm 是 ruby写的,因此系统环境需要ruby且版本必须大于1.8.5 # yum -y install ruby rubygems ...
- [SQL Server]无法创建 SSIS 运行时对象,请验证 DTS.dll 是否可用及是否已注册
很大可能是SQL Server Management Studio(SSMS)版本与当前操作系统不兼容造成的,与数据库本身没有关系,这种情况基本无解,不过可以使用其他机器连本机数据库导入导出数据. 今 ...
- Postman用法,了解一下
一.Postman的基础功能 二.接口请求流程 1. GET 请求 GET请求:点击Params,输入参数及value,可输入多个,即时显示在URL链接上, 所以,GET请求的请求头与请求参数如在接口 ...
- 检查Linux系统cpu--内存---磁盘的脚本
花了一天写了三条命令分别检查cpu,内存,磁盘 [root@localhost ~]# cat cpu_mem_disk.sh #!/bin/sh # echo "1 检查cpu利用率--- ...