python 函数/列表的应用
enumerate 函数用于遍历序列中的元素以及它们的下标:
>>> for i,j in enumerate(('a','b','c')):
print i,j
0 a
1 b
2 c
>>> for i,j in enumerate([1,2,3]):
print i,j
0 1
1 2
2 3
>>> for i,j in enumerate({'a':1,'b':2}):
print i,j
0 a
1 b
>>> for i,j in enumerate('abc'):
print i,j
0 a
1 b
2 c
集合、有序字典、可命名元祖、计数
#!/usr/bin/env python3.5
# -*-coding:utf8-*-
# s1 = set() # 集合 是一个无序且不重复的元素集合
# s1.add("alex")
# print("s1")
# s1.add("alex")
# print("s1")
# set() # 访问速度快,天生解决了重复问题 # 用于爬虫等s2 =
# s2 = set(["alex", "eric", "tony"])
#print(s2)
# s3 = s2.difference(["alex", "eric"]) # difference 没有改变原来的元数,生成了一个生的元素(集合)
# print(s3)
# # {'tony', 'alex', 'eric'}
# # {'tony'}
# s4 = s2.difference_update(["alex", "eric"]) # 不生成新的内容,而是改变原有集合的内容 ,删除当前SET中的所有包含在参数集合里的元素
# print(s2)
# print(s4)
# s5 = s2.intersection(["alex", "eric"]) # 新建一个取相同的部分
# print(s5)
# s6 = s2.intersection_update(["alex", "eric"]) # 更新原来的取相同部分
# print(s2) # b = {"1":"2","2":5}
# c = {"1":"2","2":6}
# c.update(b)
# print(b)
# print(c) # import collections
# obj = collections.Counter(["afad", "adfas"])
# print(obj) # 字典
# obj.update(["afad", "ad2adf"]) # 更新计数
# print(obj)
# obj.subtract(["afad", "adfa"]) # 删除计数
# print(obj)
# ret = obj.most_common(4)
# print(ret)
# for k in obj.elements():
# elements =>原生值
# print(k) # 循环取出从多到少的各个元素
# for k, v in obj.items():
# print(k, v) # 取字典键与值
# import collections
# # 有序字典
# dic = collections.OrderedDict()
# dic['k1'] = 'v1'
# dic['k2'] = 'v2'
# dic['k3'] = 'v3'
# print(dic)
# dic.popitem() # 后进先出
# print(dic)
# dic.pop('k2')
# print(dic)
# help(collections.OrderedDict())
# 内部用列表来维护有序字典
# dic = {"k1": []}
# dic["k1"].append("alex")
# import collections
# dic = collections.defaultdict(list) # 设置字典默认值为空列表
# dic["k1"].append("alex")
# print(dic) # 可命名元祖 意思是 对元祖的元素命名 以后可以直接调名字
# import collections
# mytupleclass = collections.namedtuple('mytupleclass', ['x', 'y', 'z']) # 创建类等同于 defaultdict
# print(help(mytupleclass))
# obj = mytupleclass(11, 22, 33)
# print(obj.x)
# print(obj.y)
# print(obj.z)
# # 主要用于坐标 # 队列 分双向队列与单向队列
# import collections
# d = collections.deque() # 创建双向队列
# d.append("1")
# d.append("10")
# d.append("1")
# d.append("10")
# r = d.count("1")
# # print(r)
# d.extend(["yy", "uu", "ii"])
# d.appendleft("ad")
# d.appendleft("12")
# d.extendleft(["adf", "1111", "222"])
# print(d)
# #print(help(d)) # 单向队列FIFO
# 生产者消费者模型
# import queue
# q = queue.Queue() # 创建单向队列
# # print(help(q))
# q.put('123')
# q.put("678")
# print(q.qsize()) # 获取队列大小
# print(q.get()) # 获取队列值
课上作业:
#!/usr/bin/env python3.5
# -*-coding:utf8-*-
# 数据库中原有
old_dict = {
"#1":{ 'hostname': "c1", 'cpu_count': 2, 'mem_capicity': 80 },
"#2":{ 'hostname': "c1", 'cpu_count': 2, 'mem_capicity': 80 },
"#3":{ 'hostname': "c1", 'cpu_count': 2, 'mem_capicity': 80 },
}
# cmdb 新汇报的数据
new_dict = {
"#1":{ 'hostname': "c1", 'cpu_count': 2, 'mem_capicity': 800 },
"#3":{ 'hostname': "c1", 'cpu_count': 2, 'mem_capicity': 80 },
"#4":{ 'hostname': "c2", 'cpu_count': 2, 'mem_capicity': 80 },
}
#1、原来没有 ->〉新加的插入
#2、原来有 ->〉更新
#3、新的无,原来有 =>把原来的删除掉
#需求
#要更新的数据
#要删除的数据
#要添加的数据
#交集、差集
old = set(old_dict.keys()) # 将旧的字典创建集合
new = set(new_dict.keys()) # 将新的字典创建集合
# 交集:要更新的数据,即把原来的更新为最新的数据
# 差集:原来的数据删除,新的数据进行添加
# delete_set = old.difference(update_set) # 差集,创建新对象,获取旧的字典里存在的且不在交集里面的,这种方式是通过循环去比较
# add_set = new.difference(update_set) # 差集,创建新对象,获取新的字典里存在的且不在交集里面的
# 交集
update_set = old.intersection(new) # 取交集
delete_set = old.symmetric_difference(update_set) # 差集,创建新对象,获取旧的字典里存在的且不在交集里面的,对称差集
add_set = new.symmetric_difference(update_set) # 差集,创建新对象,获取新的字典里存在的且不在交集里面的,对称差集
# print(update_set) # 需要更新的
# print(delete_set) # 需要删除的
# print(add_set) # 需要增加的
# 更新
for i in update_set:
b = new_dict.get(i)
c = old_dict.get(i)
if b == c:
continue
else:
c.update(b) # 将对应旧字典的数据进行更新
# print(new_dict)
# print(old_dict)
# print(b)
# print(c)
# 删除
for i in delete_set:
old_dict.pop(i) # 删除旧的不存在的字典数据
# print(old_dict)
# 添加
for i in add_set:
old_dict.update(new_dict) # 将对应旧字典的数据进行更新 print("new_dict:\n %s" % new_dict)
print("old_dict:\n %s" % old_dict)
深浅拷贝:
#!/usr/bin/env python3.5
# -*-coding:utf8-*-
import copy
# copy.copy() # 浅拷贝 只拷贝一层
# copy.deepcopy() # 深拷贝 有多层就拷贝多少份
# = # 赋值
# a1 =12324
# a3 = copy.copy(a1)
# print(a1)
# print(a3)
# # 其它,元祖,列表,字典……
# n1 = {"k1": "12", "k2": "2212", 'k3': ["112", ["adfa", "1231234"]]}
# n2 = n1
# print(id(n1))
# print(id(n2))
# n3 = copy.copy(n1)
# n3 = copy.deepcopy(n1)
# print(id(n1))
# print(id(n3))
# print(id(n1["k3"]))
# print(id(n3["k3"]))
# print(id(n1["k3"][1]))
# print(id(n3["k3"][1]))
# print(n1["k3"][1])
# 监控模板 深浅拷贝
#
# dic = {
# "cpu": [80],
# "mem": [80],
# "disk": [80]
# }
# print("brefor:",dic)
# new_dic = copy.deepcopy(dic)
# new_dic['cpu'][0] = 50
# print(new_dic)
python 函数/列表的应用的更多相关文章
- Python函数——列表推导式、生成器与迭代器
列表推导式 产生背景 现在有个需求,看列表[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],要求你把列表里的每个值加1,你怎么实现? 第一种方法: a = [1,3,4,6,7,7,8,9 ...
- python函数 | 列表生成式
在编写程序或者查看别人的程序时,经常会遇到列表生成式,这个使用起来并不复杂,但是非常有用,使我们的代码更加简洁灵活.很多python使用者并不太会使用它.今天,就给大家详细讲解列表生成式和生成器表达式 ...
- python函数中把列表(list)当参数时的"入坑"与"出坑"
在Python函数中,传递的参数如果默认有一个为 列表(list),那么就要注意了,此处有坑!! 入坑 def f(x,li=[]): for i in range(x): li.append(i*i ...
- python函数2(返回值、传递列表...)
python函数2(返回值.传递列表...) 1.返回值 1.1.返回简单的值 #返回简单值 def get_formatted_name(first_name,last_name): "& ...
- (python函数02)列表生成式
(python函数02)列表生成式 示例代码 num = [i for i in range(1, 10)] print(num) num = [i for i in range(1, 10) ...
- python中列表(list)函数及使用
序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推. Python有6个序列的内置类型,但最常见的是列表和元组. 序列 ...
- python 函数之day3
一 函数的语法及特性 什么是函数? 定义:函数是一个功能通过一组语句的集合,由名字(函数名)将其封装起来的代码块,要想执行这个函数,只要调用其函数名即可. 特性: 减少重复代码 使程序变的可扩展 使程 ...
- Python函数讲解
Python函数
- Python list列表的排序
当我们从数据库中获取一写数据后,一般对于列表的排序是经常会遇到的问题,今天总结一下python对于列表list排序的常用方法: 第一种:内建函数sort() 这个应该是我们使用最多的也是最简单的排序函 ...
随机推荐
- 如何让有物理键的手机在ActionBar始终显示更多菜单menu键
仅作记录代码用,功能未能测试成功,在低版本上不存在 sHasPermanentMenuKey 属性,会出现 java.lang.NoSuchFieldException: sHasPermanentM ...
- 使用高通SDK开发AR应用
具体AR是什么效果我这里就不说了,直接上过程: 1.去官网注册一个帐号https://developer.vuforia.com 2.下载SDK for Unity,并导入Unity 3.点击Deve ...
- [河南省ACM省赛-第三届] 网络的可靠性 (nyoj 170)
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=170 根据题意,需要找到度数为1的结点个数,如下图: #include<iostre ...
- [ An Ac a Day ^_^ ] [kuangbin带你飞]专题十二 HDU 1176 免费馅饼
题意: 中文题意不解释…… 思路: 先把x,T存到矩阵里 然后像数塔一样从最底层走一边就行了 dp[i][j]代表在时间为j时 第i个位置最多能吃到多少个馅饼 最后输出第0时刻的5位置的馅饼数量就好了 ...
- Super Jumping! Jumping! Jumping!杭电1087
Description Problem Description Nowadays, a kind of chess game called “Super Jumping! Jumping! Jumpi ...
- java中equals方法和contentEquals方法区别
java中,String类里提供了两种字符串的比较方式(算上“==”应该是三种) String line1 = new String("0123456789"); String l ...
- [Centos] mod_wsgi 安装流程以及遇到问题解决办法。apxs: command not found 或 Sorry, Python developer package does not appear to be installed.
前提: Centos 系统, apache 已安装, python 已安装. 1. 首先下载mod_wsgi-3.5.tar.gz 下载地址:https://code.google.com/p/mod ...
- Zsh安装
Zsh 使用 Homebrew 完成 zsh 和 zsh completions 的安装 brew install zsh zsh-completions 安装 oh-my-zsh 让 zsh 获得拓 ...
- The Key to final data
// FinalData.java - (insert one line description here) package com.hp.ci.mgmt.perm.hal.localization; ...
- maven打包配置
maven打包配置,到底要打包哪些文件,如何配置?? <build> <finalName>weatherAdminSys</finalName> <plug ...