Depending on what you're doing with the output, one option is to use JSON for the display.
import json
x = {'planet' : {'has': {'plants': 'yes', 'animals': 'yes', 'cryptonite': 'no'}, 'name': 'Earth'}}
print json.dumps(x, indent=2)
Output:
{
"planet": {
"has": {
"plants": "yes",
"animals": "yes",
"cryptonite": "no"
},
"name": "Earth"
}
}
The caveat to this approach is that some things are not serializable by JSON. Some extra code would be required if the dict contained non-serializable items like classes or functions.
Answer from David Narayan on Stack OverflowDepending on what you're doing with the output, one option is to use JSON for the display.
import json
x = {'planet' : {'has': {'plants': 'yes', 'animals': 'yes', 'cryptonite': 'no'}, 'name': 'Earth'}}
print json.dumps(x, indent=2)
Output:
{
"planet": {
"has": {
"plants": "yes",
"animals": "yes",
"cryptonite": "no"
},
"name": "Earth"
}
}
The caveat to this approach is that some things are not serializable by JSON. Some extra code would be required if the dict contained non-serializable items like classes or functions.
Use pprint
import pprint
x = {
'planet' : {
'name' : 'Earth',
'has' : {
'plants' : 'yes',
'animals' : 'yes',
'cryptonite' : 'no'
}
}
}
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(x)
This outputs
{ 'planet': { 'has': { 'animals': 'yes',
'cryptonite': 'no',
'plants': 'yes'},
'name': 'Earth'}}
Play around with pprint formatting and you can get the desired result.
- http://docs.python.org/library/pprint.html
Hi, say that you have a python dict like this:
mydict = {'a':1, 'b': {'c':3}}and you want to format it as follows:
mydict ={
'a':1,
'b': {
c:3
}
}or, well, you get the idea. What is the quickest way to do this is vim/vim plugins?
AndrewRadev has written some pretty nifty plugins, one of them being splitjoin
Hopefully that'll help you out :)
Using built-in vim features:
In a terminal: pip install autopep8
Then, in vim: au FileType python setlocal formatprg=autopep8\ -, then select your dictionary and gq.
Using a plugin: check out https://github.com/Chiel92/vim-autoformat. That's one of many plugins that can automatically format code for you.
My first thought was that the JSON serializer is probably pretty good at nested dictionaries, so I'd cheat and use that:
>>> import json
>>> print(json.dumps({'a':2, 'b':{'x':3, 'y':{'t1': 4, 't2':5}}},
... sort_keys=True, indent=4))
{
"a": 2,
"b": {
"x": 3,
"y": {
"t1": 4,
"t2": 5
}
}
}
I'm not sure how exactly you want the formatting to look like, but you could start with a function like this:
def pretty(d, indent=0):
for key, value in d.items():
print('\t' * indent + str(key))
if isinstance(value, dict):
pretty(value, indent+1)
else:
print('\t' * (indent+1) + str(value))
Just use pandas:
In [1]: import pandas as pd
Change the number of decimals:
In [2]: pd.options.display.float_format = '{:.3f}'.format
Make a data frame:
In [3]: df = pd.DataFrame({'gof_test': gof_test, 'gof_train': gof_train})
and display:
In [4]: df
Out [4]:

Another option would be the use of the engineering prefix:
In [5]: pd.set_eng_float_format(use_eng_prefix=True)
df
Out [5]:

In [6]: pd.set_eng_float_format()
df
Out [6]:

Indeed you cannot affect the display of the Python output with CSS, but you can give your results to a formatting function that will take care of making it "beautiful". In your case, you could use something like this:
def compare_dicts(dict1, dict2, col_width):
print('{' + ' ' * (col_width-1) + '{')
for k1, v1 in dict1.items():
col1 = u" %s: %.3f," % (k1, v1)
padding = u' ' * (col_width - len(col1))
line = col1 + padding
if k1 in dict2:
line = u"%s %s: %.3f," % (line, k1, dict2[k1])
print(line)
print('}' + ' ' * (col_width-1) + '}')
dict1 = {
'foo': 0.43657,
'foobar': 1e6,
'bar': 0,
}
dict2 = {
'bar': 1,
'foo': 0.43657,
}
compare_dicts(dict1, dict2, 25)
This gives:
{ {
foobar: 1000000.000,
foo: 0.437, foo: 0.437,
bar: 0.000, bar: 1.000,
} }