I'd like to divide the Python first row output and make it second row output.

Asked 2 years ago, Updated 2 years ago, 92 views

I wrote the code like this.

The bottom of the boundary is the output statement that outputs the data I want. I wrote the code like the top of the border because I wanted to print the result in two columns.

Data = [('Result1'), ('Result2')] Recognize the 'inside' of this part as letters

The characters themselves are printed in two columns.

The output method I want is

Interpolation method: nearest

1hr 0.000 1.375

2hr 0.000 1.750

3hr -0.375 2.125

4hr -0.500 2.000

5hr -0.625 2.125

6hr -0.875 2.000

7hr -1.000 2.000

8hr -0.875 2.375

9hr -0.875 2.500

10hr -1.125 2.500

11hr -1.000 2.375

12hr -0.875 2.125

I want to do this type of output. Which part of the code should I fix?

When I ran the code, I attached a picture of the comments.

python iris

2022-09-22 14:38

3 Answers

Number 1

from itertools import zip_longest
data = [('Result1'),('Result2')]
for columns in izip_longest(*data,fillvalue=''):
    print '\t'.join(map(' ' . join,columns))

If you want to put the variables in the data variable of Result1 and Result2, You can only remove the '' that surrounds Results 1 and 2. The revised code is as follows:

from itertools import zip_longest
data = [(Result1),(Result2)]
for columns in izip_longest(*data,fillvalue=''):
    print '\t'.join(map(' ' . join,columns))

======================================== Number 2

print 'Interpolation method: ', interp
for it, out in zip(Time, Result1):
    print "%3.1d"%(it) + 'hr', '%10.3f'%(out)

print 'Interpolation method: ', interp
for it, out in zip(Time, Result2):
    print "%3.1d"%(it) + 'hr', '%10.3f'%(out)

If you want to print it out in Time, Result1, Result2, you can modify it as follows.

print 'Interpolation method: ', interp
for it, middle, out in zip(Time, Result1, Result2):
    print "%3.1d"%(it) + 'hr', '%10.3f'%(middle), '%10.3f'%(out)


2022-09-22 14:38


2022-09-22 14:38

Please keep that in mind.

r1 = {'1hr':1, '2hr':2, '3hr':3}
r2 = {'1hr':10, '2hr':20, '3hr':30}

d = {k:(v1, v2) for k, v1, v2 in zip(r1.keys(), r1.values(), r2.values())}

for k in d.keys():
    print(k, d[k][0], d[k][1])


1hr 1 10
2hr 2 20
3hr 3 30


2022-09-22 14:38

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.