鱼C论坛

 找回密码
 立即注册
查看: 2476|回复: 5

[已解决]这个代码怎么写

[复制链接]
发表于 2016-9-25 16:14:59 From FishC Mobile | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
刚刚学到零基础14课,想知道输入一个带有数字的字符串后,如何根据数字大小将字母从大到小排列,并且数字不出现在打印的结果中。例如输入“is2,t1his,boy4,a3”输出“this is a boy”
最佳答案
2016-9-25 18:02:15
a = "is2, t1his, boy4, a3"
b = a.split(', ')
print(b)


   

dicts = {}
for words in b:
    length = len(words)
    for num in range(length):
            if ord(words[num]) in range(48, 58):
                dicts[int(words[num])] = words[0:num]+words[num+1:]

print(dicts)

result = ""

lists = []

for digs in sorted(dicts.keys()):
    lists.append(digs)

long = len(lists)


for nums in range(long - 1):
    result += dicts[lists[nums]]+" "



result += dicts[lists[-1]]

print(result)
   
写得比较烂,但是可用
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-9-25 16:38:31 | 显示全部楼层
自己想想办法呗~~
努力想~
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-9-25 18:02:15 | 显示全部楼层    本楼为最佳答案   
a = "is2, t1his, boy4, a3"
b = a.split(', ')
print(b)


   

dicts = {}
for words in b:
    length = len(words)
    for num in range(length):
            if ord(words[num]) in range(48, 58):
                dicts[int(words[num])] = words[0:num]+words[num+1:]

print(dicts)

result = ""

lists = []

for digs in sorted(dicts.keys()):
    lists.append(digs)

long = len(lists)


for nums in range(long - 1):
    result += dicts[lists[nums]]+" "



result += dicts[lists[-1]]

print(result)
   
写得比较烂,但是可用

评分

参与人数 1鱼币 +2 收起 理由
SixPy + 2 热爱鱼C^_^

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-9-25 21:43:23 | 显示全部楼层
我也写一个。
  1. str1 = 'is2,t1his,boy4,a3'
  2. strlist = str1.split(',')
  3. strdict = {}
  4. for s1 in strlist:
  5.     key = ''
  6.     value = ''
  7.     for c in s1:
  8.         if c.isdigit():
  9.             key += c
  10.         else:
  11.             value += c
  12.     strdict[key] = value
  13. for i in range(len(strdict)):
  14.     print(strdict[str(i + 1)], end = ' ')
  15. print()
  16.         
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2016-9-25 22:58:44 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-10-7 16:11:19 | 显示全部楼层
我也学到 14课   看的云里雾里的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-2-23 00:51

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表