简体   繁体   English

在python中将对象转换为列表的最佳方法是什么?

[英]What's the best way convert object to list in python?

So I receive parameter from json string, and convert it to Json object. 所以我从json字符串接收参数,并将其转换为Json对象。

In this object, I have a field named DepartmentIds . 在此对象中,我有一个名为DepartmentIds的字段。

This DepartmentIds may an int or a list of int . DepartmentIds可以在intlistint

So I do a type check to see if DepartmentIds is instance of int . 因此,我进行类型检查以查看DepartmentIds是否为int实例。

if isinstance(DepartmentIds,int) :
    DepartmentIds = [DepartmentIds]

Any syntactic sugar exists? 是否存在语法糖?

DepartmentIds = [DepartmentIds] if isinstance(DepartmentIds,int) else DepartmentIds

没有更好的..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Python2.7中将列表转换为字典的最佳方法是什么 - What’s the best way to Convert a list to dict in Python2.7 将zope DateTime对象转换为Python datetime对象的最佳方法是什么? - What is the best way to convert a zope DateTime object into Python datetime object? 将Python中的打印列表转换回实际列表的最佳方法是什么? - What is the best way to convert a printed list in Python back into an actual list 在python中将docx对象转换为pdf的最佳方法是什么 - What is the best way to convert a docx object to a pdf in python 用Django / python在Amazon s3中转换文件的最佳方法是什么? - What is the best way to convert a file in amazon s3 with Django/python? 将列表转换为 python 列表的最快方法是什么? - What's the fastest way to convert a list into a python list? 在Python中通过网络发送对象的最佳方法是什么? - What's the best way to send an object over a network in Python? 在词典列表中搜索Python字典值的最佳方法是什么? - What's the best way to search for a Python dictionary value in a list of dictionaries? 在python中接受超过1个列表的输入的最佳方法是什么? - What's best way to take input of more than 1 list in python? 在 Python 中比较列表切片的最佳方法是什么? - What's the best way of comparing slices of a list in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM