如何在raw_input中包含列表?关闭

时间:2017-01-05 01:26:17

标签: python python-2.7

我知道有很多类似的问题,但他们不回答我的问题。我想知道如何在raw_input中包含列表。我有我的代码:

Choices = ['random_one', 'random_two']
Choice = raw_input("The choices are:" + "and ".join(Choices) + ": ")

此代码有效,但我遇到了问题。它的打印方式如下:

The choices are and :

我希望它像这样打印:

The choices are random_one and random_2

提前致谢。

3 个答案:

答案 0 :(得分:3)

str.join()方法可以派上用场:

printf

结果:

Choice_1 = raw_input("The choices are: " + ", ".join(Choices) + ": ")

答案 1 :(得分:3)

就个人而言,我认为最简单的方法就是

Choice_1 = raw_input("The choices are: " + (", ".join(Choices)))

这样做是使用", "加入列表中的每个项目。括号是必要的,否则它将首先读取", "

答案 2 :(得分:0)

您可以根据需要格式化字符串:

let itemNames = ["one","two","three"]

// Eureka From Set-up
form
    +++ Section("Select item values")

    for itemName in itemNames{

    form.last

        <<< StepperRow() {
            $0.tag = itemName
            $0.title = itemName
            $0.value = 0
        }
    }

如果您不知道列表只需将其放入for循环中,或者甚至更好地删除字符串列表:

formatted_list = '{}, {}, {}'.format(var[0], var[1], var[2])
print(format)