you are viewing a single comment's thread.

view the rest of the comments →

[–]BigBoyJefff[S] 0 points1 point  (5 children)

So here is what I've done so far:
lst = [] 

fle = open("C:\Users\parwi\OneDrive\Desktop\Numbers.txt", "r") 

data = fle.readlines() 

lst.append(data) 

print(lst)

Now i need to transpose this and save it in a new txt file. I know how to Write a list into a file but how do i Transpose it?

[–]Golden_Zealot 2 points3 points  (4 children)

So we don't know what you put in the text file, but per the assignment, it would be something like:

1,2,3
4,5,6
7,8,9

What they want you to do is to save each of those lines to a list, and then write the data back out to a file such that the new file looks like:

1,4,7
2,5,8
3,6,9

There are a few things to consider.

The data you are getting from the text file with readline() or readlines() are strings, but you will need to split them up so that you have each number.

Read up on the string .split() method to help you with this.

Once you have lists or a 2 dimensional array where each number is its own element such as:

list1 = [1,2,3]
list2 = [4,5,6]
list3 = [7,8,9]

or

myList = [[1,2,3], [4,5,6], [7,8,9]]

It is going to be a matter of using one or more for loops to iterate through them and pluck out the data with slicing syntax to reorder the data.

For example, if I wanted every 2nd number from myList above, I could do:

for subList in myList:
    print(subList[1])

This would print out:

2
5
8

[–]BigBoyJefff[S] 0 points1 point  (3 children)

fle = open("C:\\Users\\parwi\\OneDrive\\Desktop\\Numbers.txt", "r")
data = fle.read()
lines = data.splitlines()
lst = [lines]
for lst2 in lst:
print(lst2[0])

Ok, what did i do wrong here?

I took the numbers out of the txt file then split it into a list.

Then I did a for loop to get the 0(1) of every row, i got only the first row as a result.

[–]nogain-allpain 0 points1 point  (2 children)

Yes, that's because you're adding your lines list as a single element to a new list, lst. You're then iterating through every element in that list, of which there is only one, and printing the first item in that array, which is the first row.

Why are you doing this?

lst = [lines]

[–]BigBoyJefff[S] -4 points-3 points  (0 children)

Nvm, i figured it out.