空闲设置更改,注释键更改

2024-03-28 17:01:57 发布

您现在位置:Python中文网/ 问答频道 /正文

这不是一个编程问题,而是一个关于空闲时间的问题。是否可以将注释块键从“#”更改为其他内容?你知道吗

以下是不起作用的部分:

    array = []
    y = array.append(str(words2)) <-- another part of the program
    Hash = y.count(#) <-- part that won't work
    print("There are", Hash, "#'s")

Tags: ofthe内容编程时间anotherhashprogram
1条回答
网友
1楼 · 发布于 2024-03-28 17:01:57

不,这不是语言的一部分。你知道吗

编辑:我很确定你想用

y.count('#') # note the quotes

记住Python的一个优点是可移植性。编写一个只与自定义版本的解释器一起工作的程序将消除这种语言的优势。你知道吗

作为一个经验法则,任何时候你发现自己认为解决办法是重写部分语言,你可能朝着错误的方向前进。你知道吗

您需要对字符串而不是列表调用count:

array = []
    y = array.append(str(words2)) <  another part of the program
    Hash = y[0].count('#')  # note the quotes and calling count on an element of the list not the whole list 
    print("There are", Hash, "#'s")

带输出:

>>> l = []
>>> l.append('#$%^&###%$^^')
>>> l
['#$%^&###%$^^']
>>> l.count('#')
0
>>> l[0].count('#')
4

count正在查找完全匹配的'#$%^&###%$^^'!='#'。您可以在如下列表中使用它:

>>> l =[]
>>> l.append('#')
>>> l.append('?')
>>> l.append('#')
>>> l.append('<')
>>> l.count('#')
2

相关问题 更多 >