- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的目标是从下面的字符串中提取覆盖每个给定索引范围的子字符串。
_string='the old school teacher is having a nice time at school'
index_list=[[0,1,2],[4,7,20]]
我的尝试:
1)>>> [[[_string[y] for y in x]for x in index_list]
[['t', 'h', 'e'], ['o', ' ', 'e']]
2)>>>[_string[x[0:]] for x in index_list]
TypeError: string indices must be integers, not list
第一次尝试仅提取与索引对应的字符,而第二次尝试导致 TypeError
。
期望的输出:
['the', 'old school teach']
关于如何获得所需输出的任何建议?谢谢。
最佳答案
_string='the old school teacher is having a nice time at school'
index_list=[[0,1,2],[4,7,20]]
print [_string[x[0]:x[-1]+1] for x in index_list]
是您要找的吗?您只需要第一个 (x[0]) 和最后一个 (x[-1]) 索引。如果您想要整个句子,也许您必须将 20 更改为 21。
关于python - 如何根据索引提取子串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22450222/
我正在尝试创建一个程序,其中字符串的前三个字符重复给定次数,如下所示: foo('Chocolate', 3) # => 'ChoChoCho' foo('Abc', 3) # => 'AbcAbcA
我有以下字符串: std::string str = "Mode:AAA:val:101:id:A1"; 我想分离一个位于 "val:" 和 ":id" 之间的子字符串,这是我的方法: std::st
DNA 字符串可以是任意长度,包含 5 个字母(A、T、G、C、N)的任意组合。 压缩包含 5 个字母(A、T、G、C、N)的 DNA 字母串的有效方法是什么?不是考虑每个字母表 3 位,我们可以使用
是否有一种使用 levenstein 距离将一个特定字符串与第二个较长字符串中的任何区域进行匹配的好方法? 例子: str1='aaaaa' str2='bbbbbbaabaabbbb' if str
使用 OAuth 并使用以下函数使用我们称为“foo”(实际上是 OAuth token )的字符串加密 key public function encrypt( $text ) { // a
我是一名优秀的程序员,十分优秀!