国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python實現嵌套列表及字典并按某一元素去重復功能示例

2020-02-16 10:54:30
字體:
來源:轉載
供稿:網友

本文實例講述了Python實現嵌套列表及字典并按某一元素去重復功能。分享給大家供大家參考,具體如下:

#! /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 '武林站長站測試結果: '  print 'taskhost: '  print taskhost

運行結果:

PS:本站還有兩款比較簡單實用的在線文本去重復工具,推薦給大家使用:

在線去除重復項工具:
http://tools.jb51.net/code/quchong

在線文本去重復工具:
http://tools.jb51.net/aideddesign/txt_quchong

更多關于Python相關內容可查看本站專題:《Python字典操作技巧匯總》、《Python字符串操作技巧匯總》、《Python常用遍歷技巧總結》、《Python數據結構與算法教程》、《Python函數使用技巧總結》及《Python入門與進階經典教程》

希望本文所述對大家Python程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 包头市| 镇赉县| 循化| 深水埗区| 那曲县| 大悟县| 靖宇县| 嘉定区| 景谷| 平凉市| 武夷山市| 万荣县| 莱州市| 临安市| 通河县| 纳雍县| 加查县| 海宁市| 琼海市| 利津县| 健康| 南汇区| 泌阳县| 弥勒县| 达拉特旗| 施秉县| 黄梅县| 甘孜县| 观塘区| 辽中县| 安达市| 东辽县| 江源县| 曲周县| 襄汾县| 静安区| 黎川县| 墨江| 安义县| 枣强县| 北辰区|