0

I want to search countries from my list here is my list:

 country=['American Samoa',
 'Andorra','Angola','Anguilla'
 ,'Antarctica',' Barbuda',
 'Argentina', 'Armenia', 'Aruba',
 'Australia',
 'Austria','Azerbaijan’,'Bahamas','Bangladesh','Barbados','BELARUS']

I have used this function but I think it's wrong

def find(f,seq):
    for item in country:
        if f(item):
            return item

Can you please correct my codes Any help would be appreciated

3
  • 2
    Why do you think it's wrong? Why do you pass seq to it if you don't use it? You probably just have to exchange country for seq. Also it would be nice to know what f is doing. Commented May 10, 2011 at 2:45
  • Because I'm not getting the result,I'm beginner in python and so confuse about this language.I thought f is a counter and that will return the first item in a sequence Commented May 10, 2011 at 2:57
  • The name of a variable never has any special meaning in Python, or in pretty much any other programming language I can think of. The purpose of a variable is the purpose that you give it. 'f' is whatever you pass to the function in that spot. Commented May 10, 2011 at 4:47

4 Answers 4

5

Hm, here's a stab which is perhaps a bit more clear:

def country_found(country_to_find):
    countries=['American Samoa','Andorra','Angola','Anguilla','Antarctica',
    'Barbuda','Argentina', 'Armenia', 'Aruba','Australia', 'Austria','Azerbaijan’,
    'Bahamas','Bangladesh','Barbados','BELARUS']

    return (country_to_find in countries)

Then you can just say "if country_found('Armenia'):" or whatever.

Sign up to request clarification or add additional context in comments.

5 Comments

just "return country_to_find in countries", no need for the if/else at the end.
It doesn't give me the string it gives me boolean value
I just want to search a counry from the list
@mary If you have a country, this function will return True if the country is found and False if it's not. Or, you could return country_to_find if country_to_find in countries if you want the actual string back for some reason.
Upvoted only because saw "if country_found('Armenia'):" - i'm Armenian :)
0

I think that you can just use

countries = ['American Samoa', ...]
if country in countries:
    ...

Comments

0

What you need is

filter(f,seq)

That's all.

And if you don't know what is f , it's a pity but we can't guess it.

See also iflter()

And search in the docs with this idea in mind when you need to do something rather generic: "there MUST be some function that does what I want, WHERE is it ?" .

Instead of "HOW to do that ?".

1 Comment

@mary And then !!!? Am I supposed to be surprised by this result ? On what basis should I be surprised ? I don't know what you want to obtain and what f is. DO you UNDERSTAND that WE CAN'T GUESS what f is , while the result will depend on f ? Instead of tripling the exclamation points, you should answer to the question that Felix Kling asked you 2 hours ago "You should explain what exactly you want do to..."
0

Yet another answer. The logic is basic. If item is in list, it also has an index. And accessing the element with this index (list[index]) will return verily this item :)

def find_country(country):
    try:
        i = countries.index(country)
        res = countries[res]
    except:
        res = "No such country"

    return res



$ python find_country.py 
Azerbaijan
Angola
No such country

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.