为什么在这种情况下需要使用星号?
Why is the use of asterisk necessary in this case?
因为我希望输入仅显示 objects,animals,sports,为什么添加星号使其成为 *categories 能够将类别过滤为 objects,animals,sports 并忽略之后的任何其他词?在这种情况下如何添加星号?
import random
# Store the category and values into a dictionary
categories = {
"objects": ["tables", "ladders", "chairs"],
"animals": ["chicken", "dog", "cat"],
"sports": ["basketball", "soccer", "rugby"]
}
response = input(' One among the following [%s] : \n' % ', '.join(map(str, list((*categories,)))))
细目如下:
import snoop
categories = {
"objects": ["tables", "ladders", "chairs"],
"animals": ["chicken", "dog", "cat"],
"sports": ["basketball", "soccer", "rugby"]
}
snoop.pp.deep(lambda: ', '.join(map(str, list((*categories,)))))
输出:
................... categories = {'objects': ['tables', 'ladders', 'chairs'], 'animals': ['chicken', 'dog', 'cat'], 'sports': ['basketball', 'soccer', 'rugby']}
........... (*categories,) = ('objects', 'animals', 'sports')
....... list((*categories,)) = ['objects', 'animals', 'sports']
... map(str, list((*categories,))) = <map object at 0x10d261520>
', '.join(map(str, list((*categories,)))) = 'objects, animals, sports'
遍历字典会产生它的键,因此 (*categories,) = ('objects', 'animals', 'sports')
。
大部分代码毫无意义:', '.join(categories)
也可以。以下每一项都是独立冗余的,可以省略:
map(str, ...)
list(...)
(*...,)
因为我希望输入仅显示 objects,animals,sports,为什么添加星号使其成为 *categories 能够将类别过滤为 objects,animals,sports 并忽略之后的任何其他词?在这种情况下如何添加星号?
import random
# Store the category and values into a dictionary
categories = {
"objects": ["tables", "ladders", "chairs"],
"animals": ["chicken", "dog", "cat"],
"sports": ["basketball", "soccer", "rugby"]
}
response = input(' One among the following [%s] : \n' % ', '.join(map(str, list((*categories,)))))
细目如下:
import snoop
categories = {
"objects": ["tables", "ladders", "chairs"],
"animals": ["chicken", "dog", "cat"],
"sports": ["basketball", "soccer", "rugby"]
}
snoop.pp.deep(lambda: ', '.join(map(str, list((*categories,)))))
输出:
................... categories = {'objects': ['tables', 'ladders', 'chairs'], 'animals': ['chicken', 'dog', 'cat'], 'sports': ['basketball', 'soccer', 'rugby']}
........... (*categories,) = ('objects', 'animals', 'sports')
....... list((*categories,)) = ['objects', 'animals', 'sports']
... map(str, list((*categories,))) = <map object at 0x10d261520>
', '.join(map(str, list((*categories,)))) = 'objects, animals, sports'
遍历字典会产生它的键,因此 (*categories,) = ('objects', 'animals', 'sports')
。
大部分代码毫无意义:', '.join(categories)
也可以。以下每一项都是独立冗余的,可以省略:
map(str, ...)
list(...)
(*...,)