简体   繁体   中英

finding substring in python from MySQL research

First of all, i'm searching a way to implement every character in a tuples, and then add those number.

right now, my codes looks like this :

def separate(s):
    result = [0, 0]
    for c in s:
        if c == "0":
            result[0] = result[0] + 1
        elif c == "1":
            result[1] = result[1] + 1
        else:
            raise "invalid sequence."
        return result

s = "01010101101010"

zero, one = separate(s)

print(zero)
print(one)

i dont quite understand why the answer it gives me is only the 2nd valor of my s(which is a 1) and 0 for my (one)

My second question is : I'm trying to get a list from MySQL but the column doesnt exist, i made it myself ( so substring(prediction, startpos,endpos-startpos) I'm asking if there is a way for me to get this substring in my code (instead of the s='101011101';

Thanks. i hope i'm clear enough

You are returning in every iteration of the for loop (in other words: you are returning in the first iteration). Unindent the "return" line one level.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM