0

Im trying to print a list with all the positions of the word "From" on a text:

name = input("Enter file: ")
if len(name) < 1 : name = "mbox-short.txt"
h= open(name)
r= h.read()
r= r.split()
l= []


for i in r:
    if i == "From":
        x= enumerate(i)
        l.append(x)
print(l)

I have like a week of experience with Python so i read that enumaerate was a good option but the result is

Enter file: 
[<enumerate object at 0x000001737D5B59A8>, <enumerate object at 0x000001737D5B59F8>, <enumerate object at 0x000001737D5B5A48>, <enumerate object at 0x000001737D5B5A98>, <enumerate object at 0x000001737D5B5AE8>, <enumerate object at 0x000001737D5B5B38>, <enumerate object at 0x000001737D5B5B88>, <enumerate object at 0x000001737D5B5BD8>, <enumerate object at 0x000001737D5B5C28>, <enumerate object at 0x000001737D5B5C78>, <enumerate object at 0x000001737D5B5CC8>, <enumerate object at 0x000001737D5B5D18>, <enumerate object at 0x000001737D5B5D68>, <enumerate object at 0x000001737D5B5DB8>, <enumerate object at 0x000001737D5B5E08>, <enumerate object at 0x000001737D5B5E58>, <enumerate object at 0x000001737D5B5EA8>, <enumerate object at 0x000001737D5B5EF8>, <enumerate object at 0x000001737D5B5F48>, <enumerate object at 0x000001737D5B5F98>, <enumerate object at 0x000001737D61F048>, <enumerate object at 0x000001737D61F098>, <enumerate object at 0x000001737D61F0E8>, <enumerate object at 0x000001737D61F138>, <enumerate object at 0x000001737D61F188>, <enumerate object at 0x000001737D61F1D8>, <enumerate object at 0x000001737D61F228>]

When i want something like this:

Enter file: 
[2,5,6,8,11]

does anyone know what function to use instead of enumerate. Thanks and sorry if this is an easy question.

Allan
  • 1
  • 1
  • Please go back to the places where you "read that enumerate was a good option", and check the code examples more carefully. – Karl Knechtel May 10 '20 at 00:10

2 Answers2

0

What enumerate gives you is a sequence of each item and its index. So you don't want to enumerate each individual item, you want to enumerate the list and use that to get the index:

for x, i in enumerate(r):
    if i == "From":
        l.append(x)

Since I'm looking at this code anyway, I'll give some suggestions on good habits to start building as you start out:

  1. When you want to use a "default" as an alternative for when something is empty or unset, you can usually use or (i.e. use this value if it's "truthy"/non-empty, otherwise use this other value).
  2. If you're only going to use a value to generate some other value, consider not assigning the intermediate step to a named variable; you can usually make things more concise by just chaining operations together. For example, you probably wouldn't do:
    x1 = 2 + 3
    x2 = x1 + 4
    x = x2 + 5
    # do thing with x

when you could just do:

    x = 2 + 3 + 4 + 5
    # do thing with x
  1. When you do name a value, give it a name that indicates something about what it is! Doesn't have to be long, but it should be at least enough that if you have three different variables you can easily keep track of which is which.
  2. When you open a file in Python, it's usually better to use with open... to open the file in a "context"; the file will automatically close() at the end of the block.
  3. When you're building a list using a relatively simple operation, it's usually better to do it in a comprehension ([x for x in ...]) than to append inside a for loop. It's more concise to read and it's sometimes more efficient as well.

All that said, here's your script in five lines (two lines if you squished it up a bit, but I like breaking up comprehensions with linebreaks):

with open(input("Enter file: ") or "mbox-short.txt") as file:
    print([
        i for i, word in enumerate(file.read().split())
        if word == "From"
    ])
Samwise
  • 68,105
  • 3
  • 30
  • 44
0

Here is a shorter version:

indices = [i for i, x in enumerate(my_list) if x == "whatever"]

(From here)

What this means:

i for i, x in enumerate(my_list)

Every index i out of index, value in an enumerated version of your list.

if x == "whatever"

Yes, every index in your list but only if the value x is equal to whatever value you want.

applemonkey496
  • 683
  • 1
  • 10
  • 29