Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How Python uses multithreading to rearrange arrays

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article Xiaobian for you to introduce in detail "Python how to use multithreading method to rearrange the array", the content is detailed, the steps are clear, the details are handled properly, I hope this "Python how to use multithreading method to rearrange the array" article can help you solve doubts, following the editor's ideas slowly in-depth, together to learn new knowledge.

Suppose you have an array of characters, just like 'XOOOXOX', I need to rearrange this array' XXXOOOO'. So how do you create simple python code and rearrange arrays using a multithreaded approach?

I just created a virtual thread to work, which may not show a performance improvement at all:

Import randomimport threadinga = random.choices (['Xboxes,' O'], kryp10000) a = '.join (a) print (a.count (' X'), a.count ('O')) thread_count = 4lock = threading.Lock () x_count = 0o_count = 0def chunk_it (seq Num): avg = len (seq) / float (num) out = [] last = 0.0 while last < len (seq): out.append (seq [last (last): int (last + avg)]) last + = avg return outdef d (I): global x_count global o_count data_part = chunk_it (a Thread_count) [I] with lock: x_count + = data_part.count ('X') o_count + = data_part.count ('O') ts = [] for i in range (thread_count): t = threading.Thread (target=d, args= (I) ) ts.append (t) for t in ts: t.start () for t in ts: t.join () a ='X' * x_count +'O' * o_countprint (a) print (a.count ('X'), a.count ('O'))

This demo is designed to use the api display of threads with relatively low Python. The concurrency of snakes is led by several modules: threading, multiprocessing, concurrent.futures, asyncio, and so on.

Read this, the "Python how to use multi-threading method to rearrange the array" article has been introduced, want to master the knowledge of this article also need to practice and use in order to understand, if you want to know more related articles, welcome to pay attention to the industry information channel.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report