0

I would like the following 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,]

data = []
data.append(Tier0)
data.append(Tier1)
data.append(Tier2)
data.append(Tier3)
data.append(Tier4)
data.append(Tier5)    
data

for Tier1 in data[1:]: 
    Tier1.insert(1, float(input('Enter the weighted value of Tiers 1-5 as a decimal: ')))
    Tier1.insert(3, Tier1[1] * MissouriBusiness)#calculates the number of businesses
    Tier1.insert(4, Tier1[2] * Tier1[1] * MissouriBusiness)#calculates the revenue

Output to change 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 something 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 a good example of this, I just cannot seem to grasp how it works.

>>> for 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 from Python's documentation: https://docs.python.org/2/tutorial/inputoutput.html

A dumbed-down explanation of how this repr function works and how I could incorporate it would be greatly appreciated because I'm just seeing gibberish whenever viewing the documentation or other similar questions.

TIA

1 Answers1

0

The repr function is inteded to return "printable object representations", possibly for use with the eval function to construct the object back from it's string representation. But repr doesn't do any formatting itself, it just calls the __repr__ method on the object, which may be some default Python implementation or your implementation on your objects.

In your case, when you call repr on a list, it just returns quoted list output, as that's how repr is implemented for the list object:

>>> 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]]
>>> 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 example you provide is actually nothing more than just a manual formatting. repr in this case is only used to get a string representation of the integer x to be able to call string.rjust formatting function on it:

>>> repr(1)
'1'
>>> repr(1).rjust(3)
'  1'

As for your case, you could use the pprint module intended for Python data structures pretty-printing. The pprint.pprint function actually behaves almost exactly as you want:

>>> 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]]
>>> 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 you need something very specific, you may need to roll your own printing.

famousgarkin
  • 13,687
  • 5
  • 58
  • 74