小编use*_*575的帖子

如何查找字符串中任何一组字符的第一个索引

我想找到字符串中第一次出现任何"特殊"字符的索引,如下所示:

>>> "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中有没有"理智"的方法来做到这一点?

python string indexing

16
推荐指数
3
解决办法
6070
查看次数

如何在RET上禁用电动缩进但仍保留其他电子字符(例如"{")?

在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档案.但是,这会禁用所有电子角色,这不是我想要的.

是否有任何方法可以禁用新行为,同时仍然有像'{'或':'这样的字符触发缩进?

emacs indentation auto-indent cc-mode emacs24

3
推荐指数
1
解决办法
934
查看次数

标签 统计

auto-indent ×1

cc-mode ×1

emacs ×1

emacs24 ×1

indentation ×1

indexing ×1

python ×1

string ×1