Return the position of a word in a string

Asked

Viewed 2,994 times

2

Could someone help me with this question?

def fileRead(file):
        txt = open(file)
        print(txt.read())

def index(filepath, keywords):
    with open(filepath) as f:
        for lineno, line in enumerate(f, start=1):
            matches = [k for k in keywords if k in line]
            if matches:
                result = "{:<15} {}".format(','.join(matches), lineno)
                print(result)

#Programa Principal
fileRead('cores.txt')
fileRead('carta.txt')
index('cores.txt', ["amarelo"])
index('carta.txt', ["azul"])
index('carta.txt', ["verde"])
  • What’s the problem with the code? if possible edit the question and explain what the purpose of it, and also post a snippet of a file and tell what you expect to get from it!

  • @stderr I have 2 files . txt that need to be read and produce the set of words contained in it .... then take as parameter the set of reference words and the name of the search file, and write, in the default output, the word and its placement in the search file if it is in the reference set. Ex.: Input : letter.txt / Output: ("blue", 3, 5) But ... I can only print the text (+) yellow 1 blue 3 green 4

1 answer

1


In your code the word and the line number is already returned, to return also the position of the word in the line, use the str.index:

pos = line.split().index(keyword)

The str.split will separate the line into pieces (separated by space) and with str.index get the word position. See an example:

s = 'foo bar baz'
i = s.split().index('bar') + 1

print ('bar é a {} palavra de {}'.format(i, s))
# bar é a 2 palavra de foo bar baz

In your code do so:

def index(filepath, keywords):
    output = "Keyword: {}, Line: {}, Index: {}"

    with open(filepath) as f:
        for lineno, line in enumerate(f, start = 1):
            line = line.rstrip()

            for keyword in keywords:
                if keyword in line:
                    pos = line.split().index(keyword) + 1

                    result = output.format(keyword, lineno, pos)
                    print (result)

index('cores.txt', ['amarelo'])

Assuming that the file colors.txt have the content below:

verde
vermelho
amarelo
cinza amarelo
azul

The result will be:

Keyword: amarelo, Line: 3, Index: 1
Keyword: amarelo, Line: 4, Index: 2
  • @sterr blz! Just one more question...... as for the Indian counting letter to letter... I need you to count the word Ex.: Keyword: yellow, Line: 3, Index: 1

  • @Absoares Add +1. Thus: line.index(keyword) + 1. That’s it?

  • @sterr .... ex> yellow is in 2 line and is in 1 word / and in 4 line , 2 word

  • @Absoares I didn’t understand very well. You want me to write "yellow is in 2 line and is 1 word / and in 4 line , 2 word"? The line.index(keyword) + 1 didn’t work?

  • @sterr ... line.index(keyword) + 1 did not work I need you to return me like this: Keyword: yellow, Line: 4, Index: 2

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.