Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

8
  • I clarified my question. I need to preserve the order of input file, so the output file has to be in order var1, var2,var3
    – Anastasia
    Commented May 7, 2015 at 5:48
  • 1
    @Anastasia: Then make results be an OrderedDict. Change results[key].append(value) to results.setdefault(key, []).append(value). Commented May 7, 2015 at 5:55
  • Also, the names of variables are words, they don't end with a number, so I can't simply re-order the file based on the numerical value.
    – Anastasia
    Commented May 7, 2015 at 5:56
  • No need to guard key = key.split('_')[0] with if '_' in key: because "nounderscore" == "nounderscore".split('_')[0]. Commented May 7, 2015 at 5:57
  • @Anastasia: No need to sort the result. collections.OrderedDict preserves the insertion order of keys. Commented May 7, 2015 at 5:58