27634

creating dictionaries to list order of ranking

Question:

I have a list of people and who controls who but I need to combine them all and form several sentences to compute which person control a list of people.

The employee order comes from a txt file:

Answer1:

from collections import defaultdict controls = defaultdict(list) with open('data.txt') as file: for line in file: controller, controlled = line.strip().split(' controls ') controls[controller].append(controlled) print 'employee order:' for controller, controlled in controls.iteritems(): if len(controlled) > 1: conjoined = ', '.join(controlled[:-1]) conjoined = '{} and {}'.format(conjoined, controlled[-1]) else: conjoined = controlled[0] print '{} controls {}'.format(controller, conjoined)

Recommend

  • Symfony2 Form is always empty after submitting
  • Single Table Inheritance in Rails 4
  • Adding Existing Group to Custom Ribbon in Office Word 2010
  • Can I store a Web UserControl ascx and code behind in database and load it from there?
  • MVC - Dynamically loading Partial Views
  • How to insert an Image in WORD after a bookmark using OpenXML
  • QObject::findChild() returns None without obvious reason
  • Sliding transition animation between Screens in Caliburn Micro
  • Why are views not counted if you embed a youtube iframe dynamically using jquery?
  • How do i find all references to a user control
  • TextBox AutoCompleteStringCollection Suggest
  • Express displaying mongodb documents in Jade
  • How to save dynamically created textboxes and their values
  • Unable to play media with vlc ocx
  • Setting WPF Window Background to Resource Dictionary Brush User Setting
  • Enumerating Controls on a Form
  • Configure Spring's MappingJacksonHttpMessageConverter
  • How can I tell a form not to dispose a particular control when it closes?
  • WPF version of .ScaleControl?
  • pip in virtualenv gets ConnectTimeoutError
  • Overlapping controls in Windows XP
  • Can't delete or rename original file after resizing
  • IE7 and TinyMCE with Plone
  • Run multiple queries from 1 SQL file showing result in multiple tables
  • Installed module is empty
  • Breeze - Deleted Items nav properties bug
  • Nant, Vault & Windows Integrated Authentication
  • javaw.exe and eclipse startup problems
  • Bug in WPF DataGrid
  • jQuery tmpl and DataLink beta
  • How can I estimate amount of memory left with calling System.gc()?
  • Why winpcap requires both .lib and .dll to run?
  • Return words with double consecutive letters
  • Python: how to group similar lists together in a list of lists?
  • AngularJs get employee from factory
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • coudnt use logback because of log4j
  • need help with bizarre java.net.HttpURLConnection behavior
  • Busy indicator not showing up in wpf window [duplicate]