python-如何检查数组是否包含值,但串联
我想检查数组是否包含值,但在串联示例中:
word = 'hello'
def splite(word):
return list(word)
for i in range(len(word)):
letter = splite(word[i])
if 'e' in letter:
print('e')
if 'h' in letter:
print('h')
输出是
eh
,我希望输出到
he
我知道的,因为在脚本中,字母“ e”检测到了,但我希望首先检测字母“ H”,因为在单词中“你好” H是第一个,我该怎么做?
I want to check if the array contains value but in series example:
word = 'hello'
def splite(word):
return list(word)
for i in range(len(word)):
letter = splite(word[i])
if 'e' in letter:
print('e')
if 'h' in letter:
print('h')
the output is
eh
and I want the output to
he
I know because in the script the letter 'e' detect first but I want the letter 'h' detect first because in the word 'hello' h is first, how can I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于字符串是字符列表,因此您无需将其拆分。您可以像“正常”列表一样迭代它:
您的代码如何产生
eh
?测试代码时,它总是打印He
。该代码的实际目标是什么?Since a string is a list of characters, you dont need to split it. You can just iterate over it like it would be a 'normal' list:
How did your code produce
eh
? When testing your code it always printedhe
. What is the actual goal of this code snipped?从您的问题标题中猜测(如何检查数组是否包含一个值,但以串联为单位),据我了解,这是一个很好的解决方案。
Guessing from your question title (how to check if array contains a value but in series), this is a great solution for your problem, as far as I understand it.