我正在学习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!!")
答案 0 :(得分:0)
ls = ["black", "back", "bag", "bleach", "biba", "adabas"]
for item in ls:
if 'ba' in item:
print(item)
输出结果为:
back
bag
biba
adabas