The way you ask the questions you ask indicates to me that you aren't exactly clear on what you want your program to do eventually.
First, that line doesn't just convert the file to a list. It converts it to a list of tuples. Each tuple is a set of space separated values for one line in the file.
I will answer your questions as given, but I expect some of the answers may confuse you even more.
1 the file it opens is dynamic rather than static? i.e. the user chooses the file to open.
"cluster2.wcf"
doesn't have to be a string. It can be a variable. If one is to read your question very literally, here is how you would modify the code to do it:
filename = raw_input("Which file would you like to read: ")
l = [s.strip().split() for s in open(filename).readlines()]
A point to note here. After you read the file into the list, the list is going to look like this:
[('This', 'is', 'the', 'first', 'line.'),
('This', 'is', 'the', 'second', 'line.'),
('And', 'finally', 'the', 'last', 'line.')]
Each element of your list is a further list of all the 'words' on that line. Words being defined as space separated groups of characters.
2 Select specific lines to read after it has been converted to a list.
This question is a confusing question. You have already read the file. Do you want to read it again? What I am guessing you actually mean is "How do I work with a specific line of the file after I've read it into a list?". I will answer this question.
Each line of the file will correspond to a particular element of the list. List elements are accessed by numbers starting with 0. The first line of your file will be in l[0]
. The second in l[1]
and so on up to (and this is a bit of Python magic here) l[-1]
. Negative indexes in Python count backwards from the end of the list.
3 assign objects to values in the list
This one is confusing because you don't specify what kind of objects you want to assign or what you want to do with the list after you assign objects to values in it. Here is an answer that takes your question very literally...
l[0] = object()
That assigns an object to the list element that used to contain a tuple representing the first line of your file. Combining this with the previous answer I am betting you can figure out how to assign an object to any element of your list.
4 select the first, last or a number of values in each line?
This depends on exactly how you want to select them. If you want to do it individually you do it this way:
first_word_of_first_line = l[0][0]
last_word_of_first_line = l[0][-1]
first_word_of_last_line = l[-1][0]
last_word_of_last_line = l[-1][-1]
first_five_words of_second_line = l[1][0:5] # The : is for specifying a range of values
last_five_words_of_first_line = l[0][-5:] # The : with no end means 'until the end'
Unfortunately, the way to get the first 4 words of the first 2 lines is not this:
first_four_words_of_first_two_lines = l[0:2][0:4] # DOESN'T WORK!!!
The reason for that is that the result of fetching a range of values from a list is a list, and so the [0:4]
applies to that list instead. What you really mean to do is to apply the [0:4]
to apply to each element of the resulting list to create a new list. Here is how you would do that:
first_four_words_of_first_two_lines = [x[0:4] for x in l[0:2]]
That gives you a new list that looks like this (given the example for question 1):
[('This', 'is', 'the', 'first'),
('This', 'is', 'the', 'second')]
I hope that is helpful. As I said, I think your questions indicate some confusion about things. But I don't know what they indicate confusion about, so I just tried to answer them as best I could.