syntaxerror不能在python中分配函数调用

时间:2018-03-18 06:25:43

标签: python python-3.x syntax-error

我在python中的以下代码中遇到语法错误任何人都可以帮我解决这个问题吗?我的代码在下面给出

    **from operator import itemgetter
d = {"A":1,"b":2,"c":5,"d":4}
print("original dic \n" ,d)
sor = sorted(d.itemsec), key=itemgetter(0)
print("in ascending order : " ,sor) 
sor = sorted(d.itemsec), key=itemgetter(0), reverse=true
print("in reverse = " ,sor)
d.update({"v":7})
print("updated directery" ,d)
d1 = {"x":9}
d.update(d1)
print("concetinated dir = \n" ,d)
d2 = {"r":12, "s":14, "u":333}
d2.update(d)
print("merged dirs are = \n" )
print(d2)**

1 个答案:

答案 0 :(得分:-1)

您的代码中有多处错误......

  1. sorted语句要求右括号括起来 它的所有论点。
  2. 只需放置字典而不添加任何其他内容,例如.items或您使用的关键字。
  3. 关键字True应该大写。
  4. 这是工作代码:

    from operator import itemgetter
    d = {"A":1,"b":2,"c":5,"d":4}
    print("original dic \n" ,d)
    sor = sorted(d, key=itemgetter(0))
    print("in ascending order : " ,sor) 
    sor = sorted(d, key=itemgetter(0), reverse=True)
    print("in reverse = " ,sor)
    d.update({"v":7})
    print("updated directery" ,d)
    d1 = {"x":9}
    d.update(d1)
    print("concetinated dir = \n" ,d)
    d2 = {"r":12, "s":14, "u":333}
    d2.update(d)
    print("merged dirs are = \n" )
    print(d2)
    

    祝你好运