1

I have the following code

F_list= []
C_list= []
C_approx_list = []
for F in range(0,101,10):
    F_list.append(F)
    C_approx_list.append((F-30)/2.)
    C_list.append((F-32)*(5./9))

conversion = [F_list,C_approx_list,C_list]

for list in conversion:
    for number in list:
        print number

With the following output:

"""Test Execution
Terminal > python exercise2_18.py
0
10
20
30
40
50
60
70
80
90
100
-15.0
-10.0
-5.0
0.0
5.0
10.0
15.0
20.0
25.0
30.0
35.0
-17.7777777778
-12.2222222222
-6.66666666667
-1.11111111111
4.44444444444
10.0
15.5555555556
21.1111111111
26.6666666667
32.2222222222
37.7777777778
"""

I want the three lists to be on the following form instead:

F_list C_approx C_list

Instead of:

  1. F_list
  2. C_approx
  3. C_list

Thanks for any potential answer!

Palaios
  • 57
  • 1
  • 8
  • This might help: http://stackoverflow.com/questions/4908987/python-write-list-list-of-lists-in-columns – Nicolas Nov 07 '12 at 16:47

2 Answers2

3
for a,b,c in zip(F_list, C_approx_list, C_list)
    print(a,b,c)

Or for output that might be easier to read

for a,b,c in zip(F_list, C_approx_list, C_list)
    print('{}\t{}\t{}'.format(a,b,c))

or simpler in Python 3.X

for a,b,c in zip(F_list, C_approx_list, C_list)
    print(a,b,c, sep='\t')
Michael Mauderer
  • 3,777
  • 1
  • 22
  • 49
  • Thanks, that answers my question completely. Do you know how I could iterate over the lists without using zip? – Palaios Nov 07 '12 at 16:48
  • You could try to build a generator to emulate zip, but what for? – Michael Mauderer Nov 07 '12 at 16:52
  • Should change my question! How would you implement a list variable called conversion being equal to: conversion[i] = [[F_list],[C_approx_list],[C_list]]. so that i = 0 prints out F_list[0], C_approx_list[0], C_list[0] – Palaios Nov 07 '12 at 16:58
  • 2
    @Palaios: `conversion = zip(F_list, C_approx_list, C_list)` – Eric Nov 07 '12 at 17:06
1

You could write this using a list comprehension:

conversion = [(F, (F-30)/2., (F-32)*(5./9)) for F in range(0,101,10)]

for line in conversion:
    print temp[0], temp[1], temp[2]
    # print(*temp)                           # in python3

You can move to your previous lists using zip:

F_list, C_approx_list, C_list = zip(*conversion)
Andy Hayden
  • 359,921
  • 101
  • 625
  • 535
  • Thank you for the different approach.. What is the function of the * ? – Palaios Nov 07 '12 at 17:29
  • 1
    It unpacks elements of the list into function args - see [this question](http://stackoverflow.com/questions/2921847/python-once-and-for-all-what-does-the-star-operator-mean-in-python). :) – Andy Hayden Nov 07 '12 at 17:46