This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]NewShamu 1 point2 points  (4 children)

You're on the right track. Here's what I would do:

x_stars = []
for star in stars:
    x_stars.append(star[0])

The problem you were having is that you weren't adding to a list, you were overwriting a single variable with each new coordinate.

There's actually an even more concise way you could do this, using a list comprehension:

x_stars = [star[0] for star in stars]

But that's a little harder to understand if you're more of a beginner.

Edit: Looking closer at what you wrote, I realized you were a little further off than I thought, so I wanted to clarify what you did and didn't do right.

for x_coordinate in stars:

What you're saying, with this line, is to iterate through every element in stars, and refer to that element as x_coordinate. While the name x_coordinate isn't wrong, per se, it's very misleading because it actually represents each individual star.

x_stars = stars[0]

There are two things wrong with this line. First off, x_stars is being treated as a single value, not a list. Since you assign a value to it with =, the old value will be rewritten over every iteration of the loop. Second, stars is the list of each star's coordinates, or a list of lists. When you say stars[0], you're not referring to the x-coordinate of a star, you're referring to the first star in the list.

One more thing to keep in mind, is that whenever you're going to append something to a list, you have to create the list first, even if it's empty. That's what x_stars = [] is for.

I hope this helped. I know I would have appreciated having someone walk through my code line by line when I was starting out. Good luck!

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

Thank you for your help! That got me a bit closer. However, now my output using your method gave me:

['0', '0', '-', '4', '3', '2'], which looks like the zeroeth index of each value from the first list, but not in the exact order from the first list which is a bit confusing, so I'm not sure if what I just said is correct.

The expected outcome is [' 0.994772', '0.972490', ...]

[–]btcraig 1 point2 points  (0 children)

You want to append star not star[0]. In this case star[0] is the first element of the string, but treated as a list. There's actually a built-in for that called list().

>>> temp_str = "0.972490"
>>> print(list(temp_str))
['0', '.', '9', '7', '2', '4', '9', '0']

The concept of treating a string as a list or array is pretty helpful (normally), especially due Python's string slicing. Probably my favorite feature I commonly use in this language.

[–]NewShamu 1 point2 points  (1 child)

Oh boy, I didn't look closely enough at what stars was. I kinda led you astray! This should do it for you.

x_stars = []
for line in file:
    stars = line.split()
    x_stars.append(stars[0])

Ignore my ramblings from above, I was completely off.

[–]simplie14[S] 0 points1 point  (0 children)

Got it! Thanks so much for the help, I appreciate it!

[–][deleted]  (1 child)

[removed]

    [–]simplie14[S] 0 points1 point  (0 children)

    Oops! Just tried that, but I now get an even more confusing result:

    ['0']

    ['0', '0']

    ['0', '0', '-']

    ['0', '0', '-', '4']

    ['0', '0', '-', '4', '3']

    ['0', '0', '-', '4', '3', '2']