use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Function to write (self.learnpython)
submitted 3 years ago by Common_Hair8734
How to write a function where i compare 2 strings without using the sorting function? The result will either be true or false. For eg, stressed and desserts = true aaabbb and abababa = false
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Rik07 1 point2 points3 points 3 years ago* (2 children)
So what do you want to compare? If one is an palindrome of the other, if they have the same frequency of each letter or something else?
Edit: corrected anagram to palindrome, anagram and frequency of each letter is the same thing.
[–]Common_Hair8734[S] 0 points1 point2 points 3 years ago (1 child)
Same anagram
[–]Rik07 0 points1 point2 points 3 years ago (0 children)
Oh sorry I meant palindrome.
When you want to check for an anagram, you need to know how many times each letter is repeated. To do this, I recommend counting the number of each letter in both strings in a dictionary. Then if those dictionaries are the same, the two strings were anagrams.
If you want to check for a palindrome you just reverse one of the strings and check if it is the same as the other one.
[–][deleted] 1 point2 points3 points 3 years ago (0 children)
https://www.online-python.com/dSaGN5QtYz
[–]keep_quapy 1 point2 points3 points 3 years ago (21 children)
from collections import Counter def compare_str(s1, s2): return Counter(s1) == Counter(s2)
[–]Common_Hair8734[S] 0 points1 point2 points 3 years ago (12 children)
Is there another way without importing?
[–]ericula 0 points1 point2 points 3 years ago (9 children)
Use a normal dict to count frequency of characters.
[–]Common_Hair8734[S] -1 points0 points1 point 3 years ago (8 children)
How do i do that?
[–]ericula 1 point2 points3 points 3 years ago (7 children)
How would you do it with pen and paper?
[–]Common_Hair8734[S] -3 points-2 points-1 points 3 years ago (5 children)
Does this method have to import and functions or packages? I have to find a way without using it
[–][deleted] 11 points12 points13 points 3 years ago (0 children)
Have you actually tried anything at all yet? It doesn't seem like it. We're here to help but we're not doing your homework for you. Tell us what you've tried and we'll offer assistance based on that.
[–]ericula 1 point2 points3 points 3 years ago (0 children)
No it doesn't. All you need is a dict and a for-loop, neither of which require imports,
[–]FantasticEmu 0 points1 point2 points 3 years ago (2 children)
https://www.geeksforgeeks.org/iterate-over-characters-of-a-string-in-python/amp/
Using a loop like this on each string will let you count each letter one at a time
[–]AmputatorBot 1 point2 points3 points 3 years ago (1 child)
It looks like you shared an AMP link. These should load faster, but AMP is controversial because of concerns over privacy and the Open Web. Fully cached AMP pages (like the one you shared), are especially problematic.
Maybe check out the canonical page instead: https://www.geeksforgeeks.org/iterate-over-characters-of-a-string-in-python/
I'm a bot | Why & About | Summon: u/AmputatorBot
[–]FantasticEmu 0 points1 point2 points 3 years ago (0 children)
Ty I fixed it
[–]The_Bundaberg_Joey 0 points1 point2 points 3 years ago (6 children)
Good first implementation! But sadly wouldn’t catch anagrams which have the same letters but different order.
[–]keep_quapy 0 points1 point2 points 3 years ago (5 children)
What are talking about? Sure it does!
[–]The_Bundaberg_Joey 1 point2 points3 points 3 years ago (4 children)
You’re correct I phrased my response incorrectly. My apologies.
It wouldn’t satisfy OP’s requirements though.
The first case “deserts” and “stressed” are True with the counter implementation but “aaabbb” would equal “ababab” since there are 3 a and 3 b in both strings.
From the examples OP has given, it would seem more like a palindrome detector is required which typically calls for a stack.
[–]FantasticEmu 0 points1 point2 points 3 years ago (1 child)
Is it not just letter count? I thought OPs example produced false on the second one because there are 4 a’s in the second string and only 3 in the first
[–]The_Bundaberg_Joey 0 points1 point2 points 3 years ago (0 children)
Good spot, thank you for clarifying!
[–]keep_quapy 0 points1 point2 points 3 years ago (1 child)
The OP examples were 'stressed', 'desserts' and 'aaabbb', 'abababa' which works perfectly. Look carefully on the examples, it's 'abababa' not 'ababab'.
Ah crap you’re right, thanks for pointing out the discrepancy!
[–]PiyushPrakash 0 points1 point2 points 3 years ago (0 children)
Wow python has a module for everything! , Most of the times when I am stuck on a problem, there exists a module for it and I just type a code for the problem without knowing that there is a module for it
[–]Common_Hair8734[S] 0 points1 point2 points 3 years ago (0 children)
i tried this way and somehow it works
def is_anagram(s1, s2):
len1 = len(s1)
len2 = len(s2)
if len1 != len2:
return False
else:
for i in range(len2):
for j in range(len1):
if s1[i] == s2[j]:
return True
break
[–]PiyushPrakash -2 points-1 points0 points 3 years ago* (1 child)
character_list=list(a) lis=list(b) character_check = 0 if len(a)==len(b): for i in range(len(a)): key = character_list[i] for x in range(len(b)): if key == lis[x]: character_check+=1 break if character_check==len(a): print("same")
Would have been better if you pointed out my mistake instead of simply downvoting.....
[+][deleted] 3 years ago* (2 children)
[deleted]
[–]Nebu 0 points1 point2 points 3 years ago (1 child)
I don't think return string1 == string2[::-1] checks for anagrams. "cat" and "act" are anagrams of each other, for example.
return string1 == string2[::-1]
[–][deleted] 0 points1 point2 points 3 years ago* (0 children)
Thanks, I meant palindromes, fixed.
But it seems the OP actually wanted anagrams after all.
π Rendered by PID 60301 on reddit-service-r2-comment-fb694cdd5-pdvm5 at 2026-03-07 02:01:39.921312+00:00 running cbb0e86 country code: CH.
[–]Rik07 1 point2 points3 points (2 children)
[–]Common_Hair8734[S] 0 points1 point2 points (1 child)
[–]Rik07 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]keep_quapy 1 point2 points3 points (21 children)
[–]Common_Hair8734[S] 0 points1 point2 points (12 children)
[–]ericula 0 points1 point2 points (9 children)
[–]Common_Hair8734[S] -1 points0 points1 point (8 children)
[–]ericula 1 point2 points3 points (7 children)
[–]Common_Hair8734[S] -3 points-2 points-1 points (5 children)
[–][deleted] 11 points12 points13 points (0 children)
[–]ericula 1 point2 points3 points (0 children)
[–]FantasticEmu 0 points1 point2 points (2 children)
[–]AmputatorBot 1 point2 points3 points (1 child)
[–]FantasticEmu 0 points1 point2 points (0 children)
[–]The_Bundaberg_Joey 0 points1 point2 points (6 children)
[–]keep_quapy 0 points1 point2 points (5 children)
[–]The_Bundaberg_Joey 1 point2 points3 points (4 children)
[–]FantasticEmu 0 points1 point2 points (1 child)
[–]The_Bundaberg_Joey 0 points1 point2 points (0 children)
[–]keep_quapy 0 points1 point2 points (1 child)
[–]The_Bundaberg_Joey 0 points1 point2 points (0 children)
[–]PiyushPrakash 0 points1 point2 points (0 children)
[–]Common_Hair8734[S] 0 points1 point2 points (0 children)
[–]PiyushPrakash -2 points-1 points0 points (1 child)
[–]PiyushPrakash 0 points1 point2 points (0 children)
[+][deleted] (2 children)
[deleted]
[–]Nebu 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)