0

I am the input from stdin

macys 100
sears 20
Boscov's 5
JCPenney 21
Kohl's 22
Others 16

This list will grow.

I want the output like below

macys sears Boscov's JCPenney Kohl's Others 
100    20   5         21       22     16
tobias_k
  • 81,265
  • 12
  • 120
  • 179
byomjan
  • 119
  • 1
  • 8
  • 2
    Is this a list of lists, or a list of strings, or just one big string? Try [`zip(*your_list)`](http://stackoverflow.com/questions/4937491/matrix-transpose-in-python) – tobias_k Mar 19 '14 at 23:16
  • this is input from hive query – byomjan Mar 19 '14 at 23:17
  • Does that come in as a string or is it formatted a python list? If it's a single string I'd recommend parsing it into a list first before attempting any operations. – asdf Mar 19 '14 at 23:19
  • @user3426930 So in what form is the data returned? Looks like tuples to me. – anon582847382 Mar 19 '14 at 23:20
  • select col1, col2 from hive table. new to python. So not sure which form it is – byomjan Mar 19 '14 at 23:23

3 Answers3

0

as everyone has mentioned use cols = zip(*rows) for example ...

some_text = """
macys 100
sears 20
Boscov's 5
JCPenney 21
Kohl's 22
Others 16
"""
#perhaps for you it will be some_text = sys.stdin.read()


#first convert it to a python list
some_list = some_text.splitlines()

#get rid of any empty lines
filtered_list = filter(None,some_list)

#split each line in half
rows_of_data = [line.split() for line in filtered_list]

#transpose it (turn it sideways)
headers,data = zip(*rows_of_data)


#print it out how you want
print "\n".join((" ".join(headers),
 " ".join("%-*s"%(len(headers[i]),value) for i,value in enumerate(data))))
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
0
for line in stdin:
    a = line.split()
    list_a = a[0]
    list_b = a[1]
print list_a
print list_b
Queenia
  • 11
  • 2
0
datastring = """macys 100
sears 20
Boscov's 5
JCPenney 21
Kohl's 22
Others 16"""

converted = zip(*(line.split() for line in datastring.splitlines()))

converted is list of tuples, each containing your items in given row.

You can use pandas external library to easily manipulate data later:

import pandas as pd

dt = pd.DataFrame.from_records(converted[1:], columns=converted[0])
print dt

Output:

  macys sears Boscov's JCPenney Kohl's Others
0   100    20        5       21     22     16
m.wasowski
  • 6,329
  • 1
  • 23
  • 30