-4

Given the string... able\nacre\nbale\nbeyond\nbinary\nboat\nbrainy\ncare\ncat\ncater\ncrate\nlawn\nlist\nrace\nreact\nsheet\nsilt\nslit\ntrace\n

I am trying to figure out how to assign each word in the string to a variable, and then sort each word alphabetically which will allow me to compare them to see which ones are anagrams and which ones are not. I have around a month of Python experience so dumb everything WAY down if you could.

CoffeeRain
  • 4,460
  • 4
  • 31
  • 50
Aaron
  • 49
  • 2
  • 8
  • 2
    Don't assign each word to a variable. Assign all words to a list instead. – Martijn Pieters Mar 15 '13 at 16:58
  • 3
    What have you tried this far? Posting code to show what you're doing always yields better answers, because it shows that you're trying rather than just fishing for an easy answer. – Mia Clarke Mar 15 '13 at 16:59
  • 3
    You asked each this question before, I don't see how asking it again helps. In addition, you won't simply get code by asking for it without having tried anything yourself. – Rushy Panchal Mar 15 '13 at 16:59
  • 1
    Please update your *first* question to improve it, please do not re-post the same question over and over again. – Martijn Pieters Mar 15 '13 at 17:02

4 Answers4

5

Instead of saving each word to a variable, you should save them all to a list. Here is how I would approach the complete problem:

from itertools import groupby
from operator import itemgetter

s = 'able\nacre\nbale\nbeyond\nbinary\nboat\nbrainy\ncare\ncat\ncater\ncrate\nlawn\nlist\nrace\nreact\nsheet\nsilt\nslit\ntrace\n'
words = s.strip().split()
sorted_words = (''.join(sorted(line)) for line in words)
grouped = sorted((v, i) for i, v in enumerate(sorted_words))
anagrams = [[words[i] for v, i in g] for k, g in groupby(grouped, itemgetter(0))]

Result:

>>> import pprint
>>> pprint.pprint(anagrams)
[['able', 'bale'],
 ['binary', 'brainy'],
 ['boat'],
 ['acre', 'care', 'race'],
 ['cater', 'crate', 'react', 'trace'],
 ['cat'],
 ['lawn'],
 ['beyond'],
 ['sheet'],
 ['list', 'silt', 'slit']]
Andrew Clark
  • 202,379
  • 35
  • 273
  • 306
0
In [27]: s = 'able\nacre\nbale\nbeyond\nbinary\nboat\nbrainy\ncare\ncat\ncater\ncrate\nlawn\nlist\nrace\nreact\nsheet\nsilt\nslit\ntrace\n'

In [28]: words = s.split()

In [29]: [''.join(sorted(w)) for w in words]
Out[29]: 
['abel',
 'acer',
 'abel',
 'bdenoy',
 'abinry',
 'abot',
 'abinry',
 ...
NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • 2
    I think the OP wants to 'sort' each word (there is a homework assignment going round today), to detect anagrams. – Martijn Pieters Mar 15 '13 at 16:59
  • @MartijnPieters: This makes more sense. Corrected. – NPE Mar 15 '13 at 17:00
  • Not sure why you were downvoted for your first version though. – Martijn Pieters Mar 15 '13 at 17:01
  • @MartijnPieters: Probably because I misunderstood the question :) – NPE Mar 15 '13 at 17:02
  • Awesome thanks for the help. Right now I have the list.. ['abel', 'acer', 'abel', 'bdenoy', 'abinry', 'abot', 'abinry', 'acer', 'act', 'acert', 'acert', 'alnw', 'ilst', 'acer', 'acert', 'eehst', 'ilst', 'ilst', 'acert'] And I'm trying to figure out a way to write a function that goes through the list, and figures out how many times each sorted word appears in the list. Any ideas how I could do that with a def function? – Aaron Mar 15 '13 at 18:29
0

You can do yourstring.split('whattosplitat'). In this case, that would be

l='able\nacre\nbale\nbeyond\nbinary\nboat\nbrainy\ncare\ncat\ncater\ncrate\nlawn\nlist\nrace\nreact\nsheet\nsilt\nslit\ntrace\n'.split('\n')

Then you can do l.sort() which will sort your list alphabetically.

CoffeeRain
  • 4,460
  • 4
  • 31
  • 50
0
s = 'able\nacre\nbale\nbeyond\nbinary\nboat\nbrainy\ncare\ncat\ncater\ncrate\nlawn\nlist\nrace\nreact\nsheet\nsilt\nslit\ntrace\n'
words = sorted(s.split('\n')[:-1]) # the last one will be '', so you want to get rid of that

To test whether or not a string is an anagram of another string:

def isAnagram(a, b):
    aLtrs = sorted(list(a)) # if a='test', aLtrs=['e', 's', 't', 't']
    bLtrs = sorted(list(a)) # same as above
    return True if aLtrs==bLtrs else False
Rushy Panchal
  • 16,979
  • 16
  • 61
  • 94
  • `isAnagram("aab", "abb")` returns `True` for me, but I expect `False`. – Kevin Mar 15 '13 at 17:36
  • @Kevin this will only handle words with unique letters (only one of each per word)... However, there's a different way you can do it; I'll edit my answer. – Rushy Panchal Mar 15 '13 at 21:07