一、数据筛选:

处理方式:

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. DNS域名解析服务以及Bind服务程序

    一般来讲域名比IP地址更加的有含义.也更容易记住,所以通常用户更习惯输入域名来访问网络中的资源,但是计算机主机在互联网中只能通过IP识别对方主机,那么就需要DNS域名解析服务了. DNS域名解析服务( ...

  2. leetcode python翻转字符串里的单词

    # Leetcode 151 翻转字符串里的单词### 题目描述给定一个字符串,逐个翻转字符串中的每个单词. **示例1:** 输入: "the sky is blue" 输出: ...

  3. jmeter beanshell postprocessor 使用

    String newtoken=bsh.args[0];print(newtoken);${__setProperty(newtoken,${token},)}; String newcompanyI ...

  4. shell编程:定义函数

    第一种方式 function hello { echo "hello" } 第二种方式 hello() { echo "hello" } 调用函数 命令行:he ...

  5. 树莓派4B更换国内源

    更换清华源:https://mirrors.tuna.tsinghua.edu.cn/help/raspbian/ 注意树莓派4B的Respbian是基于Debian 10 Bluster 不要选错. ...

  6. hibernate3.6异常

    WARN DTDEntityResolver:73 - recognized obsolete hibernate namespace http://hibernate.sourceforge.net ...

  7. 小程序之rpx适配方案

    官网文档: 我的理解: rpx是自适应单位 计算方式: 1rpx = 设备屏幕宽度 / 750 注意:750是官网规定 为什么选择iPhone6为标准,作为开发模拟? 因为在iPhone6中,1px ...

  8. HIVE的Shell操作

    1.Hive支持的一些命令 退出使用quit或exit离开交互式外壳. set key = value使用它来设置特定配置变量的值. 这里要注意的一件事是,如果您对变量名拼写错误,cli将不会显示错误 ...

  9. obj.offsetHeight与obj.style.height $(obj).height()与$(obj).css('height')

    相同:都可以获取obj的高度区别:(1)obj.offsetHeight可以获取外部.内嵌和内联中定义的高,而obj.style.height只能获取内联中定义的高:(2)obj.offsetHeig ...

  10. Eclipse 安装Activiti插件

    建议使用vpn或其他翻墙手段安装(否则下载速度可能很慢) 我的博客中有介绍如何自己搭建属于自己的ssr,https://www.cnblogs.com/zktww/p/10839347.html(由于 ...