python - Can someone explain how to use the repr function to format the output? -
python - Can someone explain how to use the repr function to format the output? -
this question has reply here:
understanding repr( ) function in python 4 answersi next code:
tier0 = ['tier', 'weights', 'price', 'number of businesses', 'revenue'] tier1 = ['tier 1', 180,] tier2 = ['tier 2', 300,] tier3 = ['tier 3', 450,] tier4 = ['tier 4', 600,] tier5 = ['tier 5', 750,] info = [] data.append(tier0) data.append(tier1) data.append(tier2) data.append(tier3) data.append(tier4) data.append(tier5) info tier1 in data[1:]: tier1.insert(1, float(input('enter weighted value of tiers 1-5 decimal: '))) tier1.insert(3, tier1[1] * missouribusiness)#calculates number of businesses tier1.insert(4, tier1[2] * tier1[1] * missouribusiness)#calculates revenue output alter from:
[['tier', 'weights', 'price', 'number of businesses', 'revenue'], ['tier 1', 0.2, 180, 40000.0, 7200000.0], ['tier 2', 0.1, 300, 20000.0, 6000000.0], ['tier 3', 0.3, 450, 60000.0, 27000000.0], ['tier 4', 0.15, 600, 30000.0, 18000000.0], ['tier 5', 0.2, 750, 40000.0, 30000000.0]] to nicer, like:
[['tier', 'weights', 'price', 'number of businesses', 'revenue'], ['tier 1', 0.2, 180, 14000.0, 2520000.0], ['tier2', 0.2, 300, 14000.0, 4200000.0], ['tier3', 0.2, 450, 14000.0, 6300000.0], ['tier4', 0.3, 600, 21000.0, 12600000.0], ['tier5', 0.1, 750, 7000.0, 5250000.0]] python documentation has illustration of this, cannot seem grasp how works.
>>> x in range(1, 11): ... print repr(x).rjust(2), repr(x*x).rjust(3), ... # note trailing comma on previous line ... print repr(x*x*x).rjust(4) ... 1 1 1 2 4 8 3 9 27 4 16 64 5 25 125 this code came python's documentation: https://docs.python.org/2/tutorial/inputoutput.html
a dumbed-down explanation of how repr function works , how incorporate appreciated because i'm seeing gibberish whenever viewing documentation or other similar questions.
tia
the repr function inteded homecoming "printable object representations", perchance utilize eval function build object it's string representation. repr doesn't formatting itself, calls __repr__ method on object, may default python implementation or implementation on objects.
in case, when phone call repr on list, returns quoted list output, that's how repr implemented list object:
>>> info = [['tier', 'weights', 'price', 'number of businesses', 'revenue'], ['tier 1', 0.2, 180, 40000.0, 7200000.0], ['tier 2', 0.1, 300, 20000.0, 6000000.0], ['tier 3', 0.3, 450, 60000.0, 27000000.0], ['tier 4', 0.15, 600, 30000.0, 18000000.0], ['tier 5', 0.2, 750, 40000.0, 30000000.0]] >>> repr(data) "[['tier', 'weights', 'price', 'number of businesses', 'revenue'], ['tier 1', 0.2, 180, 40000.0, 7200000.0], ['tier 2', 0.1, 300, 20000.0, 6000000.0], ['tier 3', 0.3, 450, 60000.0, 27000000.0], ['tier 4', 0.15, 600, 30000.0, 18000000.0], ['tier 5', 0.2, 750, 40000.0, 30000000.0]]" the repr illustration provide nil more manual formatting. repr in case used string representation of integer x able phone call string.rjust formatting function on it:
>>> repr(1) '1' >>> repr(1).rjust(3) ' 1' as case, utilize pprint module intended python info structures pretty-printing. pprint.pprint function behaves want:
>>> info = [['tier', 'weights', 'price', 'number of businesses', 'revenue'], ['tier 1', 0.2, 180, 40000.0, 7200000.0], ['tier 2', 0.1, 300, 20000.0, 6000000.0], ['tier 3', 0.3, 450, 60000.0, 27000000.0], ['tier 4', 0.15, 600, 30000.0, 18000000.0], ['tier 5', 0.2, 750, 40000.0, 30000000.0]] >>> import pprint >>> pprint.pprint(data) [['tier', 'weights', 'price', 'number of businesses', 'revenue'], ['tier 1', 0.2, 180, 40000.0, 7200000.0], ['tier 2', 0.1, 300, 20000.0, 6000000.0], ['tier 3', 0.3, 450, 60000.0, 27000000.0], ['tier 4', 0.15, 600, 30000.0, 18000000.0], ['tier 5', 0.2, 750, 40000.0, 30000000.0]] but if need specific, may need roll own printing.
python repr
Comments
Post a Comment