Python : Util 常用的数据处理方法

本文档主要记录了 python 中常用的一些数据处理方法。

根据多边形顶点列表遍历所有边

vertex_lst = [[0, 0], [1, 1], [1.5, 0.5], [1, 2], [0.6, 0.8], [0, 2], [0, 0]]
print("vertex_lst[:-1] : ", vertex_lst[:-1])
print("vertex_lst[1::] : ", vertex_lst[1::])
print("zip : ", list(zip(vertex_lst[:-1], vertex_lst[1::])), "\n")

for spoi, epoi in zip(vertex_lst[:-1], vertex_lst[1::]):
    print("起点:", spoi, "终点:", epoi)

输出结果:

/home/eln/anaconda3/envs/eln35/bin/python3.5 /home/eln/PycharmProjects/SphinxDoc/_test/test3.py
vertex_lst[:-1] :  [[0, 0], [1, 1], [1.5, 0.5], [1, 2], [0.6, 0.8], [0, 2]]
vertex_lst[1::] :  [[1, 1], [1.5, 0.5], [1, 2], [0.6, 0.8], [0, 2], [0, 0]]
zip :  [([0, 0], [1, 1]), ([1, 1], [1.5, 0.5]), ([1.5, 0.5], [1, 2]), ([1, 2], [0.6, 0.8]), ([0.6, 0.8], [0, 2]), ([0, 2], [0, 0])] 

起点: [0, 0] 终点: [1, 1]
起点: [1, 1] 终点: [1.5, 0.5]
起点: [1.5, 0.5] 终点: [1, 2]
起点: [1, 2] 终点: [0.6, 0.8]
起点: [0.6, 0.8] 终点: [0, 2]
起点: [0, 2] 终点: [0, 0]

Process finished with exit code 0

二维数组下标互换

test = [[1, 5, 3], [5, 4, 7], [6, 3, 7]]
print(list(zip(*test)))

输出结果:

/home/eln/anaconda3/envs/eln35/bin/python3.5 /home/eln/PycharmProjects/SphinxDoc/_test/test3.py
[(1, 5, 6), (5, 4, 3), (3, 7, 7)]

Process finished with exit code 0

将两层列表展开平铺成一层

这里介绍多种方法实现:使用 Python 写出优雅的让列表中的列表展开,变成扁平化的列表。并进行性能对比。例如:

# 期望输入
input = [[['A', 1], ['B', 2]], [['C', 3], ['D', 4]]]

# 期望输出
output = [['A', 1], ['B', 2], ['C', 3], ['D', 4]]

sum 函数合并

这个看上去很简洁,不过有类似字符串累加的性能陷阱。后面有性能对比。

input = [[['A', 1], ['B', 2]], [['C', 3], ['D', 4]]]
new = sum(input, [])
print(input)
print(new)

输出结果:

/home/eln/anaconda3/envs/eln35/bin/python3.5 /home/eln/PycharmProjects/SphinxDoc/_test/test2.py
[[['A', 1], ['B', 2]], [['C', 3], ['D', 4]]]
[['A', 1], ['B', 2], ['C', 3], ['D', 4]]

Process finished with exit code 0

reduce 函数

做序列的累加操作。也是有累加的性能陷阱。

from functools import reduce

input = [[['A', 1], ['B', 2]], [['C', 3], ['D', 4]]]
new = reduce(list.__add__, input)
print(input)
print(new)

输出结果:

/home/eln/anaconda3/envs/eln35/bin/python3.5 /home/eln/PycharmProjects/SphinxDoc/_test/test2.py
[[['A', 1], ['B', 2]], [['C', 3], ['D', 4]]]
[['A', 1], ['B', 2], ['C', 3], ['D', 4]]

Process finished with exit code 0

列表推导式

列表推导式,看着有些长,而且还要 for 循环两次,变成一行理解需要费劲一些,没有那么直观。

input = [[['A', 1], ['B', 2]], [['C', 3], ['D', 4]]]
new = [item for sublist in input for item in sublist]
print(input)
print(new)

输出结果:

/home/eln/anaconda3/envs/eln35/bin/python3.5 /home/eln/PycharmProjects/SphinxDoc/_test/test2.py
[[['A', 1], ['B', 2]], [['C', 3], ['D', 4]]]
[['A', 1], ['B', 2], ['C', 3], ['D', 4]]

Process finished with exit code 0

itertools 类库

通过第三方类库类实现的,相比其他的几个实现,看着还算比较优雅。

import itertools

input = [[['A', 1], ['B', 2]], [['C', 3], ['D', 4]]]
new = list(itertools.chain(*input))
print(input)
print(new)

输出结果:

/home/eln/anaconda3/envs/eln35/bin/python3.5 /home/eln/PycharmProjects/SphinxDoc/_test/test2.py
[[['A', 1], ['B', 2]], [['C', 3], ['D', 4]]]
[['A', 1], ['B', 2], ['C', 3], ['D', 4]]

Process finished with exit code 0

性能大对比

(eln35) [eln@localhost waitDoc]$ python -mtimeit -s'l = [[1, 2, 3], [4, 5, 6], [7], [8, 9]] * 99; from functools import reduce' 'reduce(list.__add__,l)'
1000 loops, best of 3: 287 usec per loop

(eln35) [eln@localhost waitDoc]$ python -mtimeit -s'l = [[1, 2, 3], [4, 5, 6], [7], [8, 9]] * 99' 'sum(l, [])'
1000 loops, best of 3: 261 usec per loop

(eln35) [eln@localhost waitDoc]$ python -mtimeit -s'l = [[1, 2, 3], [4, 5, 6], [7], [8, 9]] * 99' '[item for sublist in l for item in sublist]'
10000 loops, best of 3: 27 usec per loop

(eln35) [eln@localhost waitDoc]$ python -mtimeit -s'l = [[1, 2, 3], [4, 5, 6], [7], [8, 9]] * 99; import itertools;' 'list(itertools.chain(*l))'
100000 loops, best of 3: 15.7 usec per loop

将一组数分成每 N 个一组

a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
step = 3
b = [a[i:i+step] for i in range(0, len(a), step)]
print(b)

输出结果:

/home/eln/anaconda3/envs/eln35/bin/python3.5 /home/eln/PycharmProjects/SphinxDoc/_test/test2.py
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11]]

Process finished with exit code 0

多维数组去重

思路:先把列表中每个元素转化为 tuple ,因为 list 不可哈希但是 tuple 可哈希;然后使用 set 去重。

import numpy as np

list1 = [[1, 2], [3, 4], [5, 6], [3, 4], [7, 8], [1, 2]]
list_unique = list(set([tuple(t) for t in list1]))
print(list1)
print(list_unique)
print(np.array(list_unique).tolist())

输出结果:

/home/eln/anaconda3/envs/eln35/bin/python3.5 /home/eln/PycharmProjects/SphinxDoc/_test/test2.py
[[1, 2], [3, 4], [5, 6], [3, 4], [7, 8], [1, 2]]
[(1, 2), (3, 4), (5, 6), (7, 8)]
[[1, 2], [3, 4], [5, 6], [7, 8]]

Process finished with exit code 0

一维数组的交集、并集、差集

思路:使用列表解析式,列表解析式一般来说比循环更快;将 list 转成 set 以后,使用 set 的各种方法去处理。

listA = [1, 2, 3, 4, 5]
listB = [3, 4, 5, 6, 7]

# 求交集的两种方式
retA = [i for i in listA if i in listB]
retB = list(set(listA).intersection(set(listB)))
print("\n求交集的两种方式:")
print("retA is: ", retA)
print("retB is: ", retB)

# 求并集
retC = list(set(listA).union(set(listB)))
print("\n求并集:")
print("retC is: ", retC)

# 求差集,在 B 中但不在 A 中的两种方式
retD = list(set(listB).difference(set(listA)))
retE = [i for i in listB if i not in listA]
print("\n求差集,在 B 中但不在 A 中的两种方式:")
print("retD is: ", retD)
print("retE is: ", retE)

输出结果:

/home/eln/anaconda3/envs/eln35/bin/python3.5 /home/eln/PycharmProjects/SphinxDoc/_test/test2.py

求交集的两种方式:
retA is:  [3, 4, 5]
retB is:  [3, 4, 5]

求并集:
retC is:  [1, 2, 3, 4, 5, 6, 7]

求差集,在 B 中但不在 A 中的两种方式:
retD is:  [6, 7]
retE is:  [6, 7]

Process finished with exit code 0

二维数组的差集

import numpy as np

a1 = np.asarray([[1, 2, 3], [3, 4, 5], [4, 5, 6]])
a2 = np.asarray([[1, 2, 3], [3, 2, 5]])

a1_rows = a1.view([('', a1.dtype)] * a1.shape[1])
a2_rows = a2.view([('', a2.dtype)] * a2.shape[1])

diff1 = np.setdiff1d(a1_rows, a2_rows).view(a1.dtype).reshape(-1, a1.shape[1])
diff2 = np.setdiff1d(a2_rows, a1_rows).view(a2.dtype).reshape(-1, a2.shape[1])
diff = np.vstack((diff1, diff2))

print("a1 : ")
print(a1)
print("a2 : ")
print(a2)

print("\na1_rows : ")
print(a1_rows)
print("a2_rows : ")
print(a2_rows)

print("\ndiff1 : ")
print(diff1)
print("diff2 : ")
print(diff2)

print("\ndiff : ")
print(diff)

输出结果:

/home/eln/anaconda3/envs/eln35/bin/python3.5 /home/eln/PycharmProjects/SphinxDoc/_test/test2.py
a1 :
[[1 2 3]
 [3 4 5]
 [4 5 6]]
a2 :
[[1 2 3]
 [3 2 5]]

a1_rows :
[[(1, 2, 3)]
 [(3, 4, 5)]
 [(4, 5, 6)]]
a2_rows :
[[(1, 2, 3)]
 [(3, 2, 5)]]

diff1 :
[[3 4 5]
 [4 5 6]]
diff2 :
[[3 2 5]]

diff :
[[3 4 5]
 [4 5 6]
 [3 2 5]]

Process finished with exit code 0

dict 中将 key 相同的字典合并在一个对象里

    # dict 中将 key 相同的字典合并在一个对象里
    tmp = {'a': [5], 'b': [56], 'c': [6]}
    objs = {'a': '15', 'b': '156', 'd': '456'}
    for k, v in objs.items():
        tmp.setdefault(k, []).append(v)
    print(tmp)  # {'a': [5, '15'], 'b': [56, '156'], 'c': [6], 'd': ['456']}
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
智慧校园整体解决方案是响应国家教育信息化政策,结合教育改革和技术创新的产物。该方案以物联网、大数据、人工智能和移动互联技术为基础,旨在打造一个安全、高效、互动且环保的教育环境。方案强调从数字化校园向智慧校园的转变,通过自动数据采集、智能分析和按需服务,实现校园业务的智能化管理。 方案的总体设计原则包括应用至上、分层设计和互联互通,确保系统能够满足不同用户角色的需求,并实现数据和资源的整合与共享。框架设计涵盖了校园安全、管理、教学、环境等多个方面,构建了一个全面的校园应用生态系统。这包括智慧安全系统、校园身份识别、智能排课及选课系统、智慧学习系统、精品录播教室方案等,以支持个性化学习和教学评估。 建设内容突出了智慧安全和智慧管理的重要性。智慧安全管理通过分布式录播系统和紧急预案一键启动功能,增强校园安全预警和事件响应能力。智慧管理系统则利用物联网技术,实现人员和设备的智能管理,提高校园运营效率。 智慧教学部分,方案提供了智慧学习系统和精品录播教室方案,支持专业级学习硬件和智能化网络管理,促进个性化学习和教学资源的高效利用。同时,教学质量评估心和资源应用平台的建设,旨在提升教学评估的科学性和教育资源的共享性。 智慧环境建设则侧重于基于物联网的设备管理,通过智慧教室管理系统实现教室环境的智能控制和能效管理,打造绿色、节能的校园环境。电子班牌和校园信息发布系统的建设,将作为智慧校园的核心和入口,提供教务、一卡通、图书馆等系统的集成信息。 总体而言,智慧校园整体解决方案通过集成先进技术,不仅提升了校园的信息化水平,而且优化了教学和管理流程,为学生、教师和家长提供了更加便捷、个性化的教育体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值