Ordered dictionary?
Josiah Carlson
jcarlson at nospam.uci.edu
Fri Jan 23 14:55:04 EST 2004
Carmine Moleti wrote:
> Hi Dragos,
>
>
>>def filterList(p_list, p_value):
>> l_len = len(p_list)
>> l_temp = map(None, (p_value,)*l_len, p_list)
>> l_temp = filter(lambda x: x[1].find(x[0])==0, l_temp)
>> return map(operator.getitem, l_temp, (-1,)*len(l_temp))
>
>
>>phones_dict = {'jansen' : '0000', 'xxx' : '1111', 'jan2' : '2222'}
>>names_list = filterList(phones_dict.keys(), 'jan')
>>phones_list = map(phones_dict.get, names_list)
>
>
>>This extracts from the dictionary the telephone(values) numbers for
>>names(keys) starting with 'jan'...
>
>
> Why you didn't used the string.startswith(...) method?
>
> I wrote this:
>
> d={'carmine':'123456','carmela':'4948399','pippo':'39938303'}
> for name,number in d.items():
> if name.startswith('car'):
> print name,number
>
> This also extract from the dictionay all the (name,number) pairs whose
> name starts with a given substring ('car' in the example).
>
> Thanks for your answer
Something strange is that for short 'other' strings
string[:len(other)] == other
#is faster than
string.startswith(other)
Maybe find is faster than startswith.
- Josiah
More information about the Python-list
mailing list