add elements to indexed list locations

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Sat Jun 17 04:45:36 EDT 2006


In <1150528979.036821.246410 at g10g2000cwb.googlegroups.com>, levent wrote:

> I think I was thinking more of a linked-list idea, where you do not
> store the indices as integers to some random access array but rather as
> pointers into list's nodes. Then the subsequent inserts would not hurt
> previously stored pointers. For those who know a bit C++/STL here is a
> sketch of the idea:
> 
> name_list  heros;
> heros.push_back("clark");
> // ... add the rest
> indexed_name_list surnames;
> surnames.push_back(
>    make_pair(   find( heros.begin(), heros.end(), "clark"), "kent")   )
>    );  // the find function returns an iterator to appropriate location
> // ... add the rest
> 
> for_each(surnames.begin(), surnames.end(), insert_surnames)
> // insert_surnames is a callback that receives a single indexed surname
> // at a time and does the job, without affecting outer iterators.
> 
> 
> I was wondering how to make indices as *robust* in Python... Any ideas?

What about putting all information for each super hero into an object or
at least a list?  And those objects/lists can then be stored into a
dictionary with the first name of the heroes as key.  Something like this:

heroes = [['super', 'clark'], ['spider', 'peter'], ['bat', 'bruce']]

name2hero = dict((hero[1], hero) for hero in heroes)

fullnames = [['clark', 'kent'], ['peter', 'parker'], ['bruce', 'wayne']]
for name, surname in fullnames:
    name2hero[name].append(surname)

for hero in heroes:
    print 'Hello %s a.k.a %s %s' % tuple(hero)

IMHO you shouldn't try to program C++ in Python.  Take a step back and
describe *what* you want to achieve and not *how* you do it in another
language.  And then implement it in Python.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list