简体   繁体   English

计算列表python中的空格数

[英]counting a number of spaces in a list python

I am making a text based game, and you can place a ship piece off the game board (not a intended feature). 我正在制作基于文本的游戏,您可以将飞船放在游戏板上(不是预期的功能)。 I have been searching the web for a way to count 2 characters into a list and see if it is a certain character (so i can tell if someone placed off the board), but so far i have not found any solution. 我一直在网上寻找一种方法来将2个字符计数到一个列表中,并查看它是否是某个字符(这样我就可以确定是否有人将其放置在板上),但是到目前为止,我还没有找到任何解决方案。 Anyone have any ideas? 有人有想法么?

It would be more helpful to have an example of what your list looks like, but here's one possible solution. 举例说明您的列表看起来会更有用,但这是一种可能的解决方案。

Assuming you have a list of X's and O's like so: 假设您有一个X和O的列表,如下所示:

example_list = ['X', 'O', 'O', 'X']

Now: 现在:

# Check if the second character is 'O'
if example_list[1] == 'O':
    # Do something
else:
    # Do something else

Note that this would work the same way with the string "XOOX" , ie 注意,这与字符串"XOOX"工作方式相同 ,即

>>> example_string = "ABCD"
>>> print example_string[1]
B
>>> example_string[1] == "B"
True
>>> example_string[1] == "X"
False

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

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