python积累

目录

 

1.  PIL的draw.rectangle

2. 二维数组的创立

3. 一些写法

4. 在线笔试多行输入

5. 字典根据key和value进行排序

 6. 正则表达式

 


 

1.  PIL的draw.rectangle

可以指定画线的粗细

from PIL import Image, ImageDraw, ImageFont

def draw_rectangle(draw, coordinates, color, width=1):
    for i in range(width):
        rect_start = (coordinates[0][0] - i, coordinates[0][1] - i)
        rect_end = (coordinates[1][0] + i, coordinates[1][1] + i)
        draw.rectangle((rect_start, rect_end), outline = color)


outline_width = 10
outline_color = "blue"
draw_rectangle(draw, ((bbox['x'], bbox['y']), (bbox['x']+bbox['w'], bbox['y']+bbox['h'])), color=outline_color, width=outline_width)
draw.text((bbox['x'], bbox['y']), str(w), fill=(0, 0, 255), width= 10)

2. 二维数组的创立

python中是用List的list来创建二维数组,可以作为矩阵使用。 如果用 myList = [[0] * 3] * 4 这种方法创建,当操作myList[0][1] = 1时,发现整个第二列都被赋值,这是因为“浅拷贝

二维数组正确的创建方式:

a = [[0 for i in range(3)] for j in range(4)]

3. 一些写法

(1)交换A和B的值:

a, b = b, a

(2)用Num来更新min和max的值

max = [max, num][num > max]
min = [min, num][num < min]

(3)将一个含有两个元素的list分别赋值给两个变量

[c1, c2] = click # 或者
(d1, d2) = click

4. 在线笔试多行输入

# python 多行输入
import sys

res = []
s = sys.stdin.readline().strip("\n")
while s != "":
    s = s.split()
    s = [int(each) for each in s]
    res.append(s)
    s = sys.stdin.readline().strip("\n")

5. 字典根据key和value进行排序

现根据value来,再根据key来

a2 = sorted(d.items(),key = lambda x:(x[1], x[0]))

lambda函数有多项条件:偶数 -> 奇数 -> 小写 -> 大写

s="Hello79351WorldMyNameIsMrFiona0352231964"
c = ''.join(sorted(s, key=lambda x: (x.isdigit(),x.isdigit() and int(x)%2==0,x.islower(),x.isupper(),x)))

字典的key和value互换

 {v: k for k, v in m.items()}

 6. 正则表达式

匹配数字:有加号代表连在一起

s1 = re.findall('(\d+)', s) 

 匹配小写字母:有加号代表连在一起;没有加号代表单个字母

s1 = re.findall('([a-z]+)', s)

7. 变量的引用

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值