在Python中,如何将包含名称(1个或多个单词)和数字的单行输入字符串拆分为[“名称”、“数字”]?

在Python中,如何将包含名称(1个或多个单词)和数字的单行输入字符串拆分为[“名称”、“数字”]?,python,string,dictionary,split,mapping,Python,String,Dictionary,Split,Mapping,我是个新手。我在一个电话簿问题的测试中失败了。根据问题,用户需要输入一行输入,其中包含一个名称(可以是一个或多个单词),后跟一个数字。我必须将输入拆分为[“名称”、“编号”]并将其存储在字典中。请注意,名称将包含一个或多个单词(例如:小约翰·科纳或阿波罗·克里德)。我对分裂部分感到困惑。我尝试了split()函数和re.split()。我不确定我能解决这个问题 示例输入1:david james 93930000 样本输入2:hshhs KSSK sshs 99383000 输出:num={“d

我是个新手。我在一个电话簿问题的测试中失败了。根据问题,用户需要输入一行输入,其中包含一个名称(可以是一个或多个单词),后跟一个数字。我必须将输入拆分为[“名称”、“编号”]并将其存储在字典中。请注意,名称将包含一个或多个单词(例如:小约翰·科纳或阿波罗·克里德)。我对分裂部分感到困惑。我尝试了split()函数和re.split()。我不确定我能解决这个问题

示例输入1:david james 93930000

样本输入2:hshhs KSSK sshs 99383000

输出:num={“david james”:“93930000”,“hshhs KSSK sshs”:“99383000”}

我需要将其存储在字典中,其中key:value是“david james”:“93930000”

请帮忙。多谢各位

====>我找到了解决办法 所以我假设输入是来自用户的,如果是这样的话 您是否可以将代码中的格式更改为其他格式 与此类似。您可以根据需要的输入数量更改范围

这应该以您提到的方式打印结果

请检查更新的答案,如果这是你正在寻找的 因为


输入2的可能键:值是什么?我不确定你的意思。我是python的新手。我已重新措辞了这个问题。如果你有解决办法,一定要告诉我。他一句话就能得到信息。不是来自不同行中的用户。在这种情况下,他可以将信息附加到列表中,并在上面的函数中循环。结果应该是一样的。他能详细说明一下他是如何获得输入数据的吗?我是在一行中获得输入信息的(这是根据一个黑客等级问题)。输入由一个人的名字(可以是一个或多个单词)和他的数字组成,后面是一行。因此,我希望将输入拆分为[“name”,“number”]并将其存储在字典中。我在分裂部分面临困难。我无法使用常规的split()函数。请告诉我,我已经发布了一个新的答案,如果它是您需要的,请告诉我。如果这回答了你的问题,如果你把它作为答案,我将不胜感激。谢谢嘿谢谢你花时间给我一个详细的解释。然而,如果这个名字只有一个词“Tamer”或者甚至三个词“Tamer Jar Jr.”会怎么样?如果我没有错,上面的代码索引将生成一个错误。我正在寻找一个更通用的代码,其中可以提取“名称”(可能是一个或多个单词)和数字,并将其作为dict()中的key:value放置。嘿,我找到了一个答案。不确定它是否正确。
if __name__ == '__main__':
    N=int(input())
    phonebook={}
    (*name,num) = input().split()
    name = ''.join(map(str,name)
    phonebook.update({name:num})
print(phonebook)
name = {}

for i in range(5):
    student_name = input("Enter student's name: ")
    student_mark = input("Enter student's mark: ")
    name[student_name.title()] = student_mark

print(marks)
# Sample text in a line...
# With a name surname and number

txt = "Tamer Jar 9000"

# We define a dictionary variable
name_dictionary = {}

# We make a list inorder to appened the name and surname to the list
name_with_surname = []

# We split the text and if we print it out it should look something like this
# "Tamer", "Jar", "9000"
# But you need the name and surname together so we do that below
x = txt.split()

# We take the first value of the split text which is "Tamer"
# And we take the second value of the split text us "Jar"
name = x[0]
surname = x[1]
# And here we append them to our list 
name_with_surname.append(name + " " + surname)
#print(name_with_surname)


# Finally here to display the values in a dictionary format
# We take the value of the list which is "Tamer Jar" and the value of the number "9000"
name_dictionary[name_with_surname[0]] = x[2]
print(name_dictionary)