Eli*_*sky 635
有两种字符串方法,find()和index().两者之间的区别是找不到搜索字符串时发生的情况. find()返回-1 并index()加注ValueError.
find()>>> myString = 'Position of a character'
>>> myString.find('s')
2
>>> myString.find('x')
-1
Run Code Online (Sandbox Code Playgroud)
index()>>> myString = 'Position of a character'
>>> myString.index('s')
2
>>> myString.index('x')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found
Run Code Online (Sandbox Code Playgroud)
string.find(s, sub[, start[, end]])
返回s中找到子字符串sub的最低索引,以便sub完全包含在其中s[start:end].-1失败时返回.开始和结束的默认值以及负值的解释与切片的默认值相同.
和:
string.index(s, sub[, start[, end]])
喜欢find()但是ValueError在找不到子串的时候提高.
Sal*_*ali 112
只是为了完整起见,如果您需要查找字符串中字符的所有位置,您可以执行以下操作:
s = 'shak#spea#e'
c = '#'
print [pos for pos, char in enumerate(s) if char == c]
Run Code Online (Sandbox Code Playgroud)
将返回 [4, 9]
gho*_*g74 47
>>> s="mystring"
>>> s.index("r")
4
>>> s.find("r")
4
Run Code Online (Sandbox Code Playgroud)
"啰嗦"的方式
>>> for i,c in enumerate(s):
... if "r"==c: print i
...
4
Run Code Online (Sandbox Code Playgroud)
得到子串,
>>> s="mystring"
>>> s[4:10]
'ring'
Run Code Online (Sandbox Code Playgroud)
Dim*_*rak 15
当字符串包含重复字符时会发生什么?根据我的经验,index()我看到重复你得到相同的索引.
例如:
s = 'abccde'
for c in s:
print('%s, %d' % (c, s.index(c)))
Run Code Online (Sandbox Code Playgroud)
会回来:
a, 0
b, 1
c, 2
c, 2
d, 4
Run Code Online (Sandbox Code Playgroud)
在这种情况下,你可以这样做:
for i, character in enumerate(my_string):
# i is the position of the character in the string
Run Code Online (Sandbox Code Playgroud)
A.J*_*oly 15
只是为了完成,如果我想在文件名中找到扩展名来检查它,我需要找到最后的'.',在这种情况下使用rfind:
path = 'toto.titi.tata..xls'
path.find('.')
4
path.rfind('.')
15
Run Code Online (Sandbox Code Playgroud)
在我的情况下,我使用以下,无论完整的文件名是什么,它都适用:
filename_without_extension = complete_name[:complete_name.rfind('.')]
Run Code Online (Sandbox Code Playgroud)
Joh*_*hin 10
string.find(character)
string.index(character)
Run Code Online (Sandbox Code Playgroud)
也许您想查看文档以了解两者之间的区别.
小智 6
一个字符可能在字符串中多次出现。例如,在字符串中sentence,位置eis是1, 4, 7(因为索引通常从零开始)。但是我发现这是两个函数,find()并且index()返回字符的第一个位置。因此,这样做可以解决此问题:
def charposition(string, char):
pos = [] #list to store positions for each 'char' in 'string'
for n in range(len(string)):
if string[n] == char:
pos.append(n)
return pos
s = "sentence"
print(charposition(s, 'e'))
#Output: [1, 4, 7]
Run Code Online (Sandbox Code Playgroud)
Python 有一个内置的字符串方法可以完成这项工作:index()。
string.index(value, start, end)
Run Code Online (Sandbox Code Playgroud)
在哪里:
def character_index():
string = "Hello World! This is an example sentence with no meaning."
match = "i"
return string.index(match)
print(character_index())
> 15
Run Code Online (Sandbox Code Playgroud)
假设您需要该字符所在的所有索引match,而不仅仅是第一个索引。
pythonic 方法是使用enumerate().
def character_indexes():
string = "Hello World! This is an example sentence with no meaning."
match = "i"
indexes_of_match = []
for index, character in enumerate(string):
if character == match:
indexes_of_match.append(index)
return indexes_of_match
print(character_indexes())
# [15, 18, 42, 53]
Run Code Online (Sandbox Code Playgroud)
或者使用列表理解更好:
def character_indexes_comprehension():
string = "Hello World! This is an example sentence with no meaning."
match = "i"
return [index for index, character in enumerate(string) if character == match]
print(character_indexes_comprehension())
# [15, 18, 42, 53]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
961670 次 |
| 最近记录: |