我写了一个程序,但是不能正常工作

时间:2019-10-03 21:49:34

标签: python python-3.x list dictionary

如何只打印()该词典中的第一个列表?

drugs = {
    "Stimulants":["Ecstsy","Cocaine","Amphetamine"],#list only that I want to print()
    "Depressants":["Marijuana","Benzodiazepine","Heroin","Morphine"],
    "Hallucinogens":["LSD","Marijuana","Psilocybin"],
    "Opioids":["Codeine","Heroin","Methadone","Ocycodone"],
}

因为我写的时候。

if customer_0 == "Stimulants":
        print("\nFrom Stimulants we have:")
        for drugs_0 in drugs.values():
            print("\t" + str(drugs_0[0]))

它支持我

"Ecstsy",
"Marijuana",
"LSD",
"Codeine"

但是我要它打印出来。

"Ecstsy",
"Cocaine",
"Amphetamine",

1 个答案:

答案 0 :(得分:0)

这将起作用:

items = drugs.get("Stimulants")
    for item in items:
        print(item)

输出:

Ecstsy
Cocaine
Amphetamine