|
发表于 2022-8-4 11:07:49
|
显示全部楼层
- def check(c):
- r = False
- if '[' in c or '{' in c:
- try:
- x = eval(c)
- if '[' in c or '{' in c and ':' in c:
- r = True
- except:
- pass
- return r
- a = ['[1, 2, 3]' , '[1, 2, 3' , '{123}' , '{a : None}' , '[12 : 3]' , '{ 12 : 3 }' , 'a' , '123']
- for c in a:
- print("'" + c + "'" + " : # 合法") if check(c) else print("'" + c + "'" + " : # 不合法")
复制代码
运行实况:
- D:\[00.Exerciese.2022]\Python>python a.py
- '[1, 2, 3]' : # 合法
- '[1, 2, 3' : # 不合法
- '{123}' : # 不合法
- '{a : None}' : # 不合法
- '[12 : 3]' : # 不合法
- '{ 12 : 3 }' : # 合法
- 'a' : # 不合法
- '123' : # 不合法
- D:\[00.Exerciese.2022]\Python>
复制代码 |
|