坑得飞起
发表于 2018-3-17 00:20:27
坑得飞起 发表于 2018-3-16 00:06
ss=input()
s1=['a','e','i','o','u']
s2=list(ss)
ss=input()
s1=['a','e','i','o','u']
s2=list(ss)
zs=0
jl=[]
for i in range(len(s2)) :
if s2 in s1:
jl+=
if (len(jl)==2)and(s2]!=s2]) :
if jl==jl:
print('None')
else :
i=s2]
s2]=s2]
s2]=i
ss=''
for i in s2 :
ss+=str(i)
print(ss)
else :
print('None')
多谢版主点评
fan1993423
发表于 2018-3-17 00:20:59
a=input('请输入字符串,改变元音字母的顺序')
c=['a','e','i','o','u']
g=[]
k=[]
for b in a:
if b in c:
g.append(b)
if len(set(g))==len(g)==2:
p=list(a)
m=a.find(g)
n=a.find(g)
t=p
p=p
p=t
print(''.join(p))
else:
print('None')
dfh93@163.com
发表于 2018-3-17 07:02:06
x=input("请输入一个小写英文字符:")
str=x
a=list(str)
cstring1=''
cstring2=''
h1=0
h2=0
t=0
for i in range(len(str)):
if (a=='a') or (a=='e') or (a=='i') or (a=='o') or (a=='u'):
if t==0:
cstring1=a
h1=i
elif t==1:
cstring2=a
h2=i
t=t+1
if (t==0) or (t==1) or (t>2):
print("None")
else:
if cstring1==cstring2:
print("None")
else:
a=cstring2
a=cstring1
f=''
for i in range(len(a)):
f=f+a
print(f)
新手·ing
发表于 2018-3-17 07:16:51
def change(s):
s = list(s)
lst = ['a', 'e', 'i', 'o', 'u']
fla =
if len(fla) % 2 != 0:
return None
elif list(set(fla)) != fla:
return None
else:
temp1 = s))]
temp2 = s))]
s))] = temp1
s))] = temp2
return ''.join(s)
麻烦else哪里怎么改成一行呢。
scchapter
发表于 2018-3-17 09:24:27
sukiwhip 发表于 2018-3-15 22:17
第一个if语句有问题,如果有元音没有,则会输出none
lyjlyj
发表于 2018-3-17 11:13:21
yuanyin = "aeiou"
def ZhuanHuan(str1):
str1 = list(str1)#由于下面的交换操作时会报错
n = 0
pos = []
for item in str1:
if item in yuanyin:
pos.append(n)
n += 1
if len(pos) == 2:
#交换操作
str1],str1] = str1],str1]
str1 = ''.join(str1)
return str1
else:
return None
def main():
a = ZhuanHuan("apple")
b = ZhuanHuan("michan")
c = ZhuanHuan("abcd")
d = ZhuanHuan("abicod")
print (a,b,c,d)
if __name__ == "__main__":
main()
Agoni
发表于 2018-3-17 11:14:59
新手·ing 发表于 2018-3-17 07:16
麻烦else哪里怎么改成一行呢。
这fla精简的可怕{:10_245:}
qpwoeiruty
发表于 2018-3-17 11:45:26
不好意思,刚刚学python三天,都是些最简单的代码。
yuanyin =['a','e','i','o','u']
n=0
m=0
word = str(input('请输入一个单词'))
for i in word:
if i in yuanyin:
n=n+1
if n>=3:
print("None")
else:
for j in word:
for k in word:
if j in yuanyin and k in yuanyin and j==k:
m=m+1
if m>=3:
print("None")
else:
Word=word[::-1]
print ("结果是"+Word)
此用户不合法
发表于 2018-3-17 11:47:51
import re
def check(n):
patten = re.compile(r"a|e|i|o|u")
a = patten.findall(n)
b = list(n)
if len(a) == 2:
if a != a:
i = b.index(a)
j = b.index(a)
b, b = b, b
return ''.join(b)
a = input()
print(check(a))
8306最硬
发表于 2018-3-17 13:03:36
新手·ing 发表于 2018-3-17 07:16
麻烦else哪里怎么改成一行呢。
我觉得多行好看点{:10_250:}
天圆突破
发表于 2018-3-17 14:49:05
新手·ing 发表于 2018-3-17 07:16
麻烦else哪里怎么改成一行呢。
试了一下:
returnreduce(lambda x,y:x+y, map(lambda x:(fla if x == fla else fla) if x in lst else x, s))
不过这样必须得在最前面import一下
或者这样:
return''.join(list(map(lambda x:(fla if x == fla else fla) if x in lst else x, s)))
但是貌似上面的这句代码有问题……
elif list(set(fla)) != fla:
set无序,list有序,所以结果就是薛定谔的猫,一会None,一会正常
luokaoge
发表于 2018-3-17 17:29:41
luokaoge 发表于 2018-3-16 10:13
def aeiou():
#定义空列表
list1 = []
{:9_240:}
没看到不能相同元音的条件。。。
def aeiou():
#定义空列表
list1 = []
#让用户输入单词
a = input('Please input a word:')
#定义元音字符串
b = 'aeiou'
#判断输入单词中元音字母个数
for i in a:
if i in b:
list1.append(i)
else:
pass
if list1 != list1:
#当输入单词仅有两个元音字母时互换两个元音字母的位置
if len(list1) == 2:
s1 = list(a)
#返回输入单词元音字母的索引值
c = a.index(list1)
d = a.index(list1)
#交换元音字母的位置
e = s1
f = s1
s1 = f
s1 = e
#赋值新的字符串并打印
new =''.join(s1)
print('New word is'+ ' ' + new)
#不为两个元音字母时打印None
else:
print('None')
else :
print('None')
aeiou()
新手·ing
发表于 2018-3-17 17:37:22
def change(s):
s = list(s)
lst = ['a', 'e', 'i', 'o', 'u']
fla =
if len(fla) % 2 != 0:
return None
elif list(set(fla)) != fla:
return None
else:
temp1 = s))]
temp2 = s))]
s))] = temp1
s))] = temp2
return ''.join(s)
被翻红浪
发表于 2018-3-17 21:51:46
def vocalExchange(str1):
vocal = "aeiou"
cases = []
for each in vocal:
if each in str1 and each not in cases:
cases.extend((str1.index(each), each))
if len(cases) == 4:
return str1[:cases] + cases + str1+1:cases] + cases + str1+1:]
print(vocalExchange("apple"))
print(vocalExchange("machin"))
print(vocalExchange("abca"))
print(vocalExchange("abicod"))
_小菜鸟
发表于 2018-3-19 09:24:53
_小菜鸟 发表于 2018-3-16 16:56
测试结果
eppla
none
主函数调用的第三个参数,调试的时候写成string1了,应该改成string2。至于出错的话,不知道是不是abca打印了两次,如果是的话,就是上面这个原因。如果不是的话,请说明一下具体的出错内容。{:5_91:}
夜与晨
发表于 2018-3-19 16:50:43
本帖最后由 夜与晨 于 2018-3-19 17:01 编辑
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def output(str_origin):
list_temp = []
str_origin = str_origin.lower()
str_vowel =
for each in str_origin:
if each in 'aeiou':
list_temp.append(each)
if (len(str_vowel) == 2) and len(set(str_vowel)) == 2:
str_origin = str_origin[:str_origin.index(list_temp)] + list_temp[-1] +\
str_origin) + 1:str_origin.index(list_temp[-1])]+ list_temp +\
str_origin)+1:]
return str_origin
else:return None
if __name__ == '__main__':
# str_origin = input("请输入字符串:")
str_origin = 'iye'
print(output(str_origin))
zlfy1982
发表于 2018-3-20 13:14:51
list1 =['a','e','i','o','u']
list2 = list(input("请输入一个测试用例:"))
temp = []
for num in list2:
if num in list1:
temp.append(num)
continue
if len(temp)==2:
a =temp
b = temp
c=list2.index(a)
d = list2.index(b)
list2 = b
list2 = a
print(list2)
else:
print("NONE")
#python小白,欢迎指导!
jiantu
发表于 2018-3-20 22:33:05
def day164(temp):
str1 = 'aeiou'
str2 = ''
for each in temp:
for i in str1:
if each == i:
str2 += i
if len(str2) == 2 and str2 != str2:
temp1 = temp.replace(str2,str2)
temp1 = temp.replace(str2,str2,1)
return temp1
else:
return None
小城小爱
发表于 2018-3-21 09:09:21
def func():
string = input("请输入一个英文单词:")
words = ["a", "e", "i", "o", "u"]
word_list = []
test = []
for i in range(len(string)):
word_list.append(string)#由于字符串不能直接被修改,转换成列表
if string in words:
test.append(string) #统计原字符串中元音字母
if (len(test) == 2) and (test != test): #只有在元音字母数量为2且不一样的情况下,才有输出
word_list)], word_list)] = word_list)], word_list)]
new_string = "".join(word_list)
return new_string
else:
return None
测试结果:
>>> func()
请输入一个英文单词:apple
eppla
>>> func()
请输入一个英文单词:machin
michan
>>> func()
请输入一个英文单词:appleapple
None
>>> func()
请输入一个英文单词:abcd
None
nanayuri
发表于 2018-3-21 11:12:16
vowel_num = []
vowel_list = ['a', 'e', 'i', 'o', 'u']
def swap_vowel(word):
num = 0
for each_word in word:
if each_word in vowel_list:
vowel_num.append(each_word)
num += 1
vowel1 = vowel_num
vowel2 = vowel_num
#print(vowel1, vowel2)
if num != 2 or (num == 2 and vowel1 == vowel2):
return None
else:
str1 = str(word).replace(vowel1, '$')
str1 = str1.replace(vowel2, vowel1)
str1 = str1.replace('$', vowel2)
return str1