Python - 创建一个字典,以键为第一个字符,值作为以该字符开头的单词
在本文中,我们将编写一个Python程序来创建一个字典,以键为第一个字符,值为以该字符开头的单词。
Python中的Dictionary是一个无序的数据值集合,用于像map一样存储数据值,与其他Data Types只保存单个值作为元素不同,Dictionary保存着key: value对。字典中提供了键值,以使其更加优化。
例子:
Input: Hello World
Output: {'H': ['Hello'],
'W': ['World']}
Input: Welcome to GeeksForGeeks
Output: {'W': ['Welcome'],
't': ['to'],
'G': ['GeeksForGeeks']}
方法:
- 我们将字符串保存在一个变量中并声明一个空字典。
- 然后我们将字符串拆分为单词并形成这些单词的列表。
- 对于每个单词,我们将检查该单词的关键字是否存在。
- 如果不是,那么我们将把那个键和词添加到字典中,如果它已经存在,那么我们会将该词附加到该键的子列表中。
下面是上述方法的实现:
Python3
# Python program to Create a Dictionary with Key as First
# Character and Value as Words Starting with that Character
# Driver Code
# Declaring String Data
string_input = '''GeeksforGeeks is a Computer Science portal for geeks.
It contains well written, well thought and well explained
computer science and programming articles, quizzes etc.'''
# Storing words in the input as a list
words = string_input.split()
# Declaring empty dictionary
dictionary = {}
for word in words:
# If key is not present in the dictionary then we
# will add the key and word to the dictionary.
if (word[0] not in dictionary.keys()):
# Creating a sublist to store words with same
# key value and adding it to the list.
dictionary[word[0]] = []
dictionary[word[0]].append(word)
# If key is present then checking for the word
else:
# If word is not present in the sublist then
# adding it to the sublist of the proper key
# value
if (word not in dictionary[word[0]]):
dictionary[word[0]].append(word)
# Printing the dictionary
print(dictionary)
Python3
# Python program to Create a Dictionary with Key as First
# Character and Value as Words Starting with that Character
# Driver Code
# Declaring String Data
string_input = '''GeeksforGeeks is a Computer Science portal for geeks.
It contains well written, well thought and well explained
computer science and programming articles, quizzes etc.'''
# Storing words in the input as a list
words = string_input.split()
# Declaring empty dictionary
dictionary = {}
for word in words:
# If key is not present in the dictionary then we
# will add the key and word to the dictionary.
if (word[0].lower() not in dictionary.keys()):
# Creating a sublist to store words with same
# key value and adding it to the list.
dictionary[word[0].lower()] = []
dictionary[word[0].lower()].append(word)
# If key is present then checking for the word
else:
# If word is not present in the sublist then
# adding it to the sublist of the proper key
# value
if (word not in dictionary[word[0].lower()]):
dictionary[word[0].lower()].append(word)
# Printing the dictionary
print(dictionary)
输出:
{'G': ['GeeksforGeeks'],
'i': ['is'],
'a': ['a', 'and', 'articles,'],
'C': ['Computer'],
'S': ['Science'],
'p': ['portal', 'programming'],
'f': ['for'],
'g': ['geeks.'],
'I': ['It'],
'c': ['contains', 'computer'],
'w': ['well', 'written,'],
't': ['thought'],
'e': ['explained', 'etc.'],
's': ['science'],
'q': ['quizzes']}
您可以看到在上面程序的输出中,以G开头的单词有两个键“ G ”和“ g ”,因此为了消除该问题并使代码不区分大小写,我们将使用Python中的lower()函数。我们将用小写保存所有键,这样无论何时我们检查键时,以“ G ”和“ g ”开头的单词都将位于同一个键下。下面是实现:
蟒蛇3
# Python program to Create a Dictionary with Key as First
# Character and Value as Words Starting with that Character
# Driver Code
# Declaring String Data
string_input = '''GeeksforGeeks is a Computer Science portal for geeks.
It contains well written, well thought and well explained
computer science and programming articles, quizzes etc.'''
# Storing words in the input as a list
words = string_input.split()
# Declaring empty dictionary
dictionary = {}
for word in words:
# If key is not present in the dictionary then we
# will add the key and word to the dictionary.
if (word[0].lower() not in dictionary.keys()):
# Creating a sublist to store words with same
# key value and adding it to the list.
dictionary[word[0].lower()] = []
dictionary[word[0].lower()].append(word)
# If key is present then checking for the word
else:
# If word is not present in the sublist then
# adding it to the sublist of the proper key
# value
if (word not in dictionary[word[0].lower()]):
dictionary[word[0].lower()].append(word)
# Printing the dictionary
print(dictionary)
输出:
{'g': ['GeeksforGeeks', 'geeks.'],
'i': ['is', 'It'],
'a': ['a', 'and', 'articles,'],
'c': ['Computer', 'contains', 'computer'],
's': ['Science', 'science'],
'p': ['portal', 'programming'],
'f': ['for'],
'w': ['well', 'written,'],
't': ['thought'],
'e': ['explained', 'etc.'],
'q': ['quizzes']}