Python 列表的字符串累加器

Python String Accumulator for Lists

我仍在努力学习 Python 基础知识,但遇到了麻烦。 我有一个 class 的作业,需要一些指导。

我正在尝试从用户输入中获取整数值,然后将它们添加到 用户想要的尽可能多的项目的字符串列表。

我可以轻松地将用户输入的字符串值添加到列表中,但结果是这样的:

['Test1','Test2','Test3']

我想将其输出为:

{Test1, Test2, Test3}

这是我目前的情况:

# Tests before creating function
# definition. 
salesFigures = [] 
maxLengthList = 3
while len(salesFigures) < maxLengthList:
    item = input("Enter your sales figure to be added: ")
    salesFigures.append(item)
print("These are your sales figures:")
print(salesFigures)

您可以使用 join() + 字符串格式:

some_list = ['Test1','Test2','Test3']
desired_results = "{{{}}}".format(', '.join(some_list))
print desired_results

输出:

{Test1, Test2, Test3}