Python Forum
Best way to get data from 2D array
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Best way to get data from 2D array
#11
You could load your dictionary in a loop. If your team_stats list is just a list of sub-lists of the form [team_name, strength, form, injuries, motivation, city], it would look like this

dict_teams = {}
for team in team_stats:
    dict_teams[team[0]] = Teams(*team)
The *team syntax turns the list in a series of parameters to the Team instance. The above loop can be turned into a dictionary comprehension:

dict_teams = {team[0]: Teams(*team) for team in team_stats}
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#12
Wow, that worked perfect, thank you!

I just added an extra for-loop to iterate over the different team columns;

    

dict_teams = {}

    for team in team_stats:
        for i in range(columns):
            dict_teams[team[i]] = Teams(*team)
Now I'll try to build the add- functions. So most likely I'll soon be here again asking for more help... :) thanks again!
Reply
#13
I’m trying to add the home- and away results for each team i create. My idea was to add a home_result list and an away_result list to the class attributes like below. Then i want to run the add function every time a new class instance is created.

This doesn't work because last_row, results and the increments i and j are not defined. I then tried to put the for loop in a function to witch i could send the arguments, but that didn't work either… Any good ideas?

class Teams:

    def __init__(self, legaue ="", name = "", strength = 0, form = 0, injuries = 0, motivation = 0, city="", home_results = [], away_results = []):
        self.legaue = legaue
        self.name = name
        self.strength = strength
        self.form = form
        self.injuries = injuries
        self.motivation = motivation
        self.city = city
        self.home_results = home_results
        self.away_results = away_results

        for row in range(last_row):
            if self.name == results[row][2]:
                self.home_results[row] = (results[row][4])
                i += 1

            if self.name == results[row][3]:
                self.away_results[j] = (results[row][5])
                j += 1
Reply
#14
Don't loop through the results in the team initialization. If you do it that way, you'll have to go through all the results once for each team. Initialize all the teams based on their names and the stats you created for them. Then, once you have all of the teams in team_dict, read through the results:

with open('path/to/results.txt') as results_file:
    for row in results_file:
        team_dict[row[4]].home_results.append(row)
        team_dict[row[5]].away_results.append(row)
I would even suggest add_away and add_home methods like I discussed earlier, so that all the results could be in the same format (have the team in question's score in the same place, that sort of thing).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#15
Thanks again, your solution is so much clever, saves a lot of iterations! However, i don't get the results i want.

My code looks like this: (results = result_file in your example, row[2] = home team name)
    for row in results:
        dict_teams[row[2]].home_results.append(row)

    for i in range(5):
        print(dict_teams["Bournemouth"].home_results[i])
I just put the line print(dict_teams["Bournemouth"].home_results[i]) in to see how it worked. I was expected the 5 latest home scores for Bournemouth, but instead i end up with:

['E0', '10/08/18', 'Man United', 'Leicester', '2', ’1']
['E0', '11/08/18', 'Bournemouth', 'Cardiff', '2', '0']
['E0', '11/08/18', 'Fulham', 'Crystal Palace', '0', '2']
['E0', '11/08/18', 'Huddersfield', 'Chelsea', '0', '3']
['E0', '11/08/18', 'Newcastle', 'Tottenham', '1', '2']

So regardless what team name i put in, i just get the same results.

Btw, this code above i just placed in teh main function. I don't really understand how to use or the advantages of the add_methods?

Sorry for beeing sucha newbie here, really want to learn this and i think i'm quite near now. Blush
Reply
#16
Look back at post #13, you have home_results = [] in the initialization. That's a problem. Lists are mutable, the are just passed as a reference to the list, not a copy of the list. Also, default parameters are stored in the function or method, and are not resolved every time the method is called. So every team's home_results is referencing the same list. That's why they have the same values in them.

I think the easiest solution here would be to remove that parameter from the parameter list. Instead just have home_results = [] in the body of the __init__ method. If it's in the body of the code, it will be resolved fresh each time the code is run, and you'll get five different lists.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#17
Wow, that would have taken me a year to figure out on my own. Thanks! :)
Reply
#18
I said to take out the parameter, because I didn't think you would ever be passing home_results to a new Team class. If you might be passing a list to a class, but you want it to default to an empty list, do this:

class Spam(object):

    def __init__(self, name, some_list = None):
        self.name = name
        if some_list is None:
            self.some_list = []
        else:
            self.some_list = some_list
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#19
I have problem accessing the class from another file. I think it has something to do with the dictionary i use to store the classes? I import the class and the function, but the error message says the dictionary is not defined?
Reply
#20
What's your current code look like?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Seeing al the data in a dataframe or numpy.array Led_Zeppelin 1 1,139 Jul-11-2022, 08:54 PM
Last Post: Larz60+
Question Change elements of array based on position of input data Cola_Reb 6 2,112 May-13-2022, 12:57 PM
Last Post: Cola_Reb
  how to print all data from all data array? korenron 3 2,456 Dec-30-2020, 01:54 PM
Last Post: korenron
  Import CSV data into array and turn into integers DoctorSmiles 5 3,193 Jul-16-2020, 10:47 AM
Last Post: perfringo
  Issue with creating an array of pixel data for PNG files in Google Colab The_Sarco 1 1,922 Apr-29-2020, 12:03 AM
Last Post: bowlofred
  python3 List to array or string to extract data batchenr 4 3,230 May-28-2019, 01:44 PM
Last Post: buran
  Reading data from serial port as byte array vlad93 1 12,052 May-18-2019, 05:26 AM
Last Post: heiner55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020