[Tutor] pointers or references to variables or sub-sets of variables query.

David L Neil PyTutor at DancesWithMice.info
Sun Jul 7 04:19:05 EDT 2019


First-off, it has to be said that "100's of elements" suggests using an 
RDBMS - particularly if 'age' (eg 23 and 99) is not the only likely 
selection mechanism.


On 7/07/19 2:39 PM, mhysnm1964 at gmail.com wrote:
> Hi all.
> In C, you can use pointers to reference variables, arrays, ETC. In python, I
> do not recall anything specifically that refers to such a capability. What I
> want to do is:

Just because C has a construct does not imply that it does, nor even 
should, exist in another language! You're using Python because it is 
'better', right?

You are correct, Python does not use "pointers", and (a personal 
comment) I for one don't miss them and their many 'gotchas', eg 
out-by-one errors, preferring Python's constructs, eg for-each.

That said, Python's sequences (data structures, eg strings and lists) do 
offer indices, slicing, and striding. So, it is quite possible to 
(relatively) address the first item in a list as list_item[ 0 ]. You can 
read about these (and many other delights) in the docs...


> I want to create different data structures such as dictionaries which
> contain specific  list elements based upon specific keys. The original data
> structure could look like:
> Data = [
>    ['2019-01-19','Fred Flintstone',23],
> ['2019-02-01','Scooby doo', 99]
> ]

Warning1: seem to be missing any identification of the "key"
Warning2: the intro text talked about "dictionaries" (the correct word) 
but the code-snippet is describing nested lists


> The above structure does have 100's of elements. I want to reference
> specific lists within the above structure. Using the only method I know how:
> 
> Category = {'under-50':[data[0]], 'over-50':[data[1]]}
> 
> If I understand things correctly with Python. The above will copy the value
> into the list within the key. Not the memory address of the nested list I am
> referencing. I am using a list within the key to permit multiple references
> to different nested lists from the original data structure. The end result
> of the structure for the dict could look like this (using example, not real
> output)
> 
> Category['under-50'] = [ List1 pointer, List22 pointer, List52 pointer]
> 
> I hope the above makes sense. How can this be done?

I hope I've understood the description! One option would be to follow 
your line of thinking by turning the first data-structure into a 
dictionary (key-value) pairs, where the key is the character's age and 
the value is the inner list structure, previously outlined:

{
   23: ['2019-01-19','Fred Flintstone',23],
   99: ['2019-02-01','Scooby doo', 99]
}

Then it would be possible to maintain the two lists, each containing 
keys for the relevant dict-elements:

under_50 = [ 23 ]
over_50 = [ 99 ]

However, this would require that only one character be listed at a given 
age (dict keys must be unique), so another key might be a better choice!


Another data structure you might consider is a "linked list".

-- 
Regards =dn


More information about the Tutor mailing list