压痕在这里搞砸了,但我想计算次数"。"出现在迷宫中并使剩余的位数但.count似乎没有效果。当我使用bitsLeft变量时,它保持为0而不是计算次数"。"出现在数组
中level_1 = [
"$$$$$$$$$$$$$$$$$$$$$$$$$",
"$!0......>0........#0>..$",
"$.0000.00000.0000000000.$",
"$...00.0#....0!00.0.000.$",
"$00.00.00000.0.00.0.000.$",
"$#0..........0..0.0.....$",
"$.0000.00000.00.0.0.000.$",
"$......0........0.0.0...$",
"$.000000.00000000...0.0.$",
"$.0......0...0...00.0.0.$",
"$...000000>0...0....0.0.$",
"$00.000000000000000.0.0.$",
"$...0.....0000!0........$",
"$.000.000.0>#0.0.00000.0$",
"$.....0.0.0..0.0.0..0..0$",
"$.00000.0.0..0.0.0..0.0.$",
"$.0>....0.0000.0.0..0.0.$",
"$.00000.0...P..0.0000.0.$",
"$....00.00000000.0....0.$",
"$000.00..........0.0000.$",
"$!0..00.00000000.0......$",
"$.0.000..........0.0000.$",
"$.0.0000.0000000.0.0>00.$",
"$........0#........0....$",
"$$$$$$$$$$$$$$$$$$$$$$$$$"
]
bitsLeft = level_1.count ('.')
答案 0 :(得分:3)
您可以将sum
与获取每个字符串计数的列表推导结合起来,如下所示:
sum(s.count(".") for s in level_1)
结果:
265
答案 1 :(得分:1)
string.count()需要一个字符串。您可以使用''.join(level_1)
连接str.count()
方法的字符串:
bitsLeft = ''.join(level_1).count ('.')