简体   繁体   中英

converting an int to a list containing the int

How can i convert an int into a list containing that int.

I've code:

y=x

Here the odds of x being a int to a list is 95% to 5%. if x is a int then fine. but if x is a list, i had to use:

y=list(x)

but in this case it'll create problem for int.

I can declare x as a list for all the cases. but for just 10% of chances using a list is quite unnecessary.

Is there any other way, if i can convert a int to list (not to string) so that i can use y=list(x) all the time?

Just put square brackets around it:

y = [x]

If you want to check whether your input is an integer or not, you can use isinstance() :

if isinstance(x, int):
    y = [x]
else:
    y = list(x)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM