LUCA BRODO
LUCA BRODO

Reputation: 21

Python List comprehension of a file

While studying open cv with an online course I found this line of code

classes = [r[r.find(' ') +1:] for r in all_rows]

What does this r[r.find(' ') +1:] mean? I'll include the entire code:

import cv2
img = cv2.imread('typewriter.jpg')


all_rows = open('synset_words.txt').read().strip().split('/n')


classes = [r[r.find(' ') +1:] for r in all_rows]

for(i,c) in enumerate(classes):
    if i==4:
        break
    print(i,c)



cv2.imshow('Image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()```

Upvotes: 2

Views: 71

Answers (1)

Open AI - Opting Out
Open AI - Opting Out

Reputation: 24133

r.find(' ') will find the index of the first ' ' character in the row r.

Adding one to it gives the index of the character after the first space.

[:] is slice notation. So you're taking the slice of everything in the row after the first space.

Upvotes: 2

Related Questions