Python Forum
Array - Problem with Simple Code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Array - Problem with Simple Code
#6
Hello again. This is the second part to my previous question. I've learned a bit about arrays, but the solution to this array problem still alludes me, so I thought I'd ask for help again here. Bottom line is, my instructor wants us to understand arrays (old skool). He says its like looking under the 'hood' of lists. So here's what I've got (below). Here's my array class that was given to me by him. I believe it was designed to mimic a 'restrictive list' to appear array-like. I've enclosed my .csv file and my instructor's directions. Right now all I want to do is populate the array with the info from the .csv file. "Step 1: Add a menu option to load students from a CSV file" (no problem). This should create an Array of Student objects which contain the relevant data."(problem) I figure I can figure out steps 2 and 3 on my own. At present I'm scouring You Tube for any information on arrays so I can teach myself.
class Array(object):
    """Represents an array."""

    def __init__(self, capacity, fillValue = None):
        """Capacity is the static size of the array.
        fillValue is placed at each position."""
        self._items = list()
        for count in range(capacity):
            self._items.append(fillValue)

    def __len__(self):
        """-> The capacity of the array."""
        return len(self._items)

    def __str__(self):
        """-> The string representation of the array."""
        return str(self._items)

    def __iter__(self):
        """Supports iteration over a view of an array."""
        return iter(self._items)

    def __getitem__(self, index):
        """Subscript operator for access at index."""
        return self._items[index]

    def __setitem__(self, index, newItem):
        """Subscript operator for replacement at index."""
        self._items[index] = newItem
This is my code. I'm trying to populate an array with the data from a .csv file (bottom of page)
    #a = Array(8)
    lyst1 = []
    with open("student_advisees_testdata.csv",'r') as csv_file:
        csv_reader = csv.reader(csv_file)
        next(csv_reader)

        for line in csv_file:
            line = line.split(',')
            index = 0
            for word in line:
                lyst2 = []
                lyst2.append(word)
                lyst1.append(lyst2)
   
    a = Array(8,lyst1)
    print(a)
.csv file:
ID,First Name,Last Name,Program,Start Date,Adv Last Name,Most Recent Term,GPA
104,Todd,Jeffries,B25590S,01/11/16,Willy,2017SU,3.9
047,George,Evans,B25590C,08/21/17,Willy,2017FA,3.1
257,Jennifer,Reynolds,B25590N,01/17/17,Willy,2017FA,2.9
1707,Twan,Jelens,B25590C,08/15/16,Smith,2017SP,3.3
1865,Paul,Knapp,B25590S,08/15/16,Willy,2018FA,3.4
2104,Phyllis,Cashwell,B25590C,08/15/16,Smith,2017SP,3.25
2214,Beeman,Ciani,B25590N,08/15/16,Willy,2017SP,2.9
2215,Gertrude,Carmella,B25590C,08/15/16,Willy,2018SP,3.8
2838,Morty,Balzano,B25590C,05/30/17,Willy,2018FA,2.7
3104,Steve,Abrams,B25590S,08/21/17,Willy,2018FA,3.15
Reply


Messages In This Thread
Array - Problem with Simple Code - by emerger - Oct-11-2018, 03:02 AM
RE: Array - Problem with Simple Code - by stullis - Oct-11-2018, 03:38 AM
RE: Array - Problem with Simple Code - by emerger - Oct-11-2018, 03:52 AM
RE: Array - Problem with Simple Code - by volcano63 - Oct-11-2018, 10:16 AM
RE: Array - Problem with Simple Code - by emerger - Oct-11-2018, 09:00 PM
Arrays - Populating an Array - by emerger - Oct-11-2018, 07:28 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Simple code not working properly tmv 2 484 Feb-28-2025, 09:27 PM
Last Post: deanhystad
  python code to calculate mean of an array of numbers using numpy viren 3 1,174 May-29-2024, 04:49 PM
Last Post: Gribouillis
  Help with simple code JacobSkinner 1 1,316 Mar-18-2024, 08:08 PM
Last Post: deanhystad
  I have a code which is very simple but still I cannot detect what's wrong with it max22 1 1,241 Nov-07-2023, 04:32 PM
Last Post: snippsat
  A simple problem, how best to solve it? SuchUmami 2 1,365 Sep-01-2023, 05:36 AM
Last Post: Pedroski55
  help me simple code result min and max number abrahimusmaximus 2 1,673 Nov-12-2022, 07:52 AM
Last Post: buran
  Simple encoding code ebolisa 3 2,291 Jun-18-2022, 10:59 AM
Last Post: deanhystad
  How to solve this simple problem? Check if cvs first element is the same in each row? thesquid 2 1,936 Jun-14-2022, 08:35 PM
Last Post: thesquid
  How would you (as an python expert) make this code more efficient/simple coder_sw99 3 2,566 Feb-21-2022, 10:52 AM
Last Post: Gribouillis
  Simple code question about lambda and tuples JasPyt 7 4,763 Oct-04-2021, 05:18 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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