I need a way of accessing directly an instance of a class through an ID number.

As I tried to explain here, I am importing a .csv file and I want to create an instance of my class Person() for every line in the .csv file, plus I want to be able to directly access these instances using as a key a unique identifier, already present in the .csv file.

What I have done so far, thanks to the help of user433831, is this:

from sys import argv
from csv import DictReader
from person import Person

def generateAgents(filename):
  reader = DictReader(open(filename, 'rU'))
  persons = [Person(**entry) for entry in reader]
  return persons

where person is just the module where I define the class Person() as:

class Person:
  def __init__(self, **kwds):
    self.__dict__.update(kwds)

Now I have a list of my person instances, namely persons, which is already something neat. But now I need to create a network among these persons, using the networkx module, and I definitely need a way to access directly every person (at present my instances don't have any name).

For example, every person has an attribute called "father_id", which is the unique ID of the father. Persons not having a father alive in the current population has a "father_id" equal to "-1".

Now, to link every person to his/her father, I'd do something like:

import networkx as nx 
G=nx.Graph() 
for person in persons: 
  G.add_edge(person, person_with_id_equal_to_father_id) 

My problem is that I am unable to access directly this "person_with_id_equal_to_father_id". Keep in mind that I will need to do this direct access many many times, so I would need a pretty efficient way of doing it, and not some form of searching in the list (also considering that I have around 150000 persons in my population).

It would be great to implement something like a dictionary feature in my class Person(), with the key of every instance being a unique identifier. This unique identifier is already present in my csv file and therefore I already have it as an attribute of every person.

Thank you for any help, as always greatly appreciated. Also please keep in mind I am a total python newbie (as you can probably tell... ;) )

有帮助吗?

解决方案

Simply use a dictionary:

persons_by_id = {p.id: p for p in persons}

This requires a recent version of Python. If yours doesn't support this syntax, use the following:

persons_by_id = dict((p.id, p) for p in persons)

Having done either of the above, you can locate the person by their id like so:

persons_by_id[id]

The networkx example becomes:

import networkx as nx 
G=nx.Graph() 
for person in persons: 
  if person.father_id != -1:
    G.add_edge(person, persons_by_id[person.father_id]) 

其他提示

Here's an efficient way to do what @Toote suggests:

def generateAgents(filename):
  with open(filename, 'rU') as input:
    reader = DictReader(input)
    persons = dict((entry['father_id'], Person(**entry)) for entry in reader)
  return persons
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top