一、数据筛选:

处理方式:

1、filter函数在py3,返回的是个生成式。

from random import randint

data = [randint(-100,100) for i in range(10)]
data2 = [34, -59, -13, 96, -78, 38, 89, -96, -79, 98] info = filter(lambda x:x>0,data2)
for i in info:
print(i)

2、列表解析

from random import randint

data = [randint(-100,100) for i in range(10)]
data2 = [34, -59, -13, 96, -78, 38, 89, -96, -79, 98] info = [i for i in data2 if i >0]
print(info)

filter与列表解析的比较:

在py2使用列表生成式效率高,在py3使用filter过滤器会效率高

from random import randint
import timeit
import pprint
data = [randint(-100,100) for i in range(10)]
data2 = [34, -59, -13, 96, -78, 38, 89, -96, -79, 98] t1 = timeit.Timer('[i for i in [34, -59, -13, 96, -78, 38, 89, -96, -79, 98] if i >0]')
t2 = timeit.Timer('filter(lambda x:x>0,[34, -59, -13, 96, -78, 38, 89, -96, -79, 98])')
# t2 =
print(t1.timeit())
print(t2.timeit()) 结果:
1.9095332647026366
0.6967581773661176

3、字典解析:

使用字典生成式来筛选数据

from random import randint
info = {x:randint(10,100) for x in range(1,11)}
print(info)
info2 = {key:value for key,value in info.items() if value >60}

4、集合数据筛选:

结构看起来和字典生成式差不多

data = [34, -59, -13, 96, -78, 38, 89, -96, -79, 98]
data2 = set(data)
print(data2)
info = {x for x in data2 if x>0}
print(info)

二、如何为元组中的每个元素命名,提高程序的可读性

1、给index指定数值常量,类似C里的枚举


name,work,play,address = range(4)
people = ("Tom",35,"Teacher","swimming","shenzhen") print(people[name],people[work],people[address])

2、使用标准库中collections.namedtuple替代内置tuple,自定义一个tuple子类,这种方式开销仅仅比普通元组高一些。

from collections import namedtuple
people2 = namedtuple('people2',(['name','age','work','play','address']))
info = people2("Tom",35,"Teacher","swimming","shenzhen")
print(info)
print(info.name,info.age,info.work,info.play,info.address) 结果:
people2(name='Tom', age=35, work='Teacher', play='swimming', address='shenzhen')
Tom 35 Teacher swimming shenzhen

3、如何统计出序列中元素出现的频度

1、使用fromkey方法初始化一个dict,然后通过for循环迭代统计次数。

# from random import randint
#
# data = [randint(0,10) for x in range(30)]
# print(data)
data2 = [3, 0, 9, 1, 4, 1, 5, 7, 4, 7, 7, 3, 10, 4, 0, 6, 9, 2, 2, 4, 1, 1, 7, 8, 2, 7, 3, 1, 4, 9]
dict1 = dict.fromkeys(data2,0)
print(dict1)
for x in data2:
dict1[x] += 1 print(dict1) 结果:

{0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0, 10: 0}
{0: 2, 1: 5, 2: 3, 3: 3, 4: 5, 5: 1, 6: 1, 7: 5, 8: 1, 9: 3, 10: 1}

2、使用collections.Counter对象

# from random import randint
#
# data = [randint(0,10) for x in range(30)]
# print(data)
data2 = [3, 0, 9, 1, 4, 1, 5, 7, 4, 7, 7, 3, 10, 4, 0, 6, 9, 2, 2, 4, 1, 1, 7, 8, 2, 7, 3, 1, 4, 9]
from collections import Counter
dict1 = Counter(data2)
print(dict1)
print(dict1[1],dict1[4],dict1[7])
print(dict1.most_common(3)) 结果:

Counter({1: 5, 4: 5, 7: 5, 2: 3, 3: 3, 9: 3, 0: 2, 5: 1, 6: 1, 8: 1, 10: 1})
5 5 5
[(1, 5), (4, 5), (7, 5)]

4、如何根据字典中value的大小,对字典的key进行排序

对于这种排序,一般选择用内置函数sorted,因为这些内置函数一般使用C编写,运算速度会快些

1、使用zip函数

from random import randint
dict1 = {x:randint(50,100) for x in 'abcdefg'}
print(dict1)
ret = sorted(zip(dict1.values(),dict1.keys()))
print(ret) 结果:
{'e': 77, 'd': 100, 'b': 51, 'c': 78, 'g': 55, 'f': 80, 'a': 87}
[(51, 'b'), (55, 'g'), (77, 'e'), (78, 'c'), (80, 'f'), (87, 'a'), (100, 'd')]

2、sorted函数默认对每个迭代对象的第一个元素进行排序,可以通过指定key参数(传入一个函数,sorted每次迭代时会把选择的元素传入key中,然后让我们决定使用哪个元素作为排序对象)来排序

from random import randint
dict1 = {x:randint(50,100) for x in 'abcdefg'}
print(dict1)
print(dict1.items())
ret = sorted(dict1.items(),key=lambda x:x[1])
print(ret) 结果:
{'a': 64, 'f': 51, 'd': 67, 'e': 73, 'c': 57, 'g': 100, 'b': 71}
dict_items([('a', 64), ('f', 51), ('d', 67), ('e', 73), ('c', 57), ('g', 100), ('b', 71)])
[('f', 51), ('c', 57), ('a', 64), ('d', 67), ('b', 71), ('e', 73), ('g', 100)]

1、如何在列表,字典,集合种根据条件筛选数据?2、如何为元组中的每个元素命名,提高程序的可读性3、如何统计出序列中元素出现的频度4、如何根据字典中value的大小,对字典的key进行排序的更多相关文章

  1. 如何为元组中的每个元素命名,提高程序可读性---Python数据结构与算法相关问题与解决技巧

    实际案例: 学生信息系统中,数据为固定格式:(名字,年龄,性别,邮箱) ,通常使用元组来存储 使用优点: 使用元组最大的优点在于节省空间,存储相同的数据,使用元组比使用字典,空间小很多 使用缺点: 访 ...

  2. Python 高效编程技巧实战(2-1)如何在列表,字典, 集合中根据条件筛选数据

    Python 高效编程技巧实战(2-1)如何在列表,字典, 集合中根据条件筛选数据 学习目标 1.学会使用 filter 借助 Lambda 表达式过滤列表.集合.元组中的元素: 2.学会使用列表解析 ...

  3. python基础一 -------如何在列表字典集合中根据条件筛选数据

    如何在列表字典集合中根据条件筛选数据 一:列表 先随机生成一个列表,过滤掉负数 1,普通for循环迭代判断 2,filter()函数判断,filter(函数,list|tuple|string) fi ...

  4. python数据结构-如何在列表、字典、集合中根据条件筛选数据

    如何在列表.字典.集合中根据条件筛选数据 问题举例: 过滤列表[1, 2, 5, -1, 9, 10]中的负数 筛选字典{“zhangsan”:97, "lisi":80, &qu ...

  5. python3编程技巧二——如何在列表、字典、集合 中根据条件筛选数据

    一.列表筛选数据 # coding=utf-8 from random import randint # 创建随机列表 l = [randint(-10, 10) for i in range(10) ...

  6. python 学习笔记(一):在列表、字典、集合中根据条件筛选数据

    一.在列表中筛选数据 在列表中筛选出大于等于零的数据,一般通用的用法代码如下: data = [3, -9, 0, 1, -6, 3, -2, 8, -6] #要筛选的原始数据列表 result = ...

  7. 如何在列表,字典,集合中,根据条件筛选数据 -- Python数据结构与算法相关问题与解决技巧

    实际案例: 1.过滤掉列表 [3,9,-1,10,20,-2..]的负数 2.筛出字典{'LiLei':79,'Jim':88,'Lucy':92...}中值高于90的项 3.筛出集合 {77,89, ...

  8. python基础===如何在列表,字典,集合中根据条件筛选数据

    #常见的操作如下: data = [1, 5, -3, -2, 6, 0, 9] res = [] for x in data: if x>=0: res.append(x) print(res ...

  9. Py小技巧一:在列表,字典,集合中根据条件筛选数据

    1.过滤掉列表中的某些项---列表解析 data=[1,4,2,8,5,-1] res=[] a.依次迭代列表中每一个项 for x in data: if >=0: res.append(x) ...

随机推荐

  1. Oracle备份统计信息

    Oracle可以通过DBMS_STATS.GET_TABLE_STATS 收集表的统计信息,一般的收集方法如下: DBMS_STATS.GATHER_TABLE_STATS(OWNNAME => ...

  2. ASE19 团队项目 alpha 阶段 Frontend 组 scrum7 记录

    本次会议于11月11日,11:30 在微软北京西二号楼13158,持续15分钟. 与会人员:Jingyi Xie, , Ziwei Wu, Jiaqi Xu, Jingwei Yi, Hanyue T ...

  3. redis Hash相关命令

  4. C#多线程的应用

    1.进程 就像我们任务管理器里面运行的进程 进程(Process)是Windows系统中的一个基本概念,它包含着一个运行程序所需要的资源.一个正在运行的应用程序在操作系统中被视为一个进程,进程可以包括 ...

  5. 【LeetCode】Hash

    [451] Sort Characters By Frequency [Medium] 给一个字符串,要求返回按照字母出现频率的排序后的字符串.(哈希表+桶排) 有个技巧是Hash用Value作为In ...

  6. JMeter 常用网站

    1.jmeter插件 https://blog.csdn.net/weixin_39430584/article/details/80947093 http://www.doc88.com/p-214 ...

  7. mongodb 可视化工具

    mongodb是用命令行输入的,有些人可能不太习惯,我自己找了下mongodb的一些可视化工具,发现了一款adminmongo很好用,这里介绍给你们用一下. github地址:https://gith ...

  8. 【JZOJ3674】【luoguP4042】【BZOJ3875】骑士游戏

    description 在这个游戏中,JYY一共有两种攻击方式,一种是普通攻击,一种是法术攻击.两种攻击方式都会消耗JYY一些体力.采用普通攻击进攻怪兽并不能把怪兽彻底杀死,怪兽的尸体可以变出其他一些 ...

  9. html中设置一个div可编辑文本

    <div contenteditable="true"></div> <!-- outline: none;设置获取焦点没有高亮边框 -->

  10. Service7

    在真机上,利用clone-vm7新建一台虚拟机,名字:PXE-Server     1.设置防火墙为trusted   2.当前及永久关闭SELinux   3.配置IP地址:192.168.4.16 ...