python实现嵌套列表、字典按某一元素去重复
来源:互联网 发布:放置江湖修改数据 编辑:程序博客网 时间:2024/05/29 16:06
#! /usr/bin/env python#coding=utf-8class HostScheduler(object): def __init__(self, resource_list): self.resource_list = resource_list def MergeHost(self): allResource=[] allResource.append(self.resource_list[0]) for dict in self.resource_list: #print len(l4) k=0 for item in allResource: #print 'item' if dict['host'] != item['host']: k=k+1 #continue else: break if k == len(allResource): allResource.append(dict) taskhost=[] for item in allResource: taskhost.append(item['host']) return taskhost#该函数实现嵌套列表中,按某一元素去重复def deleteRepeat(): #1、列表中嵌套列表。按元素‘b’实现去重复 l1=[['b',1],['b',2],['c',3],['a',1],['b',1],['b',1],] l2=[] l2.append(l1[0]) for data in l1: #print len(l2) k=0 for item in l2: #print 'item' if data[0] != item[0]: k=k+1 else: break if k == len(l2): l2.append(data) print "l2: ",l2 #2、列表中嵌套字典。按键值host实现去重复 l3=[{'host':'compute21', 'cpu':2},{'host':'compute21', 'cpu':2},{'host':'compute22', 'cpu':2}, {'host':'compute23', 'cpu':2},{'host':'compute22', 'cpu':2},{'host':'compute23', 'cpu':2}, {'host':'compute24', 'cpu':2}] l4=[] l4.append(l3[0]) for dict in l3: #print len(l4) k=0 for item in l4: #print 'item' if dict['host'] != item['host']: k=k+1 #continue else: break if k == len(l4): l4.append(dict) print "l4: ",l4if __name__ == '__main__': #deleteRepeat() resource_list=[{'host':'compute21', 'cpu':2},{'host':'compute21', 'cpu':2},{'host':'compute22', 'cpu':2}, {'host':'compute23', 'cpu':2},{'host':'compute22', 'cpu':2},{'host':'compute23', 'cpu':2}, {'host':'compute24', 'cpu':2}] hostSchedule=HostScheduler(resource_list) taskhost=hostSchedule.MergeHost() print 'taskhost: ' print taskhost
结果:
l2: [['b', 1], ['c', 3], ['a', 1]]l4: [{'host': 'compute21', 'cpu': 2}, {'host': 'compute22', 'cpu': 2}, {'host': 'compute23', 'cpu': 2}, {'host': 'compute24', 'cpu': 2}]taskhost: ['compute21', 'compute22', 'compute23', 'compute24']
0 0
- python实现嵌套列表、字典按某一元素去重复
- Python实现对嵌套列表字典按某一元素合并及lambda的使用
- python 中字典嵌套列表
- Python 函数字典列表嵌套
- Python列表元素重复
- python列表元素去重
- python列表去除重复元素
- python四种方法实现去除列表中的重复元素
- Python 字典 列表 嵌套 复杂排序大全
- python字典和列表各种嵌套排序
- python list 去重重复元素问题
- python 列表,元素,集合,字典区别
- 实现Python列表字典循环
- Python 去除列表中重复的元素
- Python 去除列表中重复的元素
- [python]去除列表中重复元素
- python去除列表中重复的元素
- Python删除列表中重复元素
- Bag of Words模型
- 程序员关于提高工作效率
- ASCII、ANSI、MBCS、UNICODE字符集详解
- PowerBuilder
- 关于CSDN无法编辑自己简介的问题
- python实现嵌套列表、字典按某一元素去重复
- VisualC# winform窗体应用程序 语句加this有的怎么不加?
- Click事件在显示和隐藏中的BUG
- 自定义控件例如LinearLayout 的三种方法
- 【Android】 加解密算法 HMAC 的使用
- 第<3>章 Typeglob和符号表
- Android加载Gif图片的一般方法:Movie实现
- Zoho Books的库存功能介绍
- android基础:android页面添加滚动条