python中的三元表达式

1.获取字符串三元表达式

res='phones':[
{
'phone_name':'register-1-00100',
'phone_id':'e7923edc83634bc99b73c5cae5659673',
'node_id':'ef5c475a87154ddcb67fc80d2b89e705',
'server_id':'ef5c475a87154ddcb67fc80d2b89e705',
'imei':'869287404004197',
'phone_model_name':'kg1.cp.c100.d10SSD.e0v100',
'image_id':'20040120200426b10b000793dc8e2514',
'vnc_enable':'false',
'status':2,
'type':0,
'metadata':{
'product_id':'OFFI454827367571218435',
'order_id':'CS2004301457RZBA9'
},
'create_time':'2020-04-30T06:58:00Z',
'update_time':'2020-04-30T07:17:44Z'
},
{
'phone_name':'register-1-00099',
'phone_id':'2ca3211c33dc43b1930af13326594e7c',
'node_id':'ef5c475a87154ddcb67fc80d2b89e705',
'server_id':'ef5c475a87154ddcb67fc80d2b89e705',
'imei':'869287834137195',
'phone_model_name':'kg1.cp.c100.d10SSD.e0v100',
'image_id':'20040120200426b10b000793dc8e2514',
'vnc_enable':'false',
'status':2,
'type':0,
'metadata':{
'product_id':'OFFI454827367571218435',
'order_id':'CS2004301457RZBA9'
},
'create_time':'2020-04-30T06:58:00Z',
'update_time':'2020-04-30T07:17:43Z'
}]

phone_id_list=[]
phone_id_list.extend([phone.get("phone_id") for phone in res.get("phones")])
print(len(phone_id_list),phone_id_list)
phone_id_list = ['e7923edc83634bc99b73c5cae5659673', '2ca3211c33dc43b1930af13326594e7c']

2.运算符三元表达式

a = 1
b = 2
h = ""
h = a-b if a>b else a+b
print(h)
h=3
posted @ 2020-04-30 23:26  莫贞俊晗  阅读(679)  评论(0编辑  收藏  举报