## solution 1: fast enough response, simple function, fail fast
from time import clock
def isanaword(k,s):
    """ goes through the letters of second word (s) and returns it
        if first word (k) contains exactly same letters in same number
        """
    for c in s:
        pos = k.find(c)
        if pos==-1: return "" ## letter not contained in first one found
        k = k[:pos]+k[pos+1:] ## drop the letter in found position pos by slicing
    if not k: return s ## if all letters used, full anagram

if __name__=="__main__":
    print('To quit enter empty line')
    inputword=' '
    while inputword:
        inputword=raw_input('Give word: ')
        if inputword:
            t=clock()
            for wd in [w.rstrip()
                       for w in open('words.txt')
                       if (len(w) == len(inputword)+1 ## newline longer
                           and isanaword(w.rstrip(),inputword))]:
                print wd,
            print
            print 'Took %i ms'%((clock()-t)*1000)

Dani AI

Generated

Short, practical summary referencing the thread: was correct that Tkinter is the quickest no-install option; 's pywin32 suggestion gives native Windows control; 's anagram ideas may reduce work by narrowing candidates before copying. For 's Windows 7 script the simplest, least-fragile approach is to collect the match strings into a list, join them into one result string, then copy that single string to the clipboard. Copying once (after the scan) is better than overwriting the clipboard for every match.

A small, cross‑version Tkinter helper that works on Python 2 and 3:

try:
    import Tkinter as tk
except ImportError:
    import tkinter as tk

def copy_to_clipboard(text):
    root = tk.Tk()
    root.withdraw()
    root.clipboard_clear()
    root.clipboard_append(text)
    root.update()   # ensure Windows receives the data before destroy
    root.destroy()

Integration notes: build a list of matches (use w.strip() when reading lines rather than relying on len(w) == len(inputword)+1), then call copy_to_clipboard("\n".join(matches)). On Python 2 pass a unicode object to Tkinter (e.g. result = unicode(result) or decode file text) so text encoding is correct.

Two alternatives:

  • pyperclip (very small, cross-platform): pip install pyperclip then import pyperclip; pyperclip.copy(result).
  • pywin32 (native): after installing the matching pywin32 for the Python version, use win32clipboard/win32con to set CF_UNICODETEXT if precise control is needed.

Troubleshooting tips: on Windows 7, root.update() is required so the clipboard keeps contents after the helper exits; verify with Notepad. If clipboard appears empty after the script closes (more common on X11/Linux), keep a small hidden Tk instance alive until the paste completes or use a native API. Avoid copying in tight loops; accumulate and copy once for a predictable result.

Recommended Answers

All 7 Replies

What operating system are you using?

commented: win7 +0

win7

I'm new at coding, how would I implement that Windows clipboard functionality to the script?

Install
pywin32-219.win32-py3.4.exe
If you don't have Python34 get the appropriate installer version.
After that simply use the functions supplied by the snippet.

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.