鱼C论坛

 找回密码
 立即注册
查看: 1162|回复: 8

[已解决]python字典输出字符串

[复制链接]
发表于 2019-4-28 15:33:15 | 显示全部楼层 |阅读模式

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

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

x
{'6': '-3', '5': 1, '4': '-5', '3': '2', '2': '-2', 1: '7', 0: '-2'} 请问,如果想把这个字典,也就是把这个字典转化为正常的多项式改如何操作呢?

'-3x^6 + x^5 - 5x^4 + 2x^3 - 2x^2 + 7x - 2'
最佳答案
2019-4-28 15:48:51
本帖最后由 wp231957 于 2019-4-28 16:02 编辑
  1. zd={'6': '-3', '5': 1, '4': '-5', '3': '2', '2': '-2', 1: '7', 0: '-2'}
  2. c=''
  3. for key in zd.keys():
  4.    if int(zd[key])<0:
  5.      c+=zd[key]+'x^'+str(key)
  6.    elif int(zd[key])>1:
  7.      c+='+'+str(zd[key])+'x^'+str(key)
  8.    elif int(zd[key])==1:
  9.      c+='+x^'+str(key)
  10. print(c)
  11.    
复制代码


更新了一下,解决x^0   x^1 的问题,就是代码太繁琐了


  1. zd={'6': '-3', '5': 1, '4': '-5', '3': '2', '2': '-2', 1: '7', 0: '-2'}
  2. c=''
  3. for key in zd.keys():
  4.    if int(zd[key])<0 and int(key)>1:
  5.      c+=zd[key]+'x^'+key
  6.    elif int(zd[key])<0 and int(key)==1:
  7.      c+=zd[key]+'x'
  8.    elif int(zd[key])<0 and int(key)==0:
  9.      c+=zd[key]
  10.    elif int(zd[key])>1 and int(key)>1:
  11.      c+='+'+zd[key]+'x^'+key
  12.    elif int(zd[key])>1 and int(key)==1:
  13.      c+='+'+zd[key]+'x'
  14.    elif int(zd[key])>1 and int(key)==0:
  15.      c+='+'+zd[key]         
  16.    elif int(zd[key])==1 and int(key)>1:
  17.      c+='+x^'+key
  18.    elif int(zd[key])==1 and int(key)==1:
  19.      c+='+x'
  20. print(c)
  21.    

  22.    
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-4-28 15:36:53 | 显示全部楼层
^这个是啥操作符?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-4-28 15:48:51 | 显示全部楼层    本楼为最佳答案   
本帖最后由 wp231957 于 2019-4-28 16:02 编辑
  1. zd={'6': '-3', '5': 1, '4': '-5', '3': '2', '2': '-2', 1: '7', 0: '-2'}
  2. c=''
  3. for key in zd.keys():
  4.    if int(zd[key])<0:
  5.      c+=zd[key]+'x^'+str(key)
  6.    elif int(zd[key])>1:
  7.      c+='+'+str(zd[key])+'x^'+str(key)
  8.    elif int(zd[key])==1:
  9.      c+='+x^'+str(key)
  10. print(c)
  11.    
复制代码


更新了一下,解决x^0   x^1 的问题,就是代码太繁琐了


  1. zd={'6': '-3', '5': 1, '4': '-5', '3': '2', '2': '-2', 1: '7', 0: '-2'}
  2. c=''
  3. for key in zd.keys():
  4.    if int(zd[key])<0 and int(key)>1:
  5.      c+=zd[key]+'x^'+key
  6.    elif int(zd[key])<0 and int(key)==1:
  7.      c+=zd[key]+'x'
  8.    elif int(zd[key])<0 and int(key)==0:
  9.      c+=zd[key]
  10.    elif int(zd[key])>1 and int(key)>1:
  11.      c+='+'+zd[key]+'x^'+key
  12.    elif int(zd[key])>1 and int(key)==1:
  13.      c+='+'+zd[key]+'x'
  14.    elif int(zd[key])>1 and int(key)==0:
  15.      c+='+'+zd[key]         
  16.    elif int(zd[key])==1 and int(key)>1:
  17.      c+='+x^'+key
  18.    elif int(zd[key])==1 and int(key)==1:
  19.      c+='+x'
  20. print(c)
  21.    

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

使用道具 举报

发表于 2019-4-28 15:53:25 | 显示全部楼层
可以遍历这个字典,然后根据key和value将它们链接到字符串后面。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-4-28 16:13:10 | 显示全部楼层
dict0 = {'6': '-3', '5': 1, '4': '-5', '3': '2', '2': '-2', 1: '7', 0: '-2'}

str0=''
for key in dict0.keys():
    a = int(dict0[key])
    if a < 0:
        str0 += str(a)+'x^'+str(key)
    else:
        str0 += '+'+str(a)+'x^'+str(key)
str0 = str0.replace('1x','x')
str0 = str0.replace('x^1','x')
str0 = str0.replace('x^0','')
print(str0)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-4-29 21:05:36 | 显示全部楼层
一行代码真好玩系列
  1. def fun(dict1):
  2.     return "".join(list(map(lambda x:"{}x^{}".format(x[1] if int(x[1])<0 else "+{}".format(x[1]),x[0]) ,list(dict1.items()))))


  3. dict1 = {'6': '-3', '5': 1, '4': '-5', '3': '2', '2': '-2', 1: '7', 0: '-2'}
  4. print(fun(dict1))
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-5-7 17:47:09 | 显示全部楼层
风丶少 发表于 2019-4-28 15:36
^这个是啥操作符?

2^3, 2的三次方  
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-5-7 17:52:28 | 显示全部楼层
咕咕鸡鸽鸽 发表于 2019-4-29 21:05
一行代码真好玩系列

确实很好玩。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-5-7 17:53:18 | 显示全部楼层
shake_a_tree@16 发表于 2019-4-28 16:13
dict0 = {'6': '-3', '5': 1, '4': '-5', '3': '2', '2': '-2', 1: '7', 0: '-2'}

str0=''

谢谢&#128591;  学习到了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-9-10 06:44

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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