0

I'm in pretty far over my head right now for Python scripting and don't really understand what I'm doing.

I have a dictionary where the keys are strings and the values are lists of strings. I need to sort the strings within the list alphanumerically such that

"role_powerdns": [
  "name-2.example.com",
  "name-1.example.com",
  "name-3.example.com"

 ],

Looks like this

"role_powerdns": [
  "name-1.example.com",
  "name-2.example.com",
  "name-3.example.com"
 ],

The full original code I'm working off of is here for reference. https://github.com/AutomationWithAnsible/ansible-dynamic-inventory-chef/blob/master/chef_inventory.py

I don't fully understand myself how the code I'm working off of works, I just know the data structure it's returning.

My local additions to that base code is filtering out IPs and inserting .sub into the strings. I've tried reusing the comprehension syntax I've got below for modifying the strings to sort the strings. Could someone show provide an example of how to iterate through a nested structure like this? Alternatively if doing this sort of sorting so late in the script is not appropriate, when generally should it be handled?

    def filterIP(fullList):
       regexIP = re.compile(r'\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}$')
       return filter(lambda i: not regexIP.search(i), fullList)

    groups = {key : [domain.replace('sub.example.com', 'example.com') for domain in filterIP(list(set(items)))] for (key, items) in groups.iteritems() }


    print(self.json_format_dict(groups, pretty=True))
Brando__
  • 365
  • 6
  • 24
  • 1
    Could you clarify *"not working"*? That code seems far more complex than just trying to sort the lists. – jonrsharpe Apr 17 '17 at 21:22
  • 1
    For your example, `sorted()` function seems like a good alternative, but your code below does sth more than what is described in the example, isn't it? – VeGABAU Apr 17 '17 at 21:24
  • 1
    Assuming your dictionary is named a_dict you could sort in place the way you want with something like: `[a_dict[i].sort() for i in a_dict]`. However, as others have pointed out, your code seems to be doing more. – saxitoxin Apr 17 '17 at 21:33
  • I've updated the question with more information and a link to the code I'm working off of. There is a fair bit going on in that one statement defining groups and I don't really know how to extend it further than it is. – Brando__ Apr 17 '17 at 21:46

2 Answers2

1

If d is your dictionary, you can sort all its values by

for _, l in d.items():
    l.sort()
fuglede
  • 17,388
  • 2
  • 54
  • 99
0

fuglede's answer almost worked for me. What I needed to do instead was use iteritems. items was doing some funky local version that was being thrown away after the code block was done with it per this

Iterating over dictionaries using 'for' loops

 for key, value in groups.iteritems():
      value.sort()
Community
  • 1
  • 1
Brando__
  • 365
  • 6
  • 24
  • 1
    I'm guessing you're working with Python 2? (cf. https://stackoverflow.com/questions/10458437/what-is-the-difference-between-dict-items-and-dict-iteritems#10458567) – fuglede Apr 17 '17 at 23:06