需要从字符串中获取 "even" 索引处的单词

Need to get words at "even" indices from string

这里以字符串为例

s = 'asdf df d f d ssa'.

我需要从字符串中获取偶数索引处的单词。对于上面的字符串s,单词是:

1. 'asdf'
2. 'df'   // Even index
3. 'd'
4. 'f'    // Even index
5. 'd'
6. 'ssa'  // Even index

正确的输出应该是 'df f ssa'。我想我会用一片来做这个。

我该怎么做?

你的意思是 even 索引处的单词(如果听起来正确的话)。 split 然后 slice 从 1 开始,步长为 2:

>>> ' '.join(s.split()[1::2])
'df f ssa'

你的 even 在这里意味着 odd,因为索引从零开始。