Python 字典和多个 dict[]

标签 python dictionary

因此,对于一项作业,我必须接受用户输入(例如 1146),然后让 python 将其转换为“一一四六”

到目前为止我从字典开始

dict = { 1:"one", 2:"two", 3:"three", 4:"four", 5:"five",
     6:"six", 7:"seven", 8:"eight", 9:"nine"};

然后请求输入输出

x = raw_input(("enter number: "))
y = x.split

我想知道 dict[y] 如何或是否可以采用变量,例如用户输入后 y 何时被拆分。谁能帮忙?

最佳答案

一些提示:

  1. 在您的字典中包含 0: "zero"

  2. y = x.split
    

    不调用方法 x.split -- 它只是将方法本身分配给 y

  3. 要获取字符串的字符,您可以遍历它:

    for c in my_string:
        print c
    

关于Python 字典和多个 dict[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9559146/

相关文章:

Python递归仅在必要时评估

Python:如何从键值字典列表构造元组值字典?

python - 将字典拆分为现有列

python - 有什么比 dict.get(<thing>, dict.get(<other>)) 更好的吗?

python - 添加具有相同键和 int 类型值的字典列表

c++ - QML 映射适当的标记实例化

python - 如何从递归 Python 函数返回值?

python - crypt.crypt 引发 OSError : Invalid Argument

python - Pyplot 散点图,使用facecolors ='none' ,并将边缘颜色保持为默认的确定性标记颜色选择

python - 使用科学记数法,两个不同的 float 计算结果相等