Python implements the nested list and dictionary and repeats the function example by a certain element. python nesting
This example describes how to implement a nested list and dictionary in Python and repeat the function by a certain element. We will share this with you for your reference. The details are as follows:
#! /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 # In the nested list, def deleteRepeat (): #1. nesting list in the list is repeated by an element. Implement de-repeating 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. nested dictionaries in the list. Press the value host to repeat 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 'customer's house Test Result: 'print 'taskhost: 'print taskhost
Running result:
PS: There are two simple and practical online text deduplication tools on this site. We recommend this tool to you:
Online deduplication tools:
Http://tools.jb51.net/code/quchong
Online text deduplication tool:
Http://tools.jb51.net/aideddesign/txt_quchong