[Tutor] searching for a string in a dictionary
Alan Gauld
alan.gauld at freenet.co.uk
Wed Aug 9 09:14:24 CEST 2006
> all_types:
> <Storage {'type': 'childcare', 'id': 1, 'created':
> datetime.datetime(2006, 7, 26, 15, 18, 34, 887436)}>
> <Storage {'type': 'household', 'id': 2, 'created':
> datetime.datetime(2006, 7, 26, 15, 18, 34, 887436)}>
> .....
> <Storage {'type': 'knews', 'id': 10, 'created':
> datetime.datetime(2006, 7, 26, 15, 18, 34, 887436)}>
> there is such a list
>
> how do i search for a particular string say teststring in this list
This isn't a list in any sense that Python will recognise.
Can you show us the actual data structures please?
> if teststring in all_types:
This would only work if teststring was exactly equal to a
whole string in your list, partial string matches only work against a
single string:
>>> if "fred" in ["fred", "jack"]: print True
True
>>> if "red" in ["fred", "jack"]: print True
>>>
>>> for item in ["fred", "jack"]:
>>> if "red" in item: print item
...
fred
>>>
> if teststring in all_types.type:
No idea what you think this would do.
lists don't have a type attribute and you can't access bits
of strings that way either.
What Python tutorial are you reading?
Almost all the tutorials cover string searching and list access.
> should i use lambda
Almost certainly not.
Why do you think lambda would be useful here?
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld
More information about the Tutor
mailing list