Newbie DATA STRUCTURES Question.

Moshe Zadka moshez at math.huji.ac.il
Sat Jul 29 02:48:14 EDT 2000


On Sat, 29 Jul 2000, gbp wrote:

> Since I learned data structures in languages with pointers (C, pascal)
> I'm stuck.  How does one go about constructing a list of records in
> python?  My python is still pretty weak.  I understand how the lists
> work (a lot like Perl).  I understand that you can use objects as
> records.  I don't really get the full OOP though.  I guess that you
> can't make record anonymous in python?  Is that right?  Can you have
> pointers?  

"Everything is a pointer".

Here's a list of records:

[(1, "moshe"), (2, "gbp")

And here's code to turn this file

'''
1:moshe
2:gbp
'''

Into such a list:

def make_lor(file):
	ret = [] # an empty list
	lines = file.readlines() # read all lines from the file
	for line in lines: 
		number, name = string.split(line, ':')
		number = int(number) #  turn "number" into an integer
		record = (number, name) # a tuple
		ret.append(record) # append this
	return ret # return the created list

--
Moshe Zadka <moshez at math.huji.ac.il>
There is no IGLU cabal.
http://advogato.org/person/moshez





More information about the Python-list mailing list