首页 > 试题广场 >

查字典

[编程题]查字典
  • 热度指数:34798 时间限制:C/C++ 5秒,其他语言10秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
正在学习英语的牛妹笔记本上准备了这样一个字典:{'a': ['apple', 'abandon', 'ant'], 'b': ['banana', 'bee', 'become'], 'c': ['cat', 'come'], 'd': 'down'}。
请你创建这样一个字典,对于牛妹输入的字母,查询有哪些单词?

输入描述:
输入一个字母,必定在上述字典中。


输出描述:
同一行中依次输出每个单词,单词之间以空格间隔。
示例1

输入

a

输出

apple abandon ant 
还是这种最简洁
dict1 = {'a': ['apple', 'abandon', 'ant'], 'b': ['banana', 'bee', 'become'], 'c': ['cat', 'come'], 'd': 'down'}
a = input()
for i in dict1[a]:
    print(i,end=' ')

发表于 2022-08-03 14:47:50 回复(9)
输入d,输出d o w n也能过?
发表于 2022-07-28 17:07:43 回复(7)
解决输入d,输出d o w n问题
方案:使用isinstance()判断value是否为列表类型,从而决定是否进行遍历输出。
d = {'a': ['apple', 'abandon', 'ant'], 
        'b': ['banana', 'bee', 'become'], 
        'c': ['cat', 'come'], 
        'd': 'down'}

word = input()

if isinstance(d[word],list):
    for i in d[word]:
        print(i,end=' ')
else:
    print(d[word])


发表于 2023-07-27 18:17:26 回复(1)
dict1={'a': ['apple', 'abandon', 'ant'], 'b': ['banana', 'bee', 'become'], 'c': ['cat', 'come'], 'd': 'down'}
a=input()
for i in dict1[a]:
    print(i, end=' ')

发表于 2022-07-29 21:28:26 回复(0)
dc = {'a': ['apple', 'abandon', 'ant'], 'b': ['banana', 'bee', 'become'], 'c': ['cat', 'come'], 'd': 'down'}
print(' '.join(dc[input()]))

发表于 2022-08-29 19:41:12 回复(2)
a = {'a': ['apple', 'abandon', 'ant'], 'b': ['banana', 'bee', 'become'], 'c': ['cat', 'come'], 'd': ['down']}
b = input()
for i in a[b]:
    print(i,end=' ')
发表于 2022-08-21 16:01:03 回复(0)
dict1 = {
    "a": ["apple", "abandon", "ant"],
    "b": ["banana", "bee", "become"],
    "c": ["cat", "come"],
    "d": "down",
}
if isinstance(value := dict1.get(input()), list):
    for i in value:
        print(i, end=" ")
else:
    print(value)
发表于 2023-12-01 00:39:41 回复(0)
dic = {
    "a": ["apple", "abandon", "ant"],
    "b": ["banana", "bee", "become"],
    "c": ["cat", "come"],
    "d": "down",
}
letter = input()
try:
    # 输入d 输出d o w n 也通过了,有问题
    if type(dic[letter]) == list:
        # 或者判断键值对的值的长度 len(dic[letter])
        # 如果为1,则直接输出
        for word in dic[letter]:
            print(word, end=" ")
    else:
        print(dic[letter])
except:
    print("输入的字母不在字典内")

发表于 2022-12-06 16:32:41 回复(2)
dict_1={'a': ['apple', 'abandon', 'ant'], 
        'b': ['banana', 'bee', 'become'], 
        'c': ['cat', 'come'],
        'd': 'down'}
i=str(input())
while i in dict_1.keys():
    for j in dict_1[i]:
        print(j,end=' ')
    break

发表于 2022-08-05 10:52:25 回复(0)
这样就对了
dict = {'a': ['apple', 'abandon', 'ant'],
         'b': ['banana', 'bee', 'become'],
         'c': ['cat', 'come'], 'd': 'down'}
zimu = input()
if type(dict[zimu]) == list:
    for i in dict[zimu]:
        print(i, end=' ')
else:
    print(dict[zimu])

发表于 2025-03-04 20:59:07 回复(0)
x = {'a':['apple','abandon','ant'], 'b':['banana','bee','become'], 'c':['cat','come'],'d':'down'}
y = x[input()]
if  type(y) is list:
    for i in y:
        print(i,end=' ')
else:
    print(y)
发表于 2023-06-20 17:36:41 回复(0)
word = {
    "a": ["apple", "abandon", "ant"],
    "b": ["banana", "bee", "become"],
    "c": ["cat", "come"],
    "d": "down",
}

s = word[input()]

if type(s) is list:
    print(" ".join(s))
else:
    print(s)

发表于 2023-05-09 16:07:54 回复(1)
dic = {
    "a": ["apple""abandon""ant"],
    "b": ["banana""bee""become"],
    "c": ["cat""come"],
    "d""down",
}
a = input()
for i, j in dic.items():
    if a == i:
        print(*j)
发表于 2022-11-12 20:40:51 回复(0)
user = input()
dicts = {'a': ['apple', 'abandon', 'ant'], 'b': ['banana', 'bee', 'become'], 'c': ['cat', 'come'], 'd': 'down'}
for i in dicts[user]:
    print(i, end=' ')

发表于 2022-09-22 20:23:49 回复(0)
dict1 = {'a': ['apple', 'abandon', 'ant'], 
         'b': ['banana', 'bee', 'become'], 
         'c': ['cat', 'come'], 
         'd': 'down'}
shuru = input()
for i in dict1:
    if i == shuru:
        for j in dict1[i]:
            print(j,end=' ')

发表于 2022-08-04 12:59:20 回复(0)
my_dict={'a': ['apple', 'abandon', 'ant'], 'b': ['banana', 'bee', 'become'], 'c': ['cat', 'come'], 'd': 'down'}
name=input()
for i in my_dict[name]:
    print(i,end=' ')
发表于 2022-08-01 14:45:48 回复(0)
English_dict = {'a': ['apple', 'abandon', 'ant'], 'b': ['banana', 'bee', 'become'], 'c': ['cat', 'come'], 'd': 'down'}
letter_input = input()
for letter in English_dict:
    if letter_input == letter:     
        for word in English_dict[letter]:
            print(word,end=' ')
 #3,4两步合一   if letter_input in English_dict.keys()

发表于 2022-07-30 15:45:57 回复(0)
dict_1={'a': ['apple', 'abandon', 'ant'], 'b': ['banana', 'bee', 'become'], 'c': ['cat', 'come'], 'd': 'down'}
letter=input()
for i in dict_1[letter]:
    print(i,end=' ')
发表于 2025-06-01 17:59:15 回复(0)
dict1={'a': ['apple', 'abandon', 'ant'], 'b': ['banana', 'bee', 'become'], 'c': ['cat', 'come'], 'd': 'down'}
a=input()
if a in dict1:
    for i in dict1[a]:
        print(i,end=' ')
发表于 2025-05-16 21:00:35 回复(0)
a ={'a': ['apple', 'abandon', 'ant'], 'b': ['banana', 'bee', 'become'], 'c': ['cat', 'come'], 'd': 'down'}
b = input()
if b in a :
    value = a[b]
    if isinstance(value,str):
        c = value
    else:
        c = " ".join(value)
    print(c)
else:
    print("输入的键不存在于字典中。")

发表于 2025-05-08 11:20:40 回复(0)