将用户输入转换为列表名称 [英] Convert User Input into a List name
问题描述
这是我到目前为止:
TotalLists=int(input("How many Lists are you making?"))
TotalListsBackup=TotalLists
Lists=[]
while TotalLists>0:
ListName=input("What would you like to call List Number "+str(TotalLists))
Lists.append(ListName)
TotalLists=TotalLists-1
TotalLists=TotalListsBackup-1
while TotalLists>=0:
Lists[TotalLists] #I would like to create actual lists out of the list names at this step but I dont know how...
TotalLists=TotalLists-1
TotalLists=TotalListsBackup-1
print("Here are your Lists: ")
while TotalLists>=0:
print(Lists[TotalLists])
TotalLists=TotalLists-1
我想要能够:
- 在列表名称之间创建一个列表
- 能够使用户想要的列表的代码没有cap
考试我想输入:Grocery,
代码将创建一个名为Called Grocery
For example, I want to input: Grocery, The code will create a list Called Grocery
解决方案我有想到:
-
数组? (我从来没有使用过,我对Python编程很新,我不知道太多)
Arrays? (I have never used them, I am very new to Python Programming and I dont know too much)
列表列表? (不知道该怎么做,看起来,但没有得到直接的答案)
Lists of Lists? (Not sure how to do that. Looked it up, but didn't get a straight answer)
使用变量,创建一个名称如下所示的列表:
Using Variables, Creating a list with a name like:
List1[]
并有变量调用:
List1Name=input("What would you like to call list 1?")
我不知道如何创建一个
如果您有任何问题,请问,因为我知道我不善于解释。
If you have any questions please ask, for I know I am not good at explaining.
推荐答案
有趣的是,你标记了这个问题字典,但没有在你的帖子中提到。有人告诉你使用字典吗?这正是你应该做的,像这样(假设TotalLists已经定义):
It's interesting that you have tagged the question "dictionary" but didn't mention that in your post. Did somebody tell you to use a dictionary? That's exactly what you should be doing, like this (assume TotalLists is already defined):
d = {}
for _ in range(TotalLists): # The same loop you have now
ListName = input("whatever...")
d[ListName] = []
最后,您有一个字典d,其中包含用户输入的密钥,以及空列表的值。字典条目数是TotalLists。我忽略了用户两次输入相同名称的可能性。
At the end of this you have a dictionary d containing keys that are the user-entered names, and values that are empty lists. The number of dictionary entries is TotalLists. I'm ignoring the possibility that the user will enter the same name twice.
这篇关于将用户输入转换为列表名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!