slayercjc
发表于 2017-8-13 15:50:17
本帖最后由 slayercjc 于 2017-8-14 08:26 编辑
def average_string(s):
s = s.split(' ') #接受输入的字符串以空格分开成数组
N = ['zero','one','two','three','four','five','six','seven','eight','nine']
l = []
t = []
for i in s: #把用户输入的字符串拼接成数组后循环
if i in N: #如果输入的数值在N数组里就追加到t变量
t.append(i)
if len(t) < len(s): #如果t数组的长度小于s数组的长度就说明有问题,就输出'n/a',否则继续执行else
print('n/a')
else:
for i in s: #循环s数组,得到对应N数组的键
l.append(N.index(i))
e= sum(l)//len(l) #求出平均值(整数形式)
print(e) #根据键得到相应的英文字符串
average_string('one one two')
想了半天,把代码写的简单明了些。。。希望多加点分。。。。
woigh
发表于 2017-8-13 18:58:39
貌似打很多多馀的代码{:10_269:}
number = {'one':1, 'two':2, 'three':3, 'four':4, 'five':5, 'six':6, 'seven':7, 'eight':8, 'nine':9}
def average_string(num):
sum = 0
mapp = {1:'one', 2:'two', 3:'three', 4:'four', 5:'five', 6:'six', 7:'seven', 8:'eight', 9:'nine'}
for i in range(len(num.split())):
sum += number]
if sum/len(num.split()) in mapp:
return mapp
else:
return 'n/a'
chunchun2017
发表于 2017-8-13 18:58:59
shinemic 发表于 2017-8-13 09:16
def average_string(s):
num_map = {'zero': 0, 'one': 1, 'two': 2, 'three': 3, 'four': 4,
...
大神永远是我们学习的榜样!
龙潭小天才
发表于 2017-8-13 21:31:05
def averge_string(string):
num={'zero':0,'one':1,'two':2,'three':3,'four':4,'five':5,'six':6,'seven':7,'eight':8,'nine':9}
l=string.split(' ')
str1=['zero','one','two','three','four','five','six','seven','eight','nine']
sum=0
for i in l:
if i not in str1:
return 'n/a'
break
else:
sum+=num
return str1
print(averge_string('one two three'))
1173419450
发表于 2017-8-13 21:38:46
看
MSK
发表于 2017-8-14 09:15:02
def average_string(s):
a = ['zero','one','two','three','four','five','six','seven','eight','nine']
b =
try:
c = sum( for i ins.split()]) // len(s.split())
except ValueError:
return 'n / a'
return a
我的老惨了{:10_266:}
chakyam
发表于 2017-8-14 09:27:35
看看大神
pythonueser
发表于 2017-8-14 09:40:12
看看题主
uilight
发表于 2017-8-14 16:22:03
瞄一眼
qwc3000
发表于 2017-8-15 06:40:40
def average_string(a):
dicts = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6, 'seven': 7, 'eight': 8, 'nine': 9, 'ten': 10}
temp = a.split(' ')
tmp_int = 0
try:
for i in temp:
tmp_int += dicts
tmp_int = tmp_int / len(temp)
temp = dicts.items()
keys = []
values = []
for key, value in temp:
keys.append(key)
values.append(value)
if (tmp_int in values):
tmp_int = values.index(tmp_int)
else:
print('n/a')
print("average_string :%s" % keys)
except:
print('n/a')
星际流火
发表于 2017-8-15 11:15:33
好
wac12245
发表于 2017-8-15 11:30:07
哎呦不错哦
小贱剑
发表于 2017-8-15 11:30:47
def average_string(str1):
list1=str1.split(' ')
list2=[]
dict1={'one':1,'two':2,'three':3,'four':4,'five':5,'six':6,'seven':7,'eight':8,'nine':9,'ten':10}
for i in list1:
if dict1.get(i):
list2.append(dict1)
else:
break
if len(list2)==len(list1):
print(sum(list2)/len(list1))
else:
print('n/a')
虫师2017
发表于 2017-8-15 14:55:12
看看
Liber_C
发表于 2017-8-15 20:41:08
def average_string(str):
for i in range(1,9):
b=str.split()
One=b.count('one')
Two=b.count('two')
Three=b.count('three')
Four=b.count('four')
Five=b.count('five')
Six=b.count('six')
Seven=b.count('seven')
Eight=b.count('eight')
Night=b.count('night')
c=One+Two+Three+Four+Five+Six+Seven+Eight+Night
if c==0:
return n/a
else:
d=(One*1+Two*2+Three*3+Four*4+Five*5+Six*6+Seven*7+Eight*8+Night*9)/c
if int(d)-d==0:
return d
else:
return n/a
ド゛゜范
发表于 2017-8-15 21:07:56
{:5_93:}
lalalaoh
发表于 2017-8-15 21:43:43
def average_string(string):
di = {'zero': 0, 'one' : 1, 'two': 2, 'three':3,
'four':4, 'five': 5, 'six': 6, 'seven':7,
'eight': 8, 'nine':9}
string_list = string.split(" ")
total =0
for each in string_list:
if each not in di:
return 'n/a'
else:
total += di
result = int(total/ len(string_list))
for key,value indi.items():
if(value == result):
return key
Dowai
发表于 2017-8-16 10:47:00
哦
restpop
发表于 2017-8-16 11:16:10
能问个问题么?py文件为什么双击打不开?但是可以在CMD中运行。 双击编写好的pygame小游戏就可以直接打开一个窗口。 双击其他py文件却打不开,不知道是为什么? 还请小甲鱼解惑
soujiu
发表于 2017-8-16 22:11:28
def average_string(string):
dic=dict(zip(('one','two','three','four','five','six','seven','eight','nine','ten'),range(1,11)))
lst=string.split(' ')
res=0
for i in lst:
tmp=dic.get(i,0)
if not tmp: return 'n/a'
res+=tmp
return res/len(lst)
print(average_string('one two three'))