为什么for a in '123456789'这样写不对?
print [a*100 + b*10 + c for a in ‘123456789’for b in ‘0123456789’ for c in ‘0123456789’ if a == c]
print [a*100 + b*10 + c for a in ‘123456789’for b in ‘0123456789’ for c in ‘0123456789’ if a == c]
2019-03-04
'123456789' 用引号的是字符串,for a in '123456789' 迭代出来的是'1' '2' '3'...这样的字符串。
把前面a*100 b*10 改成 a b就好了
print [a + b + c for a in '123456789'for b in '0123456789' for c in '0123456789' if a == c]
但是结果是对称数的字符串,再把字符串转换为int类型就可以了
print [int(a + b + c) for a in '123456789'for b in '0123456789' for c in '0123456789' if a == c]
举报