Randomly take dictionary key and value

Sometimes it is necessary to randomly take the value in the key or value of the dictionary, as follows.

compan={"9101": ["9101001", "9101002", "9101003", "9101004", "9101005"],
     "9102": ["9102001", "9102002", "9102003", "9102004", "9102005"],
     "9103": ["9103001", "9103002", "9103003", "9103004", "9103005"],
     "9104": ["9104001"],
     "9105": ["9105001", "9105002", "9105003", "9105004", "9105005"],
     "9106": ["9106001", "9106002", "9106003", "9106004", "9106005"],
     "9107": ["9107001", "9107002", "9107003", "9107004", "9107005"]}

Choose random.choice

import random
key=random.choice(list(compan))
value=random.choice(compan[key])

Result presentation
Insert picture description here

Guess you like

Origin blog.csdn.net/kairui_guxiaobai/article/details/106696682