performance - Random-access container for strings in python? -


i manipulate indexed instances (say, music tracks) , have lookup object's name index (int->string). dicts slow (i have 10m objects). memory not problem, convinient solution create random-access array of strings csv file names.

however, have failed make in python -- got error 0-dim arrays (strings) couldn't indexed. what's native python way create random access container strings?

from remember, dictionaries in python have o(1) average access time, lists faster. if indices not sparse, can try this:

reader = [(1, 'a'), (2, 'b')] # replace csv reader.  # first, fill dictionary: text_dict = {} index, text in reader:     text_dict[index] = text  # create sufficiently large list: max_index = max(text_dict.iterkeys()) texts = [none] * (max_index + 1)  # , fill it: index, text in text_dict.iteritems():     texts[index] = text  print texts # prints: [none, 'a', 'b'] print texts[1] # prints: 

Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -