Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 哪个角色先来?_String_Python 3.x - Fatal编程技术网

String 哪个角色先来?

String 哪个角色先来?,string,python-3.x,String,Python 3.x,所以输入是word,我想知道是a还是b先到 我可以使用a_index=word.find('a')并将其与b_index=word.find('b')进行比较,如果a是第一位的,则返回a是第一位的。但是,如果b不在word中,.find()将返回-1,因此简单地比较b\u index“x”。@viraptor假设列表中只包含单词中的字母。但是,我同意这是错误的假设。现在应该可以了。我希望您删除否决票。我不完全熟悉*args语法,所以我使用了[list]相反,我们的想法是,要比较的字符数可能会有所

所以输入是
word
,我想知道是
a
还是
b
先到

我可以使用
a_index=word.find('a')
并将其与
b_index=word.find('b')
进行比较,如果
a
是第一位的,则返回
a是第一位的。但是,如果
b
不在
word
中,
.find()
将返回
-1
,因此简单地比较
b\u index
将返回
b是第一个
。这可以通过添加更多的if语句来实现,但是有更干净的方法吗

功能描述:
输入:
word
[字符列表]

输出:
列表中第一个出现在单词中的字符


示例:
first\u实例(“蝶形”、“a”、“u”、“e”)
返回
u

在任何情况下,您都需要检查输入的类型:

if isinstance(your_input, str):
   a_index = your_input.find('a')
   b_index = your_input.find('b')
   # Compare the a and b indexes

elif isinstance(your_input, list):

   a_index = your_input.index('a')
   b_index = your_input.index('b') 
   # Compare the a and b indexes
else:

   # Do something else
编辑:

def first_instance(word, lst):
    indexes = {}
    for c in lst:
        if c not in indexes:
            indexes[c] = word.find(c)
        else:
            pass

    return min(indexes, key=indexes.get)
它将从list
lst
返回单词中的第一个字符

如果需要返回此信函的索引,请将返回语句替换为:

return min_value = indexes[min(indexes, key=indexes.get)]

在任何情况下,您都需要检查输入的类型:

if isinstance(your_input, str):
   a_index = your_input.find('a')
   b_index = your_input.find('b')
   # Compare the a and b indexes

elif isinstance(your_input, list):

   a_index = your_input.index('a')
   b_index = your_input.index('b') 
   # Compare the a and b indexes
else:

   # Do something else
编辑:

def first_instance(word, lst):
    indexes = {}
    for c in lst:
        if c not in indexes:
            indexes[c] = word.find(c)
        else:
            pass

    return min(indexes, key=indexes.get)
它将从list
lst
返回单词中的第一个字符

如果需要返回此信函的索引,请将返回语句替换为:

return min_value = indexes[min(indexes, key=indexes.get)]

您可以创建一个包含
word
字符列表的函数-将这些字符转换为一个集合,以便快速查找和循环
word
以找到的第一个字母为准,例如:

# Chars can be any iterable whose elements are characters
def first_of(word, chars):
    # Remove duplicates and get O(1) lookup time
    lookup = set(chars)
    # Use optional default argument to next to return `None` if no matches found
    return next((ch for ch in word if ch in lookup), None)
例如:

>>> first_of('bob', 'a')
>>> first_of('bob', 'b')
'b'
>>> first_of('abob', 'ab')
'a'
>>> first_of("butterfly", ['a', 'u', 'e'])
'u'

这样,您只需在
word
上迭代一次,并在找到的第一个字母上短路,而不是运行多个查找,存储结果,然后计算最低索引。

您可以创建一个函数,该函数使用
word
和一个
字符列表
-将这些字符转换为一个集合,以便快速查找和删除循环使用
单词
找到第一个字母,例如:

# Chars can be any iterable whose elements are characters
def first_of(word, chars):
    # Remove duplicates and get O(1) lookup time
    lookup = set(chars)
    # Use optional default argument to next to return `None` if no matches found
    return next((ch for ch in word if ch in lookup), None)
例如:

>>> first_of('bob', 'a')
>>> first_of('bob', 'b')
'b'
>>> first_of('abob', 'ab')
'a'
>>> first_of("butterfly", ['a', 'u', 'e'])
'u'

这样,您只需在word上迭代一次,并在找到的第一个字母上短路,而不是运行多个查找,存储结果,然后计算最低索引。

创建一个没有缺少字符的列表,然后按位置排序

def first_found(word, chars):
  places = [x for x in ((word.find(c), c) for c in chars) if x[0] != -1]
  if not places:
    # no char was found
    return None
  else:
    return min(places)[1]

列出一个没有缺少字符的列表,然后按位置排序

def first_found(word, chars):
  places = [x for x in ((word.find(c), c) for c in chars) if x[0] != -1]
  if not places:
    # no char was found
    return None
  else:
    return min(places)[1]

我对*args语法不太熟悉,所以我改用了[list],其思想是要比较的字符数可能会有所不同。该列表将包含字符。恐怕我不是否决票的来源。我误解了您的意思,但功能将是
def first_instance(word,list)
,而不是两个单独的函数。列表将包含要在word中检查的字符(因此可能有两个以上的字符)。例如
第一个实例(“蝴蝶”、“a”、“u”、“e”)
我否决了你的投票,因为你的第一个答案与问题不匹配。而且你的编辑有一个特别提到的错误:
第一个实例(“abcdef”、[“a”、“x”])
->
“x”
。@viraptor假设列表中只包含单词中的字母。但是,我同意这是错误的假设。现在应该可以了。我希望您删除否决票。我不完全熟悉*args语法,所以我使用了[list]相反,我们的想法是,要比较的字符数可能会有所不同。列表将包含字符。恐怕我不是否决票的来源。我误解了你的意思,但功能将是
def first_instance(word,list)
,而不是两个单独的函数。列表将包含要签入word的字符(因此可能有两个以上的字符)。例如:
第一个实例(“蝴蝶”,“a”,“u”,“e”)
我否决了你,因为你的第一个答案与问题不匹配。你的编辑有一个特别提到的错误:
第一个实例(“abcdef”,“a”,“x”)
->
“x”
在你的情况下。@viraptor假设列表中只包含单词中的字母。但是,我同意这是错误的假设。现在应该没问题了。我希望你不要投反对票。我想Jon和我对这个问题的理解不同(单词中的第一个字母与单词中的第一个字母)。你对
第一个实例(“abcdef”,“d”,“a'])
的预期结果是什么?@viraptor将是
第一个实例(“abcdef”,“d”,“x'])
将是
d
。我想Jon和我对这个问题的理解不同(单词中的第一个字母与单词中的第一个字母)。您对
第一个实例(“abcdef”,“d”,“a'])
的预期结果是什么?@viraptor将是
a
第一个实例(“abcdef”,“d”,“x'])
将是
d