简体   繁体   English

Python:有没有一种方法可以查找和删除字符串中字符的第一个和最后一个出现的位置?

[英]Python: Is there a way to find and remove the first and last occurrence of a character in a string?

The problem: 问题:

Given a string in which the letter h occurs at least twice. 给定一个字符串,其中字母h至少出现两次。 Remove from that string the first and the last occurrence of the letter h, as well as all the characters between them. 从该字符串中删除字母h的第一个和最后一个出现以及它们之间的所有字符。

How do I find the first and last occurrence of h? 如何找到h的第一个和最后一个出现? And how can I remove them and the characters in between them? 以及如何删除它们以及它们之间的字符?

#initialize the index of the input string
index_count =0

#create a list to have indexes of 'h's
h_indexes = []

#accept input strings
origin_s = input("input:")

#search 'h' and save the index of each 'h' (and save indexes of searched 'h's into h_indexes
for i in origin_s:

first_h_index =
last_h_index = 

#print the output string
print("Output:"+origin_s[     :     ]+origin_s[     :])

You need to use regex: 您需要使用正则表达式:

>>> import re
>>> s = 'jusht exhamplhe'
>>> re.sub(r'h.+h', '', s)
'juse'

Using a combination of index , rindex and slicing : 使用indexrindexslicing的组合:

string = 'abc$def$ghi'
char = '$'
print(string[:string.index(char)] + string[string.rindex(char) + 1:])
# abcghi

How do I find the first and last occurrence of h? 如何找到h的第一个和最后一个出现?

First occurence: 第一次出现:

first_h_index=origin_s.find("h");

Last occurence: 上次出现:

last_h_index=origin_s.rfind("h");

And how can I remove them and the characters in between them? 以及如何删除它们以及它们之间的字符?

Slicing 切片

string = '1234-123456789'
char_list = []
for i in string:
    char_list.append(string[i])

char_list.remove('character_to_remove')

According to the documentation, remove(arg) is a method acting on a mutable iterable (for example list ) that removes the first instance of arg in the iterable. 根据文档, remove(arg)是作用于可变可迭代对象(例如list )的方法,该方法删除可迭代对象中arg的第一个实例。

This will help you to understand more clearly: 这将帮助您更清楚地了解:

string = 'abchdef$ghi'

first=string.find('h')

last=string.rfind('h')

res=string[:first]+string[last+1:]

print(res)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果字符串中的第一个或最后一个字符是 $ 在 python 中删除它 - If first or last character from string is $ remove it in python Python - Pandas - 删除第一次出现的字符和修复字符串之间的内容 - Python - Pandas - Remove content between the first occurrence of a character and a fix string 正则表达式删除除第一次出现和最后一次出现的特殊字符 - regex remove special character except first occurrence and last occurrence Python在索引后找到第一个出现的字符 - Python find first occurrence of character after index python3找到最后出现的字符串然后写 - Python3 find last occurrence string then write python-如何反复删除字符串中的第一次出现的python? - How to remove first occurrence in string repeatedly python? Python 3.x -> 使用递归和循环查找字符串中字符的最后一次出现 - Python 3.x -> Find Last Occurrence of character in string by using recursion and loop 通过在python中另一个字符之前最后出现的字符来解析字符串 - parse string by the last occurrence of the character before another character in python 在字符的第一次和最后一次出现时拆分? - Split at first and last occurrence of a character? 在Python中最后一次出现给定字符之后,找到字符串字符的最简单方法是什么? - What is the easiest way of finding the characters of a string after the last occurrence of a given character in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM