SoFunction
Updated on 2024-11-18

Python implementation of nested lists and dictionaries and de-duplication by an element example of functionality

This article example describes the Python implementation of nested lists and dictionaries and de-duplication function by an element. Shared for your reference, as follows:

#! /usr/bin/env python
#coding=utf-8
class HostScheduler(object):
  def __init__(self, resource_list):
    self.resource_list = resource_list
  def MergeHost(self):
    allResource=[]
    (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):
          (dict)
    taskhost=[]
    for item in allResource:
      (item['host'])
    return taskhost
# The function implements a nested list, de-duplicating by an element.
def deleteRepeat():
  #1, Nested lists in lists. Realize de-duplication by element 'b'
  l1=[['b',1],['b',2],['c',3],['a',1],['b',1],['b',1],]
  l2=[]
  (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):
        (data)
  print "l2: ",l2
  #2. Nested dictionaries in lists. De-duplication by key-valuehost implementation
  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=[]
  (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):
        (dict)
  print "l4: ",l4
if __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=()
  print 'My test results: '
  print 'taskhost: '
  print taskhost

Run results:

PS: There are two relatively simple and practical online text to repeat the tool, recommended for everyone to use:

Online duplicate item removal tool:
http://tools./code/quchong

Online text de-duplication tool:
http://tools./aideddesign/txt_quchong

More about Python related content can be viewed on this site's topic: theSummary of Python dictionary manipulation techniques》、《Summary of Python string manipulation techniques》、《Summary of common Python traversal techniques》、《Python Data Structures and Algorithms Tutorial》、《Summary of Python function usage tipsand thePython introductory and advanced classic tutorials

I hope that what I have said in this article will help you in Python programming.