Gordon Lau
Gordon Lau

Reputation: 11

How do I iterate this list to create a matrix?

In an online course, part of the exercise is to create a matrix.

We were given the data below:

KobeBryant_FT = [696,667,623,483,439,483,381,525,18,196]
JoeJohnson_FT = [261,235,316,299,220,195,158,132,159,141]
LeBronJames_FT = [601,489,549,594,593,503,387,403,439,375]
CarmeloAnthony_FT = [573,459,464,371,508,507,295,425,459,189]
DwightHoward_FT = [356,390,529,504,483,546,281,355,349,143]
ChrisBosh_FT = [474,463,472,504,470,384,229,241,223,179]
ChrisPaul_FT = [394,292,332,455,161,337,260,286,295,289]
KevinDurant_FT = [209,209,391,452,756,594,431,679,703,146]
DerrickRose_FT = [146,146,146,197,259,476,194,0,27,152]
DwayneWade_FT = [629,432,354,590,534,494,235,308,189,284]

in order to create a matrix, this would be the code:

FT = np.array([KobeBryant_FT, JoeJohnson_FT, LeBronJames_FT, 
               CarmeloAnthony_FT, DwightHoward_FT, ChrisBosh_FT, ChrisPaul_FT,
               KevinDurant_FT, DerrickRose_FT, DwayneWade_FT])

I was wondering if there's a way to use a 'for' loop to emulate the output of the above code without having to manually type in KobeBryant_FT, JoeJohnson_FT, LeBronJames_FT, etc,etc.

Upvotes: 1

Views: 82

Answers (3)

Filip Matyja
Filip Matyja

Reputation: 137

It depends how you would receive this data. If it's in the *.txt file, than you can read it line by line(if you want to assign at the same time name with the list) or just read everything at once, and by using regular expression look for anything inside square brackets.

import re

file = open("data.txt","r").read()

lists_txt = re.finditer('[\[].*[\]]$', file, flags=re.MULTILINE)
#item in lists_txt = anything found inside [] brackets.

for item in lists_txt:
    txt = str(item.group(0))
    print(txt[1:-1])

Output from program:

696,667,623,483,439,483,381,525,18,196
261,235,316,299,220,195,158,132,159,141
601,489,549,594,593,503,387,403,439,375
573,459,464,371,508,507,295,425,459,189
356,390,529,504,483,546,281,355,349,143
474,463,472,504,470,384,229,241,223,179
394,292,332,455,161,337,260,286,295,289
209,209,391,452,756,594,431,679,703,146
146,146,146,197,259,476,194,0,27,152
629,432,354,590,534,494,235,308,189,284

You should get strings of of anything inside []. Now by using .split(",") you can convert them into lists.

Hopefully it will help you out :)

Upvotes: 1

Kevin Fang
Kevin Fang

Reputation: 2012

When you create the lists, you can put them in a dictionary, so that you can reference them in an easier way:

d = {"KB":[696,667,623,483,439,483,381,525,18,196], "JJ":[261,235,316,299,220,195,158,132,159,141]}
np.stack(d.values())
# Out[32]: 
# array([[261, 235, 316, 299, 220, 195, 158, 132, 159, 141],
#        [696, 667, 623, 483, 439, 483, 381, 525,  18, 196]])

If you want the array in some particular order, you could use an Ordereddict:

from collections import OrderedDict
d = OrderedDict({"KB":[696,667,623,483,439,483,381,525,18,196], "JJ":[261,235,316,299,220,195,158,132,159,141]})
np.stack(d.values())

Upvotes: 0

EMKAY
EMKAY

Reputation: 369

Try this,

import pandas as pd
df = pd.DataFrame(FT)

Upvotes: 0

Related Questions