首页 > 试题广场 >

查字典

[编程题]查字典
  • 热度指数: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 
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)
这样子把最后一个"down"考虑到了,但是应该不够简洁,不知各位有什么改进建议
dic = {
    "a": ["apple", "abandon", "ant"],
    "b": ["banana", "bee", "become"],
    "c": ["cat", "come"],
    "d": "down",
}
s = input()
for i in dic:
    if i == s:
        for a in list(dic[i]):
            if type(dic[i]) == list:
                print(a, end=" ")
            else:
                print(dic[i], end=" ")
                break

发表于 2025-03-18 17:49:41 回复(0)
dicts = {'a': ['apple', 'abandon', 'ant'], 'b': ['banana', 'bee', 'become'], 'c': ['cat', 'come'], 'd': 'down'}
n = input()
print(" ".join(dicts[n]))

发表于 2024-10-01 11:37:57 回复(0)
mydict={'a': ['apple', 'abandon', 'ant'], 'b': ['banana', 'bee', 'become'], 'c': ['cat', 'come'], 'd': 'down'}
niu = input()
for x in mydict:
    if niu == x:
        print(' '.join(mydict[x]))
发表于 2024-07-23 19:38:39 回复(0)
通过直接在d上加[]解决输入d输出d o w n的问题?
a = {
"a": ["apple", "abandon", "ant"],
"b": ["banana", "bee", "become"],
"c": ["cat", "come"],
"d": ["down"],
}
print(' '.join(a[input()]))
发表于 2024-06-11 10:43:08 回复(1)
original_dict = {'a': ['apple', 'abandon', 'ant'], 'b': ['banana', 'bee', 'become'], 'c': ['cat', 'come'], 'd': 'down'}
letter = input()

if isinstance(original_dict[letter], list):
    words = original_dict[letter]
    print(' '.join(words))
else:
    print(original_dict[letter])

编辑于 2024-04-11 15:49:12 回复(1)
dic = {
    "a": ["apple", "abandon", "ant"],
    "b": ["banana", "bee", "become"],
    "c": ["cat", "come"],
    "d": "down",
}
key = input()
for i in dic[key]:
    print(i,"",end="")           %%print默认元素的分隔符为空格 先输出i 由于第二个元素是空字符串 所以是输出分隔符空格 
发表于 2024-04-02 23:53:56 回复(0)
letter_dict = {'a': ['apple', 'abandon', 'ant'],
               'b': ['banana', 'bee', 'become'],
               'c': ['cat', 'come'],
               'd': 'down',
               }
str1 = input()
if str1 in letter_dict.keys():
    if type(letter_dict[str1]) is list:
        for i in letter_dict[str1]:
            print(i, end=' ')

    elif type(letter_dict[str1]) is str:
        print(letter_dict[str1])








发表于 2024-03-26 21:48:29 回复(0)
'''
    get 方法是 Python 字典的一个内置方法,
    它允许我们通过键(在这里是字母)来查找并返回相应的值。
    如果键存在,则返回该键对应的值;
    如果键不存在,返回None
    isinstance() 是一个内建函数,
    它接收两个参数:要检查的对象和要检查的类型(或类型元组)。
    当对象属于指定类型时,它返回 True,
    否则返回 False
'''
dict_1 = {'a':['apple', 'abandon', 'ant'],
    'b':['banana', 'bee', 'become'],
    'c':['cat', 'come'], 
    'd':'down'
    }
str_1 = input()
word_1 = dict_1.get(str_1)
if isinstance(word_1,list):
    for i in word_1:
        print(i,end=' ')
if isinstance(word_1,str):
    print(word_1)

编辑于 2024-03-13 22:28:28 回复(0)
my_dict = {
    'a': ['apple', 'abandon', 'ant'],
    'b': ['banana', 'bee', 'become'],
    'c': ['cat', 'come'],
    'd': 'down'
}

x = input()

if x in my_dict.keys():
    for y in my_dict[x]:
        print(y,end=" ")
编辑于 2024-02-17 21:15:06 回复(0)
b = input()
a = {
    "a": ["apple", "abandon", "ant"],
    "b": ["banana", "bee", "become"],
    "c": ["cat", "come"],
    "d": "down",
}

for i in a.keys():
    if b == i :
        if type(a[i])==list:
            for j in a[i]:
                print(j, end=' ')
        else:
            print(a[i])
发表于 2023-10-08 17:41:24 回复(0)
myDict = {
    "a": ["apple", "abandon", "ant"],
    "b": ["banana", "bee", "become"],
    "c": ["cat", "come"],
    "d": ["down"],
}
letter = input()
print(' '.join(myDict[letter]))

发表于 2023-08-25 21:06:20 回复(0)
解决输入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)
d={'a': ['apple', 'abandon', 'ant'], 'b': ['banana', 'bee', 'become'], 'c': ['cat', 'come'], 'd': 'down'}
s=input()
for i in d[s]:
    print(i,end=' ')
发表于 2023-07-24 19:21:07 回复(0)
dict1 = {'a': ['apple', 'abandon', 'ant'],
'b': ['banana', 'bee', 'become'],
'c': ['cat', 'come'], 
'd': 'down'}
n = input()
if n in dict1.keys():
    for i in dict1[n]:
        print(i, end=' ')

发表于 2023-07-21 17:03:45 回复(0)
dict1 = {'a': ['apple', 'abandon', 'ant'], 'b': ['banana', 'bee', 'become'], 'c': ['cat', 'come'], 'd': 'down'}
char = input()
for i in range(0, len(dict1[char])):
    print(dict1[char][i], end = ' ')
发表于 2023-06-27 10:47:01 回复(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)
# 方法1 标准化输入
words = {'a': ['apple', 'abandon', 'ant'], 'b': ['banana', 'bee', 'become'], 'c': ['cat', 'come'], 'd': 'down'}
print(' '.join(words[sys.stdin.readline().strip()]))

# 方法2 input输入
words = {'a': ['apple', 'abandon', 'ant'], 'b': ['banana', 'bee', 'become'], 'c': ['cat', 'come'], 'd': 'down'}
print(' '.join(words[input()]))

发表于 2023-06-13 23:40:03 回复(0)
my_dict={'a': ['apple', 'abandon', 'ant'], 'b': ['banana', 'bee', 'become'], 'c': ['cat', 'come'], 'd': 'down'}
letter=input()
for key ,value in my_dict.items():
    if letter in key:
        for i in value:
            print(i,end=" ")

发表于 2023-05-27 17:41:46 回复(0)