Python 对于列表中的元素,如何将strip函数与split函数结合使用?

Python 对于列表中的元素,如何将strip函数与split函数结合使用?,python,dictionary,strip,Python,Dictionary,Strip,我正在制作一个程序,创建一个大学列表的字典,以及它们在申请人偏好方面的排名 以下是我创建的内容: list1=input("Please write some universities you want to attend, separated by commas, with your first choice first.\n") list1=list1.strip() list1=list1.split(",") ranking=range(1,l

我正在制作一个程序,创建一个大学列表的字典,以及它们在申请人偏好方面的排名

以下是我创建的内容:

list1=input("Please write some universities you want to attend, separated by commas, with your first choice first.\n")

list1=list1.strip()
list1=list1.split(",")

ranking=range(1,len(list1)+1)
dictionary_of_colleges={rank:school for rank,school in zip(ranking,list1)}
print(dictionary_of_colleges)
它主要做我想让它做的事。唯一的问题是,当我输入一个大学列表时,在大学名称前有一个空白,我似乎不知道如何去掉这个空白

例如,list1的输入:

UPenn, Georgia Tech, Texas, Eastern, NW Missouri State
获取此输出:

{1: 'UPenn', 2: ' Georgia Tech', 3: ' Texas', 4: ' Eastern', 5: ' NW Missouri State'}
虽然我可以要求用户输入大学名称,但逗号后不留空格,我希望程序本身能够去掉空格

正如你所看到的,我尝试过使用strip函数,但它似乎不起作用。 请告知。谢谢大家!

变化

dictionary_of_colleges={rank:school for rank,school in zip(ranking,list1)}

strip函数删除字符串开头和结尾的空格。在您的使用中,它是从整个学校列表的开头和结尾删除空格,而不是从每个学校删除空格

还请注意,您可以省略
排名的创建
,只需使用
枚举(列表1,1)
,如中所示

dictionary_of_colleges={rank:school.strip() for rank,school in enumerate(list1,1)}
其中枚举中的1告诉它从索引1开始

dictionary_of_colleges={rank:school.strip() for rank,school in enumerate(list1,1)}