我正在尝试让我的程序从字符串中相邻的列表中检查2个字符的实例的字符串,并返回另一个字符串以替换这两个字符。
def main():
dubs = ["ai", "ae", "ao", "au", "ei", "eu", "iu", "oi", "ou", "ui"]
newdubs = [ "eye", "eye", "ow", "ow", "ay","eh-oo", "ew", "oy", "ow","ooey"]
word = input("ENTER WORD : " )
count = 0
fin = []
while count < len(word):
if word[count:count+2] in dubs:
if word[count:count+2] == dubs[0]:
fin.append(newDubs[0] + "-")
if word[count:count+2] == dubs[1]:
fin.append(newDubs[1] + "-")
if word[count:count+2] == dubs[2]:
fin.append(newDubs[2] + "-")
if word[count:count+2] == dubs[3]:
fin.append(newDubs[3] + "-")
if word[count:count+2] == dubs[4]:
fin.append(newDubs[4] + "-")
if word[count:count+2] == dubs[5]:
fin.append(newDubs[5] + "-")
if word[count:count+2] == dubs[6]:
fin.append(newDubs[6] + "-")
if word[count:count+2] == dubs[7]:
fin.append(newDubs[7] + "-")
if word[count:count+2] == dubs[8]:
fin.append(newDubs[8] + "-")
if word[count:count+2] == dubs[9]:
fin.append(newDubs[9] + "-")
if word[count:count+2] not in dubs:
fin.append(word[count])
count+=1
fin= "".join(fin)
print(fin)
像wanai
这样的词,我希望wan-eye
结果是waneye-i
我还需要进行检查,以查看dubs
之前的字符是否是元音,但不必担心,直到它能正常工作为止
答案 0 :(得分:1)
使用
class MobileBind extends React.Component {
constructor(props) {
super(props);
this.state = {
birthYear: '',
defaultYear: true,
};
}
render() {
const {
birthYear,
} = this.state;
const createNumberSet = (min = 0, max = 10) => {
if (min > max)
return [];
return Array(max - min + 1)
.fill(min)
.map((value, index) => value + index)
.filter(value => value >= min);
};
const currentYear = moment().year();
const years = createNumberSet(currentYear - 100, currentYear);
const months = createNumberSet(1, 12);
const days = createNumberSet(1, 31);
return (
<div className={style['birth-date-input']} style={{ margin: 0 }}>
<FormControl>
<Select name="birthYear"
defaultValue={birthYear}
onChange={this.handleChange}
onClick={this.handleDefalutYear}
native>
{
[
<option key="birthYear-default" value="" disabled >
Year</option>,
...(years || []).map(value => <option key={`birthYear-${value}`} value={value} >{value}</option>)
]
}
</Select>
</FormControl>
</div>
}
handleChange = event => {
const target = event.target;
const value = target.type === 'checkbox' ? target.checked : target.value;
const name = target.name;
this.setState({ [name]: value });
}
handleDefalutYear = () => {
this.setState({defaultYear:false});
}
+ zip()
:
replace()
答案 1 :(得分:0)
我将重组您的代码以使其更具模块化:
dubs = ["ai", "ae", "ao", "au", "ei", "eu", "iu", "oi", "ou", "ui"]
newdubs = [ "eye", "eye", "ow", "ow", "ay","eh-oo", "ew", "oy", "ow","ooey"]
def dubbizer(word):
for itter in range(len(dubs)):
word = word.replace(dubs[itter], "-"+newdubs[itter])
return word
print(dubbizer("wanai"))
这应该为您提供wan-eye
不替换:
dubs = ["ai", "ae", "ao", "au", "ei", "eu", "iu", "oi", "ou", "ui"]
newdubs = [ "eye", "eye", "ow", "ow", "ay","eh-oo", "ew", "oy", "ow","ooey"]
def dubbizer(word):
for itter in range(len(dubs)):
while dubs[itter] in word:
word = word[:word.find(dubs[itter])]+"-"+newdubs[itter]+word[word.find(dubs[itter])+len(dubs[itter]):]
return word
print(dubbizer("wanai"))