创建以混合大小写的单词作为第一个元素,以完全小写的字符串作为第二个元素的元组
Create tuples that have a word of mixed case as the first element and an entirely lowercase string as the second element
创建以混合大小写的单词作为第一个元素,以完全小写的字符串作为第二个元素的元组。列出 space 个分隔的单词供您输入。
这是我目前所拥有的:
result = {}
words = ("VaneSSa likES tO RuN")
words1 = words.split()
for dic in words1:
result[dic] = dic.lower()
print(result)
输出:
{'VanEssa': 'vanessa', 'likeS': 'likes', 'tO': 'to', 'Run': 'run'}
我意识到这是一本字典,练习是让它成为一个元组。这是他们使用的示例:
"例如,如果给定字符串:
RADIO aStRoNoMy Pulsar
您需要使用元组理解获取以下元组元组:
(('RADIO', 'radio'), ('aStRoNoMy', 'astronomy'), ('Pulsar', 'pulsar'))" =12=]
我也参考了
但我很难理解如何将其从字典更改为元组
字典具有将所有项目作为元组获取的功能。使用:tuple(result.items())
items
函数的结果是一个元组列表。要改为使用元组的元组,我使用 tuple
函数进行转换。
运行:
result = []
words = ("VaneSSa likES tO RuN")
words1 = words.split()
for dic in words1:
result.append(tuple([dic, dic.lower()]))
result = tuple(result)
试试这个:
words = input("Enter words to transform to lowercase here. Separate them with spaces:")
result = tuple((word, word.lower()) for word in words.split())
print(result)
给你:
(("RADIO", "radio"), ("aStRoNoMy", "astronomy"), ("Pulsar", "pulsar"))
创建以混合大小写的单词作为第一个元素,以完全小写的字符串作为第二个元素的元组。列出 space 个分隔的单词供您输入。
这是我目前所拥有的:
result = {}
words = ("VaneSSa likES tO RuN")
words1 = words.split()
for dic in words1:
result[dic] = dic.lower()
print(result)
输出: {'VanEssa': 'vanessa', 'likeS': 'likes', 'tO': 'to', 'Run': 'run'}
我意识到这是一本字典,练习是让它成为一个元组。这是他们使用的示例:
"例如,如果给定字符串:
RADIO aStRoNoMy Pulsar
您需要使用元组理解获取以下元组元组:
(('RADIO', 'radio'), ('aStRoNoMy', 'astronomy'), ('Pulsar', 'pulsar'))" =12=]
我也参考了
但我很难理解如何将其从字典更改为元组
字典具有将所有项目作为元组获取的功能。使用:tuple(result.items())
items
函数的结果是一个元组列表。要改为使用元组的元组,我使用 tuple
函数进行转换。
运行:
result = []
words = ("VaneSSa likES tO RuN")
words1 = words.split()
for dic in words1:
result.append(tuple([dic, dic.lower()]))
result = tuple(result)
试试这个:
words = input("Enter words to transform to lowercase here. Separate them with spaces:")
result = tuple((word, word.lower()) for word in words.split())
print(result)
给你:
(("RADIO", "radio"), ("aStRoNoMy", "astronomy"), ("Pulsar", "pulsar"))