问题
I'm trying to write a program that checks whether 2 inputs are anagrams. I feel like this should be relatively easy but I can't seem to figure it out. I'm supposed to define a function as:
def isAnagram(s1, s2):
so far, I have this:
word1 = input("Enter a string: ")
word2 = input("Enter a second string: ")
def isAnagram(s1, s2):
s1 = word1.sort()
s2 = word2.sort()
if s1 == s2:
print("This is an anagram")
else:
print("This is not an anagram)
isAnagram()
I guess I don't fully understand defining functions, so if you could please explain what's happening, that would be great!
回答1:
You've defined the function almost correctly, but there are a few problems.
First you're asking for s1
and s2
as parameters. That's good. Now use those values, not the globals word1
and word2
.
Second, if those values are strings, you can't call sort
on them, because strings don't have a sort
method. But you can call the sorted
function on any sequence, even strings.
Third, there's a simple typo, a missing "
on the second print
.
It might be better to return a True or False value, and put the print
outside the function, but let's leave that for now.
Putting that together, here's a working function:
def isAnagram(s1, s2):
s1 = sorted(s1)
s2 = sorted(s2)
if s1 == s2:
print("This is an anagram")
else:
print("This is not an anagram")
But now, you have to call the function properly, too. You've defined the function to take two parameters, s1
and s2
. That means you need to call the function with two arguments.
So, where do you get those arguments? Well, you've already got those variables word1
and word2
sitting around, and they seem like exactly what you want.
So, change the last line to:
isAnagram(word1, word2)
And you're done.
回答2:
You've got the right idea, but since word1
and word2
are strings, they don't have the .sort()
attribute. You can still sort the letters within that string:
>>> w='hello'
>>> sorted(w)
['e', 'h', 'l', 'l', 'o']
The result of sorted()
is a list, we can turn it back to a string my joining them using an empty string:
>>> ''.join(sorted(w))
'ehllo'
Armed with this knowledge, your program might look something like:
word1 = input("Enter a string: ")
word2 = input("Enter a second string: ")
def isAnagram(s1, s2):
s1 = ''.join(sorted(word1))
s2 = ''.join(sorted(word2))
if s1 == s2:
print("This is an anagram")
else:
print("This is not an anagram")
isAnagram(word1, word2)
回答3:
Your approach to finding anagrams is perfectly reasonable. Sorting the words and comparing them is the easiest way to find if two words are anagrams of one another.
However, I think you're confused by the idea of function parameters. When you define
foo(x1, x2)
when foo
is called, it is expected to be called with 2 parameters. You define
anagram(s1, s2)
but never supply it with s1 and s2. The parameter list is not a list of variable names that you use in the function -- you can assign new variables at will. Instead, it is the list of inputs that the function takes.
so, anagram()
is incorrect. You need to call anagram(input1, input2)
. (Assuming you don't have default values, which I won't get into.
def isAnagram(s1, s2):
sortedWord1 = sorted(s1) # s1 is word1! (sorted instead of sort, strings are immutable)
#what should you do here?
if sortedWord1 == sortedWord2:
print("This is an anagram")
else:
print("This is not an anagram") # you forgot a closing quote!
word1 = input("Enter a string: ")
word2 = input("Enter a second string: ")
isAnagram(word1, word2)
I changed your code very slightly such that it should do the right thing. I recommend that you read up on functions a bit more before you continue, though.
Think of them just like functions in math! f(x)
is meaningful, f
, while still meaningful, is probably not what you were looking for.
>>> isAnagram("anagram", "nagaram")
This is an an anagram
>>> isAnagram("anagram", "woohoo")
This is not an anagram
>>> isAnagram("a", "a")
This is an an anagram
来源:https://stackoverflow.com/questions/19944214/anagram-python-3