1

The code below is producing this error:

Traceback (most recent call last):
  File "pulllist2.py", line 28, in <module>
    list_sym[w] = symbol
IndexError: list assignment index out of range

What I am trying to achieve here is only add an element to the array if word1 and word2 are in the string of security_name. I do not know another way to initialize the arrays either.

#!/usr/bin/env python3
import glob
import os
import sys
import fnmatch

list_sym = []
list_name = []
w = 0
count = 0
word1 = 'Stock'
word2 = 'ETF'

# for loop that parses lines into appropriate variables
for file in glob.glob('./stocklist/*.txt'):
with open(file) as input:

    for line in input:
        # split line into variables.. trash will have no use
        symbol, security_name, trash = line.split('|', 2)

        if word1 in security_name or word2 in security_name: 
            # stores values in array
            list_sym.append(1) ## initialize arrays
            list_name.append(1)
            list_sym[w] = symbol
            list_name[w] = security_name
            count += 1

        w += 1
2
  • Why use a parallel list when you can use a dictionary? And you've already initialized your arrays at the start. As well, your for loop isn't right. You can't access line in a file object. Commented May 14, 2015 at 2:37
  • Looking into dictionary's right now, thanks for the tip. Commented May 14, 2015 at 2:39

3 Answers 3

2

You've initialized your lists here, albeit an empty list:

list_sym = []

This is fine, however, what happens when you add to the list? You can simply use the append method.

list_sym.append(whatever)

It looks like you come from the old languages (Java or C or C++). Everything with braces and fixed array lengths and boring stuff like that. You don't have to index everything you do.

appending a list, is simply add to the list. You can add anything to a list.

list_sym.append(5345034750439)
list_sym.append("Yo. What's up!")

This would be safer for you, as you can just add, and not try and get a position. Appending is the way to go.

To find the length:

>>> len(list_sym)
10 # There's 10 objects in the list.

Let me recommend you the dictionary, like I said:

Dictionaries allow you to pair up objects together, simply like assigning variables. It's as simple as this:

my_dictionary = {}

To add an object, you need your key and your value.

my_dictionary["name"] = "Zinedine"
                ^           ^
               Key         Value

To access the value, you need to know the key (How would you get the treasure without the golden key). Like you were trying to index the list, you get the value by the same concept. Dictionaries are commonly used to pair up objects, or to store multiple variables under a common scope. I hope you find it useful! :)

Sign up to request clarification or add additional context in comments.

1 Comment

You are right, I come from old languages apparently.. Python is amazing :D Thanks for the help, very well explained.
0

You cannot do this in python. Try instead

list_sym.append(symbol)
list_name.append(security_name)

2 Comments

Okay so far, this is what I wanted it to do. So how would I see the number of elements in that list_sym? Or the length of it.
To find the length, you can just do this: len(list_sym). I highlighted that in my answer now too. :)
0

I think the code is unnecessarily complicated. I gather you are trying to 'associate' symbols with security names and also want symbols and security names as lists separately. A regexp might be a good idea here.

import re
r = re.compile(r'Stock|ETF')
list_sym = {} # NOTE: A dict - keys symbols, values security names
for file in glob.glob('./stocklist/*.txt'):
    with open(file) as input:

        for line in input:
        # split line into variables.. trash will have no use
        symbol, security_name, trash = line.split('|', 2)

        if r.match(security_name): # something matches 
            # stores values in array
            list_sym[symbol.lower()] = security_name # just to be safe

symbols = list_sym.keys()
security_names = list_sym.values()

This broadly explains the idea. Some edits may be required to try what you are trying to achieve.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.