繁体   English   中英

如何从 Python output 中删除 [ ] 和 ''

[英]How to remove [ ] and '' from Python output

这是一些基于键打印出字典值的代码,我尝试了一些方法,例如 .remove() 但找不到不需要整个 class 来剥离字典的好解决方案。

这是我的代码:

def WriteoutLines(TheDict, choice):

    DictValues = TheDict.copy()
    Result = TheDict.get(choice)
    print(Result)


TheDict = {"pres": ["the President right now is",
                    "Joe Biden"],
           "FSUNick": ["the seminoles",
                       "unconquerd"],
           "class": ["introcution to Python"]
           }
TheKeys = list(TheDict.keys())

Done = False
while (not Done):
    print("we have these keys")
    for i in range(0, len(TheKeys)):
        print(TheKeys[i])
    print("enter zz to end program or the word you want a listing for")
    choice = input("enter the string you want typed out")

    if (choice in TheDict.keys()):
        WriteoutLines(TheDict, choice)

    elif (choice == "zz"):
        break
    else:
        print("The was not a legal choice")

尝试str.join()

the_dict = {
    "pres": ["the President right now is", "Joe Biden"],
    "FSUNick": ["the seminoles", "unconquerd"],
    "class": ["introcution to Python"]
}

while True:
    print("we have these keys")
    print("\n".join(the_dict.keys()))
    print("enter zz to end program or the word you want a listing for")
    choice = input("enter the string you want typed out")
    if choice == "zz":
        break
    print(" ".join(the_dict.get(choice) or ["That was not a legal choice"]))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM