gpt4 book ai didi

python - 如何在函数参数中留出空格?

转载 作者:行者123 更新时间:2023-11-30 21:59:49 29 4
gpt4 key购买 nike

当以下行的两个单词之间有空格时,以下代码如何正确工作以使每个参数都等于 true?

custom = detector.CustomObjects(cell phone=True, car=True)

这来自 ImageAI 库,下面是示例:

"""
There are 80 possible objects that you can detect with the
ObjectDetection class, and they are as seen below.

person, bicycle, car, motorcycle, airplane,
bus, train, truck, boat, traffic light, fire hydrant, stop_sign,
parking meter, bench, bird, cat, dog, horse, sheep, cow, elephant, bear, zebra,
giraffe, backpack, umbrella, handbag, tie, suitcase, frisbee, skis, snowboard,
sports ball, kite, baseball bat, baseball glove, skateboard, surfboard, tennis racket,
bottle, wine glass, cup, fork, knife, spoon, bowl, banana, apple, sandwich, orange,
broccoli, carrot, hot dog, pizza, donot, cake, chair, couch, potted plant, bed,
dining table, toilet, tv, laptop, mouse, remote, keyboard, cell phone, microwave,
oven, toaster, sink, refrigerator, book, clock, vase, scissors, teddy bear, hair dryer,
toothbrush.

To detect only some of the objects above, you will need to call the CustomObjects function and set the name of the
object(s) yiu want to detect to through. The rest are False by default. In below example, we detected only chose detect only person and dog.
"""
custom = detector.CustomObjects(person=True, dog=True)

任何帮助将不胜感激。

最佳答案

Python 的语法要求关键字参数是有效的标识符,不允许使用空格。您需要解压一个显式字典。

custom = detector.CustomObjects(**{"cell phone": True, "car": True})

作为演示,接受或拒绝手机完全取决于可调用对象(而不是语言语义问题):

>>> def foo(**kwargs):
... for k, v in kwargs.items():
... print("Key: {}".format(k))
... print("Value: {}".format(v))
...
>>> foo(**{"cell phone": 9})
Key: cell phone
Value: 9
<小时/>

请注意,除了文档之外,actual argument defined by CustomObjectscell_phone,而不是 cellphone。如果传递 cell_phone 的值,则该方法返回的 dict 包含字符串 cell Phone 作为键。

关于python - 如何在函数参数中留出空格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54494924/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com