0

I have some problems with parsing the correct way. I want to split the complete string in two seperate strings. And then remove the "="-signs frome the first string and the ","-sign from the 2nd string. From my output I can conclude that I did something wrong, but I do not seem to get where the problem lies. I want the first part to convert to integers, and I've already tried it with map(int, split()).

If anyone has a tip, I would appreciate that.

This is my output:

('5=20=22=10=2=0=0=1=0=1', 'Vincent Appel,Johannes Mondriaan')

This is my program:

mystring = "5=20=22=10=2=0=0=1=0=1;Vincent Appel,Johannes Mondriaan"

def split_string(mystring):
    strings = mystring.split(";")
    x = strings[0]
    y = strings[-1]
    print(x,y)

def split_scores(x):
    scores = x.split("=")
    score = scores[0]
    names = scores[-1]
    stnames(names)
    print score

def stnames(y):
    studentname = y.split(",")
    name = studentname[1]
    print name

split_string(mystring)
4
  • 1
    Why don't you use split_scores and stnames? Commented Nov 21, 2015 at 16:53
  • 2
    Which is your desired output? Commented Nov 21, 2015 at 16:53
  • It would help if you gave an example of what you want the output to look like. I can't figure out how the numbers are supposed to relate to the names. Commented Nov 21, 2015 at 17:34
  • @tdelaney: Hi my output should be: -^^--_-- For an integer equal to zero I want to print a "_" sign, for an integer < 20 I want to print a "-" sign and for integer >= 20 I want to print a "^" sign. Commented Nov 21, 2015 at 18:52

3 Answers 3

1

split_string(mystring) runs the 1st function, producing the tuple with 2 strings. But nothing runs the other functions which are intended to perform further splitting.

try:

x, y = split_string(mystring)
x1 = split_scores(x)
y1 = stnames(y)
(x1, y1)

oops, your functions print the results, don't return them. So you also need:

def split_string(mystring):
    # split mystring on ";"
    strings = mystring.split(";")
    return strings[0],strings[1]

def split_string(mystring):
    # this version raises an error if mystring does not have 2 parts
    x, y = mystring.split(";")
    return x,y

def split_scores(x):
    # return a list with all the scores
    return x.split("=")

def stnames(y):
    # return a list with all names
    return y.split(",")

def lastname(y):
    # return the last name (, delimited string)
    return y.split(",")[-1]

If you are going to split the task among functions, it is better to have them return the results rather than print them. That way they can be used in various combinations. print within a function only for debugging purposes.

Or a compact, script version:

x, y = mystring.split(';')
x = x.split('=')
y = y.split(',')[-1]
print y, x

If you want the scores as numbers, add:

x = [int(x) for x in x]

to the processing.

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

4 Comments

It is inefficient to define a new function that calls one built-in function. Instead use the built-in function directly.
I'm more concerned about clarity - as seen by humans - than with efficiency.
Hi thank you, the compact version has worked for me. The output is what I wanted for now. Now I only have to convert the scores to int to use the scores further to make a graph: So, if score = 0 then print a '_'-sign, if score < 20 then print a '-'-sign and if score >= 20 print a '^'-sign. But for now I'm glad that the solution you provided has worked out for me.
See my addition of a list comprehension to convert x to a list of ints.
0

Try this:

def split_string(mystring):
    strings = mystring.split(";")
    x = int(strings[0].replace("=",""))
    y = strings[-1].replace(","," ")
    print x,y

Comments

0

My two cents.

If I understood what you want to achieve, this code could help:

mystring = "5=20=22=10=2=0=0=1=0=1;Vincent Appel,Johannes Mondriaan"

def assignGradesToStudents(grades_and_indexes, students):
    list_length = len(grades_and_indexes)
    if list_length%2 == 0:
        grades = grades_and_indexes[:list_length/2]
        indexes = grades_and_indexes[list_length/2:]
        return zip([students[int(x)] for x in indexes], grades)

grades_and_indexes, students = mystring.split(';')
students = students.split(',')
grades_and_indexes = grades_and_indexes.split('=')

results = assignGradesToStudents(grades_and_indexes, students)
for result in results:
    print "[*] {} got a {}".format(result[0], result[1])

Output:

[*] Vincent Appel got a 5
[*] Vincent Appel got a 20
[*] Johannes Mondriaan got a 22
[*] Vincent Appel got a 10
[*] Johannes Mondriaan got a 2

Comments

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.