minks
minks

Reputation: 3039

How can I output the data in a customized way?

I am trying to output names of books along with their probabilities. This is what the current output looks like:

Happy Journey, What a Good Life!, Far Far Away, Slow and Steady, The Meeting
(0.94, 0.56, 0.43, 0.24, 0.10)

What I am trying to achieve is to have the probabilities paired with the names:

Happy Journey(0.94), What a Good Life!(0.56), Far Far Away(0.43), Slow and Steady(0.24), The Meeting(0.10)

This is what I have done till now:

f.write("Top 5 Probable Books:")     

for item in w:
    f.write(d.get(int(item))) #gets the names of the book from a dictionary
    f.write(", ")             # w is a label in the dictionary 
f.write("\n")

This is what my prob5 looks like: [[0.940021 0.561000 0.430012 0.241982 0.100134]] which is why I am rounding it off to 4 decimal places and I need to extract the probabilties from this numpy array.

for pk in prob5:
    jk=', '.join("%.4f" %b for b in pk) #picking out probabilities from a numpy array
    jk="("+jk+ ")" + "\t"
    f.write(jk)

I am getting a little confused as to how to run the two loops at the same time to achieve the desired output format.

Upvotes: 0

Views: 67

Answers (3)

Pier Paolo
Pier Paolo

Reputation: 920

I don't know what kind of problems you encounter while manipulating a numpy array, but if prob5 is as you described you could directly access its 0-th element as prob5[0]. As for printing 4 decimals, have a look at the syntax of .format().

Assuming that you are using Python 3:

>>> f.write(', '.join('{}({:.4f})'.format(string, number) for string, number in zip(w, prob5[0])))
Happy Journey(0.9400), What a Good Life!(0.5610), Far Far Away(0.4300), Slow and Steady(0.2420), The Meeting(0.1001)

If you use Python 2.x, use the itertools module and its izip function that has improved performance over the native zip. If the objects have a different number of elements, use izip_longest instead.

# example
import itertools
...
f.write(', '.join('{}({})'.format(string, number) for string, number in itertools.izip(w, prob5)))
#f.write(', '.join('{}({})'.format(string, number) for string, number in itertools.izip_longest(w, prob5))) # if w and prob5 have a different number of elements

Upvotes: 2

alec_djinn
alec_djinn

Reputation: 10819

Do you mean like this?

names = ['Happy Journey', 'What a Good Life!', 'Far Far Away', 'Slow and Steady', 'The Meeting']
probabilities = (0.94, 0.56, 0.43, 0.24, 0.10)
myDesiredOutput = ['{}({})'.format(names[n],probabilities[n]) for n,v in enumerate(names)]

>>>myDesiredOutput
['Happy Journey(0.94)',
 'What a Good Life!(0.56)',
 'Far Far Away(0.43)',
 'Slow and Steady(0.24)',
 'The Meeting(0.1)']

And to write the output directly to a file:

with open('out.txt', 'w') as f:
    for item in ['{}({})\n'.format(names[n],probabilities[n]) for n,v in enumerate(names)]:
        f.write(item)

You can change '\n' with ',' if you want to write the output in one single line.

Upvotes: 1

timlyo
timlyo

Reputation: 2404

You can use zip to combine the two lists and then itertools grouper to split into groups of two.

def grouper(iterable, n, fillvalue=None):
    "Collect data into fixed-length chunks or blocks"
    # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx"
    args = [iter(iterable)] * n
    return zip_longest(*args, fillvalue=fillvalue)

for group in grouper(zip(w, prob5), 2):
    print(group)

Upvotes: 1

Related Questions