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

首頁 > 編程 > Python > 正文

Python實現(xiàn)嵌套列表及字典并按某一元素去重復(fù)功能示例

2020-01-04 16:13:18
字體:
供稿:網(wǎng)友

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

#! /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#該函數(shù)實現(xiàn)嵌套列表中,按某一元素去重復(fù)def deleteRepeat():  #1、列表中嵌套列表。按元素‘b'實現(xiàn)去重復(fù)  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實現(xiàn)去重復(fù)  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 'VEVB武林網(wǎng)測試結(jié)果: '  print 'taskhost: '  print taskhost

運行結(jié)果:

Python,嵌套,列表,字典,去重復(fù)

 

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


注:相關(guān)教程知識閱讀請移步到python教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 晋州市| 安化县| 裕民县| 绥化市| 崇左市| 西昌市| 勃利县| 高唐县| 商水县| 永康市| 偃师市| 五寨县| 孟连| 库车县| 泰来县| 齐齐哈尔市| 乌兰浩特市| 临海市| 瓮安县| 靖边县| 津市市| 肇东市| 金平| 哈尔滨市| 横峰县| 湖北省| 内乡县| 塔城市| 双牌县| 黄石市| 岱山县| 淮安市| 应用必备| 天峻县| 海口市| 文成县| 丰台区| 陇西县| 长治县| 麟游县| 界首市|