0
def main():

    my_list = [[float(i) for i in line.split(',')] for line in open("Alpha.txt")]
    print(my_list)
    for elem in my_list:
        listA=[]
        listA = elem
        print(listA)


main()

this code prints out the correct data of which im looking for, however i need to set each print from the for loop into a object. Any help as to how i would go about doing that?

[1.2, 4.3, 7.0, 0.0]
[3.0, 5.0, 8.2, 9.0]
[4.0, 3.0, 8.0, 5.6]
[8.0, 4.0, 3.0, 7.4]
3
  • 1
    What kind of object?? a list is an object. Commented Oct 23, 2013 at 17:51
  • 1
    Do you want to put each list in a different variable? Commented Oct 23, 2013 at 17:53
  • yes sorry for the confusion, i want each line to become a different variable like a = [1.2, 4.3, 7.0, 0.0] b = [3.0, 5.0, 8.2, 9.0] and so on Commented Oct 23, 2013 at 17:54

3 Answers 3

2

What you're thinking of/trying to do is to dynamically name variables.

Don't.

Either leave your data in the list and access it via index

my_list[0] #what you were trying to assign to 'a'
my_list[0][0] #the first element in that sub-list

Or, if you have meaningful identifiers that you want to assign to each, you can use a dict to assign "keys" to "values".

d = {}
for sublist, meaningful_identifier in zip(my_list, my_meaningful_identifiers):
    d[meaningful_identifier] = sublist

Either way, leverage python data structures to do what they were supposed to do.

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

2 Comments

You may want to consider having your meaningful_identifiers contained within the input file and create a dict as roippi suggests
you are so correct, feel very dumb for not recognizing i could just use the my_list[]. question though, if the data in alpha.txt was changed with more or less data, how would i have a dynamic code to ensure all of the data is read correctly?
1

This is not a good idea, let me warn you, and you should never use this in production code (it is prone to code injection), and screws up your global namespace, but it does what you asked.

You would use exec() for this, which is a function that dynamically executes statements.

def main():
    my_list = [[float(i) for i in line.split(',')] for line in open("Alpha.txt", "r")]
    print(my_list)
    for elem in my_list:
        exec "%s = %s" % ("abcdefghijklmnopqrstuvwxyz"[my_list.index(elem)], elem) in globals()

main()

Now, your global namespace is filled with variables a, b, c, etc. corresponding to the elements.

It is also prone to exceptions, if you have more than 26 elements, you will get an IndexError, although you could work around that.

1 Comment

yeah the a, b, c , d are very stupid names i agree. in my final code i would set the to something much more meaningful and less confusing such as my_list_first my_list_second and such. Thanks for the breakdown of exec()!
1

Try:

myList = [map(float, line.split(',')) for line in open ("Alpha.txt")]

Now you can get each line in a different variable if you want:

a = myList[0]
b = myList[1]

and so on. But since you have a list, it's better to use it and access elements using indices. Are you sure have a correct understanding of arrays?

As the other answers point out, it is dangerous and doesn't make sense to dynamically create variables.

1 Comment

i havent yet learned about arrays fully, but your code makes complete sense.

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.