Implementing a simple abstract data structure in Python using lists
Implementing a simple abstract data structure in Python is relatively easy — particularly when compared to something like C, which we'll compare below. In this exercise, we are taking a comma-separated text file (CSV), loading it into Python, and populating a list object with its contents.
For reference, the following are the contents of the text file (players.txt) we'll be using:
JKIDD,Jason Kidd,Dallas Mavericks,Point Guard,5,21000000 KG,Kevin Garnett,Boston Celtics,Power Forward,5,22000000 KB24,Kobe Bryant,LA Lakers,Shooting Guard,24,19000000 SBATTIER,Shane Battier,Houston Rockets,Small Forward,31,6864200 ABIEDRINS,Andris Biedrins,Golden State Warriors,Center,15,9000000
As you can see, the comma separates the data fields, which are, in order: the ID, player name, team, position, jersey number, and salary. Knowing this, all we need to do is iterate over the file and split each line into sub-lists. Dictionaries are definitely better for this, and it's up to the student to figure out how.
1 2 3 4 5 6 7 8 9 10 |
path = "players.txt" dbfile = file(path, "r") players = [] for line in dbfile: temp = line.split(",") temp[5] = float(temp[5]) #convert salary data to a float players.append(temp) dbfile.close() |
That done, our new list, players, should be populated by the file contents, and ought to look like this:
[['JKIDD', 'Jason Kidd', 'Dallas Mavericks', 'Point Guard', '5', 21000000.0], ['KG', 'Kevin Garnett', 'Boston Celtics',
'Power Forward', '5', 22000000.0], ['KB24', 'Kobe Bryant', 'LA Lakers', 'Shooting Guard', '24', 19000000.0], ['SBATTIER'
, 'Shane Battier', 'Houston Rockets', 'Small Forward', '31', 6864200.0], ['ABIEDRINS', 'Andris Biedrins', 'Golden State
Warriors', 'Center', '15', 9000000.0]]
All done! You can now manipulate the file data in the list like you would any normal list. For further reading on the various list methods, you can refer to: An Introduction to Python Lists.
Now let's take a look at the code requried to perform a similar operation in C; this time, saving the contents in a C struct:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | #include<stdio.h> typedef struct Player{ char *name; char *team; char *position; int number; double salary; } My_Player; int main(void){ My_Player players[5]; char *path = "players.txt"; char tmpstr[254]; FILE *dbfile; int line_counter = 0; int parse_counter = 0; char *temp; dbfile = fopen(path, "r"); while(!feof(dbfile)){ if(fgets(tmpstr, 255, dbfile)){ temp = strtok(tmpstr, ","); while(temp != NULL){ switch(parse_counter){ case 0: players[line_counter].name = temp; break; case 1: players[line_counter].team = temp; break; case 2: players[line_counter].position = temp; break; case 3: players[line_counter].number = temp; break; case 4: players[line_counter].salary = atof(temp); break; } parse_counter++; temp = strtok(NULL, ","); } printf("%s", players[line_counter].name); parse_counter = 0; line_counter++; } } fclose(dbfile); return 0; } |
Not fun ;)