Python2.7-迭代字符串索引并仅打印偶数或奇数索引值时输出不正确
当遍历字符串并仅打印偶数字符串索引的值时,Python正在打印string.index(3)处的值,尽管我要求它仅在索引为偶数时打印值。我想知道为什么会发生这种情况。以下是我一直使用的代码:Python2.7-迭代字符串索引并仅打印偶数或奇数索引值时输出不正确,python,indexing,Python,Indexing,当遍历字符串并仅打印偶数字符串索引的值时,Python正在打印string.index(3)处的值,尽管我要求它仅在索引为偶数时打印值。我想知道为什么会发生这种情况。以下是我一直使用的代码: my_string = 'Hello, World!' for i in my_string: if my_string.index(i) % 2 == 0: print i else: print my_str
my_string = 'Hello, World!'
for i in my_string:
if my_string.index(i) % 2 == 0:
print i
else:
print my_string.index(i)
当我们运行代码时,它应该返回:
H
1
l
3
o
5
7
o
9
l
11
!
但是,Python正在返回:
H
1
l
l
o
5
7
o
9
l
11
!
如您所见,返回的是索引[3]处的“l”,而不是3
我使用的每个解释器似乎都是这样,所以我假设这是Python的问题。有办法解决这个问题吗?这是发生在其他人身上的吗?如果有人正在编写一个需要准确分离偶数和奇数索引的程序,这似乎是一个非常大的问题 这是因为
.index
返回第一个匹配字符的索引(或第一个匹配子字符串的开头),而不是当前迭代的位置。由于“Hello”
有2个l
字符,因此my_string.index('l')
将始终返回2,并且作为偶数,打印字符而不是索引
您需要的是,它为给定iterable中的每个项目生成成对的索引,item
:
my_string = 'Hello, World!'
for index, char in enumerate(my_string):
if index % 2 == 0:
print(char)
else:
print(index)
这是因为
.index
返回第一个匹配字符的索引(或第一个匹配子字符串的开头),而不是当前迭代的位置。由于“Hello”
有2个l
字符,因此my_string.index('l')
将始终返回2,并且作为偶数,打印字符而不是索引
您需要的是,它为给定iterable中的每个项目生成成对的索引,item
:
my_string = 'Hello, World!'
for index, char in enumerate(my_string):
if index % 2 == 0:
print(char)
else:
print(index)
.index
不会执行您认为它会执行的操作-它会返回第一个索引。就您而言,您有:
Hello... # text
01234 # indices
l
的第一个索引是2
。它是均匀的,不显示。您想使用的是枚举
:
my_string = 'Hello, World!'
for i, char in enumerate(my_string):
if i % 2 == 0:
print char
else:
print i
如果你会看到:
s、 索引(x)| s中第一次出现x的索引
您也可以这样尝试:
Python 2.7.10 (default, Oct 23 2015, 19:19:21)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.
Type "help", "copyright", "credits" or "license" for
>>> help('hello'.index)
Help on built-in function index:
index(...)
S.index(sub [,start [,end]]) -> int
Like S.find() but raise ValueError when the substring is not found.
(这应该引导你找到
>>> help('hello'.find)
Help on built-in function find:
find(...)
S.find(sub [,start [,end]]) -> int
Return the lowest index in S where substring sub is found,
such that sub is contained within S[start:end]. Optional
arguments start and end are interpreted as in slice notation.
Return -1 on failure.
S中的最低指数
在
'Hello,World!'
中,l
的最低索引是2。o
的最低索引是4。如果您将代码并将逻辑翻转到,如果i%2:
,那么您将看到第二个o
!。索引
不会做您认为它做的事情-它返回第一个索引例:在你的情况下,你有:
Hello... # text
01234 # indices
l
的第一个索引是2
。它是偶数,不显示。您要使用的是枚举
:
my_string = 'Hello, World!'
for i, char in enumerate(my_string):
if i % 2 == 0:
print char
else:
print i
如果你会看到:
s、 索引(x)| s中第一次出现x的索引
您也可以这样尝试:
Python 2.7.10 (default, Oct 23 2015, 19:19:21)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.
Type "help", "copyright", "credits" or "license" for
>>> help('hello'.index)
Help on built-in function index:
index(...)
S.index(sub [,start [,end]]) -> int
Like S.find() but raise ValueError when the substring is not found.
(这应该引导你找到
>>> help('hello'.find)
Help on built-in function find:
find(...)
S.find(sub [,start [,end]]) -> int
Return the lowest index in S where substring sub is found,
such that sub is contained within S[start:end]. Optional
arguments start and end are interpreted as in slice notation.
Return -1 on failure.
S中的最低指数
在
'Hello,World!'
中,l
的最低索引是2。o
的最低索引是4。如果您获取代码并将逻辑翻转到,如果i%2:
则您将看到第二次o
!index
返回其参数第一次出现的索引;l
在Hello,World!
中出现三次,但是my_string.index('l')
总是返回2
改用枚举
:
for i, c in enumerate(my_string):
if i % 2 == 0:
print c
else:
print i
index
返回其参数第一次出现的索引;l
在Hello,World!
中出现三次,但my_字符串。index('l')
始终返回2
改用枚举
:
for i, c in enumerate(my_string):
if i % 2 == 0:
print c
else:
print i
尝试在范围(len(my_string))上迭代,而不是在sting本身上迭代。完整的指令是: 对于范围内的i(len(我的字符串): 这将使每个数字从0步进到比字符串中的字符数少一个的位置。然后,您可以使用i对字符串进行切片以获得单个字符。您还可以使用另一个值或范围函数的其他输入值来指定从何处开始(默认值为0)和步长(默认值为1)。
至于其余部分,请在if语句中使用i%2==0,打印i以打印索引,打印my_string[i]以打印字符。请尝试在范围(len(my_string))上迭代,而不是在sting本身上迭代。完整的指令是: 对于范围内的i(len(我的字符串): 这将使每个数字从0步进到比字符串中的字符数少一个的位置。然后,您可以使用i对字符串进行切片以获得单个字符。您还可以使用另一个值或范围函数的其他输入值来指定从何处开始(默认值为0)和步长(默认值为1)。
至于其余部分,在if语句中使用i%2==0,打印i以打印索引,然后打印我的_字符串[i]打印字符。对不起,我在标题中拼写错误。是的,我知道Python从0开始计数。在字符串的索引3处,有一个“l”。不应按照我的代码打印“l”。我要求解释器只显示每个偶数索引的值。
index
方法是独立的当前与字符串一起使用的任何迭代器。抱歉,我在标题中拼写错误。是的,我知道Python从0开始计数。在字符串的索引3处,有一个“l”。不应根据我的代码打印“l”。我要求解释器只显示每个偶数索引的值。索引
方法独立于当前与字符串一起使用的任何迭代器。如果我们的答案更相似,我会大叫“Jinx!”。甚至我们的帽子都是一样的。不过,你确实以16秒的优势击败了我。Ruawahrrh rawwrh rawwrhhr(对于那些在冬季狂欢之后来到这里的人来说,这是可恶的雪人语)你们两个都有i/c反转OK,这是因为有两个