我如何找到哪个字符;“风险值中的风险值”;是用python编写的
如何找出变量中的变量是什么字符。 我希望这听起来不要太混乱 我想做的是做一个搜索引擎。 我有一个叫做“currentchar”的变量,它是一个整数 所以我想做的是:我如何找到哪个字符;“风险值中的风险值”;是用python编写的,python,search,python-3.x,character,Python,Search,Python 3.x,Character,如何找出变量中的变量是什么字符。 我希望这听起来不要太混乱 我想做的是做一个搜索引擎。 我有一个叫做“currentchar”的变量,它是一个整数 所以我想做的是: if s[i] in l[currentchar:]: #Just checking if a string is in a string... currentchar = (Figure out which char "s[i]" started at.) 例如,如果s[i]是“ph”并且l[currentchar:是“
if s[i] in l[currentchar:]: #Just checking if a string is in a string...
currentchar = (Figure out which char "s[i]" started at.)
例如,如果s[i]
是“ph”
并且l[currentchar:
是“大象”
我不想currentchar
设置为3
,因为“ph”
通过“大象”
开始了3个字符
我希望人们理解我的意思。在检查子字符串是否存在时,请使用
索引
s = "elephant"
print (s.index("ph"))
3
if s[i] in l[currentchar:]: #Just checking if a string is in a string...
currentchar = l[currentchar:].index(s[i])
你甚至没有为自己找到一个解决方案,胡?可能是@user1541397的副本,不用担心,如果你在使用index,你想确保子字符串存在,或者它会抛出一个错误。或者你可以使用
find
:currentchar=l[currentchar:]。find(s[i])
。如果find
找不到它,它将返回-1,而不是异常(因此您需要在它之后而不是之前使用if
)。str.find
和str.index
都支持启动参数,因此另一种方法是使用currentchar=l.index(s[i],currentchar)
@xZise,if检查没有问题,如果字符串不存在,我想OP不希望currentchar设置为-1I,我只是想指出还有find
。我还注意到,他仍然需要一个if,我的第二个带有start参数的示例实际上使用的是str.index
。