Find a word composed only of different symbols



  • How do you find a word composed only of different symbols? If that's a few words, find the first one.

    For example: For example, the line has words with different symbols (am, studying, new, and, want, to, learn, lot)

    string = "I am studying programming. I'm new and I want to learn a lot."
    

    But there are a few, I need to find the first.

    Please, how can I find it?



  • The answer depends on what you understand under the symbol, for example, if the symbol is Unicode codepoint,

    word = next(w for w in words if len(w) == len(set(w)))
    

    If you want the letters visible to the user, consider:

    import regex as re # pip install regex
    

    chars = lambda word: re.findall(r'\X', word)
    word = next(w for w in words if len(chars(w)) == len(set(chars(w))))

    The procedure for breaking the text into words depends on the task, for example:

    >>> words = re.findall(r'\w+', string)
    >>> words
    ['I', 'am', 'studying', 'programming', 'I', 'm', 'new', 'and', 'I', 'want', 'to', 'learn', 'a', 'lot']


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2