0

Alright, so i got this question that im given a group of scores in the file, and i have to make them into a list, sort the list, and find the median. I got the sorting and the median done..

But I just got one problem that I can't seem to figure out. Here is my code:
I'm new to python..i know i can use sort() and everything..but is there a way to solve the problem with the code i used?:)

#Text file in another file:
score.txt
10,11,09,10,22,22,20,01
def elements(score_file):
    line = score_file.readline().strip()
    score = line.split(",")
    
    while line != "":
        line = score_file.readline().strip()
        score = score + line.split(",")
    
    print("Original list:")   
    print(score)
    return score
#The ouput i get for the orginal list comes up to(How do i get rid of the '' at the end?):
Original list:
['10', '11', '09', '10', '22', '22', '20', '01', '']

THANKS ALOT GUYS!!

Edited by dragonstear: n/a

3
Contributors
7
Replies
8
Views
6 Years
Discussion Span
Last Post by dragonstear
Featured Replies
  • 1

    [CODE]def elements1(score_file): return [int(score) for score_line in open(score_file) for score in score_line.split(",")] #cleaner def elements2(score_file): score = [] for line in score_file: score = score + line.rstrip().split(",") return score #minimal change def elements(score_file): line = score_file.readline().strip() score = line.split(",") while line != "": line = score_file.readline().strip() if line: score = … Read More

0

I know, but lets assume that its not allowed:(

All you need to do is call the sort() and pop() methods.

0

You'll need to write custom functions. If list comprehensions are allowed you could easily iterate through to all but the last item, then write a bubblesort or quicksort. If not comprehensions you're missing a key factor to Python.

0

I got the sorting and the median done..

I do not see any sorting and median, so I am confused of what you want.

def elements(score_file):
    return [int(score)
            for score_line in open(score_file)
            for score in score_line.split(",")]

print sorted(elements('score.txt'))

Edited by pyTony: n/a

0

thats in two other functions. not really important
but i still can't get rid of the blank '' at the end of the list

I do not see any sorting and median, so I am confused of what you want.

def elements(score_file):
    return [int(score)
            for score_line in open(score_file)
            for score in score_line.split(",")]

print sorted(elements('score.txt'))

Edited by dragonstear: n/a

1
def elements1(score_file):
    return [int(score)
            for score_line in open(score_file)
            for score in score_line.split(",")]

#cleaner
def elements2(score_file):
    score = []
    for line in score_file:
        score = score + line.rstrip().split(",")
    return score

#minimal change
def elements(score_file):
    line = score_file.readline().strip()
    score = line.split(",")

    while line != "":
        line = score_file.readline().strip()
        if line: score = score + line.split(",")

    return score

print(sorted(elements1('score.txt')))
print(elements2(open('score.txt')))
print(elements(open('score.txt')))

Edited by pyTony: n/a

Comments
thanks alot!
0

Thank you so much!!!

def elements1(score_file):
    return [int(score)
            for score_line in open(score_file)
            for score in score_line.split(",")]

#cleaner
def elements2(score_file):
    score = []
    for line in score_file:
        score = score + line.rstrip().split(",")
    return score

#minimal change
def elements(score_file):
    line = score_file.readline().strip()
    score = line.split(",")

    while line != "":
        line = score_file.readline().strip()
        if line: score = score + line.split(",")

    return score

print(sorted(elements1('score.txt')))
print(elements2(open('score.txt')))
print(elements(open('score.txt')))
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.