Python implements the nested list and dictionary and repeats the function example by a certain element. python nesting

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.