Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
209 views
in Technique[技术] by (71.8m points)

python - How to iterate this switchcase?

I tried to create this switcher in python:

def mainswitcher(i):
        switcher={
                #SelfOptions#
                "test":              "SomeStringherefortest",
                "test2":          "SomeStringherefortest2",
                "test3":           "SomeStringherefortest3" }

Now I have to get a list of the names. So I need list = [test,test2,test3]

How does this work? Does it even work? I know I cant loop though an object - but is there a way around?

question from:https://stackoverflow.com/questions/65836883/how-to-iterate-this-switchcase

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your switcher is a dictionary. It has keys and their values.

To get a list of the keys, simply call switcher.keys().

Also, you can actually loop through most objects that have structered elements (implement Iterable). For dictionaries, you'd use for key,value in switcher.items(): or switcher.values() respectively.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...