简体   繁体   English

从MySQL研究中找到python中的子字符串

[英]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) 我不太明白为什么它给我的答案只是我s的第二个勇气(即1)和我的(一个)0

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'; 我的第二个问题是:我试图从MySQL获取列表,但该列不存在,我自己创建了它(所以substring(prediction,startpos,endpos-startpos))我在问是否有一种获取方法我的代码中的此子字符串(而不是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). 您将在for循环的每个迭代中返回(换句话说:您将在第一次迭代中返回)。 Unindent the "return" line one level. 取消缩进“返回”行一级。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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