Hello,

I am trying to figure out how to search determine if a word has all the letters in a predetermined set. So for example, I give the letters "yx" and I search a database of words and pull the words that contain only those letters. So if I used the previous letters a word like "x-ray" would show up. This is what I have so far:

all((c in chars) for c in word)

If I use any instead of all I can get all words just 1 of letters. But why can't I do it this way?!?!!

Cheers,

Dani AI

Generated

wanted a clean Python way to accept words that use only a small set of letters (your example: allow x and y, and accept x-ray because the hyphen isn’t a letter). Common gotchas are case, punctuation (hyphens, apostrophes) and Unicode letters. was on the right track with using a data structure you can reuse — below are two small, practical options: one compact that normalizes and tests the distinct letters, and one that early‑exits per character (better when scanning many words).

import re

def only_from(word, allowed):
    cleaned = re.sub(r'[^A-Za-z]', '', word).lower()   # remove non-letters
    return set(cleaned).issubset(set(allowed.lower()))
ALLOWED = set('yx')  # compute once if checking many words

def only_from_fast(word):
    for ch in word.lower():
        if ch.isalpha() and ch not in ALLOWED:
            return False
    return True

Notes and tips:

  • Choose the first when you want a compact set-based check (useful if you need to know which letters appear). Choose the second for streaming/large lists because it returns as soon as a bad letter appears.
  • If you need to allow digits or internal punctuation, tweak the regex or the isalpha() check to include those characters.
  • For non-ASCII letters (accents, other scripts) prefer ch.isalpha() and, if needed, normalize with unicodedata.normalize before testing.
  • Precompute ALLOWED once and reuse it when filtering thousands of words; that saves repeated set construction and makes the loop cheap.

These approaches expand on ’s suggestion to use a reusable set and also handle the practical issues (’s x-ray case) that commonly trip people up.

Recommended Answers

All 5 Replies

First, what language are you using? Next, why isn't this in the Software Development forum?

hmmm I just noticed that. Sorry. Thought i out it under Python....

So you mean you want a function that determines if a word contains all of the specified letters; ie: has_all_letters("x-ray", "xy") would return true.

Start by iteration through all of the characters in the "xy" string. For each character. Check if the character exists within the "x-ray" string. If a letter does not exist, return false. If the end if the loop is reached, return true.

That paragraph should pretty much corralete one-to-one to the final code.

Thanks. Got it working it your way. If there is any more "pythonic" way of coding it please let me know.

Cheers,
cg

I'm not sure of a more Pythonic way of doing it, but there are faster (a little more complicated though) ways of doing it.

Right now it's running at O(m*n) time (m is the size of the "x-ray" string, n is the size of the the "xy" string). You can get this down to O(m+n) time by storing all of the letters in the "x-ray" string in a set, storing all of the letters in the "xz" string in another set, and confirming that the intercept is the same size of the "xy" string itself. There are a few other ways of getting the O(m+n) time though.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.