简体   繁体   中英

String in Python 3.4

I have this question of how to return true for strings. Can anyone help me on how to prompt user input in Python 3.4 and answer this question

Write a class/function to return True if 2 input strings are anagram to each other. string1 is an anagram of string2 if string2 can be obtained by rearranging the characters in string1.

Example:
string1 = 'smart'
string2 = 'marts'
result: True

string1 = 'secure'
string2 = 'rescue' 
result: True

Perhaps something along the lines of ( warning untested code ):

def isAnagram(string1, string2):
    if sorted(list(string1)) == sorted(list(string2)):
        return True
    else:
        return False

Admittedly there are more concise ways of doing this however this is particularly easy to understand in my view.

Try this:

def is_anagram(a, b):
    return True if sorted(a) == sorted(b) else False

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM