In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how Python converts numbers into lists. I hope you will get something after reading this article. Let's discuss it together.
1. Digitizedef digitize (n): return list (map (int, str (n)) # EXAMPLESdigitize (123) # [1,2,3]
The body logic of this function is to convert the input number into a string, then use the map function to convert the string into int type in order, and finally convert it to list.
Why do the numbers you enter get a list after this conversion? This is because str in Python is an iterable type. So str can use the map function, while map returns an iterator, which is also an iterative type. Finally, use this iterator to build a list.
2. Python determines whether the object can be iterated.
At present, the common method of judgment on the network is to use the Iterable type of the module of collections.abc (which was part of collections before 3.3.3).
From collections.abc import Iterableisinstance ('abc', Iterable) # Trueisinstance (map (int,a), Iterable) # True
Although there is no problem using this in the current scenario, according to the official documentation, the only reliable way to detect whether an object is an iterable is to call iter (obj).
Class collections.abc.Iterable
ABC for classes that provide the _ iter__ () method.
Checking isinstance (obj, Iterable) detects classes that are registered as Iterable or that have an _ iter__ () method, but it does not detect classes that iterate with the _ getitem__ () method. The only reliable way to determine whether an object is iterable is to call iter (obj).
> iter ('abc') after reading this article, I believe you have a certain understanding of "how Python converts numbers into lists". If you want to know more about it, welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.