Reputation: 35
Currently I have some data:
EXAMPLE_DATA = [
['time', 'age', 'height', 'width', 'ethnicity', 'religion'],
['18:42:11', '61', '153.9615', '0.8', 'Mixed', 'None'],
['18:35:00', '34', '116.4253333', '10.17', 'Mixed', 'None']]
I have a function called 'example_func' that calls EXAMPLE_DATA[1] e.g: the 2nd row of the data
I then used the code:
def display_data( example_func ):
for row in example_func:
print(row)
This gives the following output:
18:42:11
61
153.9615
0.8
Mixed
None
I want the following output to be:
Time: 18:42:11
Age: 61
Height: 153.9615
Ethnicity: Mixed
However, I want to set the headings in my code, and don't want to use the headings from the EXAMPLE_DATA.
As you'll have noticed, I also don't want to display 'Width' or 'Religion' in my final output.
If you need any more information please let me know.
Upvotes: 0
Views: 352
Reputation: 123473
Not exactly sure I understand all of your question, but here's a guess:
from collections import namedtuple
EXAMPLE_DATA = [
['time', 'age', 'height', 'width', 'ethnicity', 'religion'],
['18:42:11', '61', '153.9615', '0.8', 'Mixed', 'None'],
['18:35:00', '34', '116.4253333', '10.17', 'Mixed', 'None']]
def display_data(example_func):
Record = namedtuple('Record', example_func[0])
for row in example_func[1:]:
print('Time: {time}\n'
'Age: {age}\n'
'Height: {height}\n'
'Ethnicity: {ethnicity}\n'.format(**Record(*row)._asdict()))
display_data(EXAMPLE_DATA)
You could write it a little more concisely using f-strings in Python 3.6+.
def display_data(example_func):
Record = namedtuple('Record', example_func[0])
for rec in (Record(*row) for row in example_func[1:]):
print(f'Time: {rec.time}\n'
f'Age: {rec.age}\n'
f'Height: {rec.height}\n'
f'Ethnicity: {rec.ethnicity}\n')
Upvotes: 2
Reputation: 149
Note: the following only works if you pass the whole table instead of one row. Oversight on my part, sorry. It's significantly more elegant this way. If you really want it to only work on one row, example code is at the end.
You could make a dictionary, mapping the headings you want to the index they have in EXAMPLE_DATA
:
HEADINGS = {'Time':0, 'Age':1, 'Height':2, 'Ethnicity':4}
With this, you can just run through the keys and display the value at the corresponding index.
def display_data( example_func, headings ):
for row in example_func:
for key in headings.keys():
print(key + ": " + row[headings[key]]
However, if you want absolute efficiency, you can calculate headings.keys()
once and just use it several times. It doesn't matter.
def display_data( example_func, headings ):
keys = headings.keys()
for row in example_func():
for key in keys:
print(key + ": " + row[headings[key]]
One-row example code:
def display_data( example_func, headings ):
keys = headings.keys()
for row in [example_func()]:
for key in keys:
print(key + ": " + row[headings[key]]
Upvotes: 0
Reputation: 1334
Here is a function that does the job:
def display_data(example_func):
headings = ['Time', 'Age', 'Height', 'Ethnicity'] # Your headings
rows = [0, 1, 2, 4] # The corresponding rows
for heading, row in zip(headings, rows): # extracts each heading and the corresponding row
print(heading + ': ' + example_func[row])
Upvotes: 2
Reputation: 21
use Pandas for your problem. Pandas will automatically handle the data as your requested format. you can modify the output as your wish.
Upvotes: 0