我想找到字符串中第一次出现任何"特殊"字符的索引,如下所示:
>>> "Hello world!".index([' ', '!'])
5
Run Code Online (Sandbox Code Playgroud)
...除了那些无效的Python语法.当然,我可以编写一个模拟此行为的函数:
def first_index(s, characters):
i = []
for c in characters:
try:
i.append(s.index(c))
except ValueError:
pass
if not i:
raise ValueError
return min(i)
Run Code Online (Sandbox Code Playgroud)
我也可以使用正则表达式,但这两种解决方案似乎都有点矫枉过正.在Python中有没有"理智"的方法来做到这一点?
在Emacs 24.4中,默认缩进行为已更改 - 现在新行自动缩进.从发行说明:
*** `electric-indent-mode' is now enabled by default.
Typing RET reindents the current line and indents the new line.
`C-j' inserts a newline but does not indent. In some programming modes,
additional characters are electric (eg `{').
Run Code Online (Sandbox Code Playgroud)
我更喜欢旧的行为,所以我补充道
(electric-indent-mode 0)
Run Code Online (Sandbox Code Playgroud)
到我的.emacs档案.但是,这会禁用所有电子角色,这不是我想要的.
是否有任何方法可以禁用新行为,同时仍然有像'{'或':'这样的字符触发缩进?