In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to use the itertools.chain () function in the development of big data, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article.
You want to perform the same operation on multiple objects, but these objects are in different containers, and you want the code to avoid writing repetitive loops without losing readability.
From itertools import chain a = [1,2,3,4] b = ['x','y','z'] for x in chain (a, b): print (x) 1 2 3 4 x y z
The advantage: if you use aiterb to traverse, then you need to have the same type of an and b, and if the data is a little larger, it will consume memory, while chain returns the elements of iterating objects by creating iterators.
How to convert an itertools.chain object into an array list_of_numbers = [[1,2], [3], []] import itertoolschain = itertools.chain (* list_of_numbers)
The first is relatively simple, using the list method directly, as follows:
List (chain)
But there are two disadvantages:
Will nest an extra list on the outer layer
The efficiency is not high.
The second is to use the method np.fromiter of the numpy library. The example is as follows:
> import numpy as np > from itertools import chain > list_of_numbers = [[1,2], [3], []] > np.fromiter (chain (* list_of_numbers), dtype=int) array ([1,2,3])
Compare the operation time of the two methods, as follows:
> list_of_numbers = [[1,2] * 1000, [3] * 1000, []] * 1000 > np.fromiter (chain (* list_of_numbers), dtype=int) 10 loops, best of 3: 103ms per loop > np.array (list (chain (* list_of_numbers) 1 loops, best of 3: 199ms per loop
You can see that the operation speed is faster using the numpy method.
Thank you for reading this article carefully. I hope the article "how to use the itertools.chain () function in big data's Development" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.