c_S71
c_S71

Reputation: 27

How to print a data set list vertically?

I'm working on an assignment for my intro to comp. sci. class and I have data sent that I am trying to get to print vertically, but am unable to do so.

My code:

def getData():
    return [

    [8,8],[2,8],[3,7],

    [3,4],[2,6],[3,5],

    [6,5],[7,3],[7,5]

]

def getLables():
    return [1, 0, 0, 0, 0, 0, 1, 1, 1]

data = getData()
labels = getLables()
print('[X, Y]', data)
print('Labels:\n', labels)

My output:

[X, Y] [[8, 8], [2, 8], [3, 7], [3, 4], [2, 6], [3, 5], [6, 5], [7, 3], [7, 5]]
Labels:
[1, 0, 0, 0, 0, 0, 1, 1, 1]

Output I need:

[X, Y]  Lable
[8, 8]   1
[2, 8]   0
[3, 7]   0

and so on...

I tried using sep='\n' & end='\n' in the print statements, but it just puts adds an extra line.

Upvotes: 0

Views: 96

Answers (3)

Alain T.
Alain T.

Reputation: 42143

You need to either go through the two lists in parallel in a for loop or join them into a single list:

Parallel loop:

data   = getData()
labels = getLabels()
print("[X,Y]","Label")
for index in range(len(data)):
    print(data[index], labels[index])

merging the lists:

print("[X,Y]","Label")
for d,l in zip(getData(),getLabels()):
    print(d,l)

Upvotes: 2

YamiOmar88
YamiOmar88

Reputation: 1476

I propose the following:

def getData():
    return [[8,8],[2,8],[3,7],[3,4],[2,6],[3,5],[6,5],[7,3],[7,5]]

def getLables():
    return [1, 0, 0, 0, 0, 0, 1, 1, 1]

data = getData()
labels = getLables()

print('[X, Y] Labels')
for a,b in zip(data, labels):
    print(a, b)

Upvotes: 4

Parakiwi
Parakiwi

Reputation: 611

You could keep an index and iterate through each list equally:

print('[X, Y]', 'Labels:')
i = 0
while i < len(data):
    print(data[i], labels[i])
    i += 1

Upvotes: 1

Related Questions