Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x Python中的搜索子字符串_Python 3.x_Pycharm - Fatal编程技术网

Python 3.x Python中的搜索子字符串

Python 3.x Python中的搜索子字符串,python-3.x,pycharm,Python 3.x,Pycharm,我正在学习Python编程的基础知识。程序应打印定义列表中包含子字符串“ba”的所有字符串。程序没有提供预期的输出。我已经分析了代码并尝试了所有可能的更改,但没有成功。请检查并帮助我解决问题 提前谢谢 """list""" ls = ["black", "back", "bag", "bleach", "biba", "adabas"] el = len(ls) ind1 = 0 sublen = len(ls[ind1]) ind2 = 0 cha1 = "b" cha2 = "a" """

我正在学习Python编程的基础知识。程序应打印定义列表中包含子字符串“ba”的所有字符串。程序没有提供预期的输出。我已经分析了代码并尝试了所有可能的更改,但没有成功。请检查并帮助我解决问题

提前谢谢

"""list"""
ls = ["black", "back", "bag", "bleach", "biba", "adabas"]
el = len(ls)
ind1 = 0
sublen = len(ls[ind1])
ind2 = 0
cha1 = "b"
cha2 = "a"

"""printing vals to see if they r not giving any error"""
print(sublen)
print(ls[ind1])
print(ls[ind1][ind2])
print(ls[ind1][ind2 + 1])

"""while loop to get the strings having 'ba' in them"""
while ind1 < el:
    while ind2 < sublen:
        if cha1 == ls[ind1][ind2] and cha2 == ls[ind1][ind2 + 1]:
            print(ls[ind1])
        ind2 += 1
    ind1 += 1
print("finished!!")
“列表”
ls=[“黑色”、“背部”、“袋子”、“漂白剂”、“比巴”、“阿达巴斯”]
el=len(ls)
ind1=0
次级n=len(ls[ind1])
ind2=0
cha1=“b”
cha2=“a”
“”“打印VAL以查看它们是否没有给出任何错误”“”
打印(转租)
打印(ls[ind1])
打印(ls[ind1][ind2])
打印(ls[ind1][ind2+1])
“”“while循环以获取包含“ba”的字符串”“”
而ind1
输出为:

back
bag
biba
adabas

非常感谢简化和更好的解决方案!!:)但是你能告诉我我的程序有什么问题吗。我在代码中找不到任何错误:|我刚才发现了错误。我没有在执行内部while循环时重置计数器var ind2。ls=[“back”,“black”,“bag”,“bleat”,“biba”,“adabas”]el=len(ls)ind1=0 sublen=len(ls[ind1])ind2=0 cha1=“b”cha2=“a”而ind1back bag biba adabas