摘自:
http://m.blog.csdn.net/will130/article/details/50441937
t1 = [{'a':1}, {'a':2}, {'a':2}]
t2 = [{value:key} for key, value in dict([d.items()[0] for d in t1]).items()]
class 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:
for item in allResource:
if dict['host'] != item['host']:
k=k+1
else:
break
if k == len(allResource):
allResource.append(dict)
taskhost=[]
for item in allResource:
taskhost.append(item['host'])
return taskhost
def deleteRepeat():
l1=[['b',1],['b',2],['c',3],['a',1],['b',1],['b',1],]
l2=[]
l2.append(l1[0])
for data in l1:
for item in l2:
if data[0] != item[0]:
k=k+1
else:
break
if k == len(l2):
l2.append(data)
print "l2: ",l2
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:
for item in l4:
if dict['host'] != item['host']:
k=k+1
else:
break
if k == len(l4):
l4.append(dict)
print "l4: ",l4
if __name__ == '__main__':
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']
大学可以申请助学贷款,申请额度不超过学费和生活费总额的 60%,输入你每个月的生活费(浮点数),请计算你每个学期能够贷款多少元?根据提示,在右侧编辑器补充代码,计算并输出助学贷款的数量。本关要求在两个输入语句中分别加入提示性文字“请输入每学分学费金额:”和“请输入你每个月生活费:”输入每学分应缴纳的学费(整数,单位为元),计算并输出第一学期应缴纳多少学费?根据提示,在右侧编辑器补充代码,计算并输出数组的大学第一学期共修学分和应缴纳学费。3学分 4学分 4学分 2学分 2学分 2学分。
假设在
python
中有一
字典
如下:x={‘a':'1,2,3', ‘b':'2,3,4'}需要合并为:x={‘c':'1,2,3,4'}需要做到三件事:1. 将字符串转化为数值
列表
2. 合并两个
列表
并添加新的键值3. 去除重复元素第1步通过常用的函数eval()就可以做到了,第2步需要添加一个键值并添加元素,第3步利用set集合的性质可以达到
去重
的效果,不过最后需要再将set集合转化为list
列表
...
#! /usr/bin/env
python
#coding=utf-8class HostScheduler(object): def __init__(self, resource_list): self.resource_list = resource_list def MergeHost(self):
allResource=[]
今天我在爬虫汇总数据的时候,采用的数据结构为
列表
套
字典
如下:
这里有一个
列表
里面有三个元素类型都是
字典
,可以看到索引为0和2的元素
key
和
value
完全相同。
这是我想去掉其中一个就是一个简单的
字典
去重
方法,
列表
去重
很简单,直接采用set转为集合方法在转回list类型,我尝试了一下,我天真的以为此时的数据结构也可以这样操作。
错误提示说 不可哈希类型。
可哈希:int \ float \ str \ tuple
不可哈希: list \ set \ dict
列表
嵌套
字典
去重
:
data_l
new_dict = {}
for i in range(len(sequence)):
if not new_dict.__contains__(sequence[i][par_name]):
new_dict[sequence[i][par_name]] = True
注
python
内置函数sorted()函数返回新的
列表
,并不对原
列表
做任何修改。问题使用set()函数
去重
后,会自动排序,则原
列表
的顺序会发生改变。以上就是关于
列表
去重
的5种方法,可以根据需求选择对应的方法。结果[‘a’,‘b’,1,3,9]结果[‘a’,‘b’,1,3,9]结果[1,3,9,‘b’,‘a’]结果[‘a’,‘b’,1,3,9]结果[‘a’,‘b’,1,3,9]结果[‘a’,‘b’,1,3,9]第一种方法,使用sort()方法。...
工作中遇到以下小问题,解决方法如下,可能比较暴力,暂时留档,再进行优化。
要求:将
列表
中json的 ‘id’ 字段值相同的数据,根据 type的值,按照一定的优先级次序排列,
列表
中仅保留优先级最高的type。
测试用例:
list1 示例数据:
type优先级
列表
:[6, 4, 2, 5, 8, 3, 7, 1] (依次递减,6优先级最高,1优先级最低)
draw_data = [
{'geometry':{"coordinates":[121.87635833333333, 30.86
对
python
列表
里的
字典
元素
去重
方法详解如下所示:def list_dict_duplicate_removal():data_list = [{"a": "123", "b": "321"}, {"a": "123", "b": "321"}, {"b": "321", "a": "123"}]run_function = lambda x, y: x if y in x else x + [...