简体   繁体   中英

Printing words with the same first and last character and words with the same second character and one before the last character

#!/ usr/bin/python3
import sys

def main():
    for line in sys.stdin:
        line = line.split()
        x    = -1
        for word in line:
            if word[-1]==word[0] or word[x-1]==word[1]:
                print(word)         

main()

It also prints dots at the end of the sentences, why? And words like 'cat' and 'moon' should also be out of the question. But it also prints these words. Can someone point me in the right direction please?

I think your problem is because the second and second last characters of 'cat' are the same.

def main():
    for line in sys.stdin:
        line = line.split()
        x    = -1
        for word in line:
            if (word[-1]==word[0] and len(word)<=2) or (word[x-1]==word[1] and len(word)<=4):
                print(word)

or something like that, depending on your preference.

This should get rid of that pesky cat, although moon stays.
It will also include words that use upper and lower case characters, so sadly not only will moon print but also Moon, MOon, mooN and moOn.

Edit: Forgot to test for one character words (a, I etc)

import sys
def main():
    for line in sys.stdin:
        line = line.split()
        for word in line:
            uword = word.lower()
            if len(uword) > 1:
                if uword[0:1]==uword[-1] or (uword[1:2]==uword[-2] and len(uword) > 3):
                    print(word)
main()

I got it guys, understood the question wrong. This prints the right words, that I got beforehand. That cleared things up for me. This is the right code but it still gives "sys.excepthook is missing". I run this code with another code that gives a space an newline. So every space between words becomes a newline: cat cdb.sentences| python3 newline.py| python3 word.py |head -n 5

import sys
def main():
    for line in sys.stdin:
        line = line.split()
        for word in line:
            letterword = lw = word.lower()
            if len(lw) > 1:
                if lw[0:1]==lw[-1] and (lw[1:2]==lw[-2]):
                    print(word)
main() 

import sys

def main():
    for line in sys.stdin:
        line = line.rstrip()
        text = ""
        for word in line:
            if word in ' ': 
                text=text + '\n'
            else:
                text=text + word
        print(text)
main()

It should give the 5 first words that have the same first, last letter, -2 and 1 letters. With an white line between each one of them. First i want to solve that hook. Thx

You are not helping yourself by answering your own question with what is essentially a completely different question in an answer.
You should have closed your original off by accepting one of the answers, if one of them helped, which it looked like they did and then asked a new question.

However, the answer to your 2nd question/answer can be found here:
http://python.developermemo.com/7757_12807216/ and it is a brilliant answer

Synopsis:
The reason this is happening is that you're piping a nonzero amount of output from your Python script to something which never reads from standard input. You can get the same result by piping to any command which doesn't read standard input, such as

python testscript.py | cd .

Or for a simpler example, consider a script printer.py containing nothing more than

print 'abcde'

Then

python printer.py | python printer.py

will produce the same error.

The following however will trap the sys.excepthook error:

import sys
import logging

def log_uncaught_exceptions(exception_type, exception, tb):

    logging.critical(''.join(traceback.format_tb(tb)))
    logging.critical('{0}: {1}'.format(exception_type, exception))

sys.excepthook = log_uncaught_exceptions
print "abcdfe"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM