In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Python in how to randomly disrupt the list sorting, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.
Scene:
Now there is a list: [1, 2, 3, 3, 4, 5, 6], I need to output this list in a random scrambled form.
Professional point terminology: traverse the data in a container one by one at a time.
Note: do not generate a random list set.
Environment:
Python 3.6
Solution: solution 1:
Someone may use Random built-in functions to indirectly achieve the desired results. But this way is too primitive, not elegant enough, and there is a suspicion of repeating the wheel. Here I will not post the effects I have achieved through random.
Option 2:
There is a random.shuffle () method in Random that provides the perfect solution. The code is as follows:
X = [1, random.shuffle, 2, 3, 4, 5, 6] print (x)
Output result:
First output: [6, 5, 1, 3, 2, 4] second output: [6, 1, 3, 5, 2, 4] third output: [5, 3, 1, 2, 4, 6]
As we can see from the results, the output is completely random, with only two lines of code, no random, no for loop.
Source code interpretation:
This part of the original link: how to use random.shuffle () to disrupt the order of lists in Python [1]
Def shuffle (self, x, random=None): "Shuffle list x in place, and return None. Scramble the list in place and do not generate a new list.
Optional argument random is a 0-argument function returning a random float in [0.0,1.0); if it is the default None, the standard random.random will be used. The optional parameter random is a function from 0 to argument that returns a random floating point in [0.0recoery 1.0); if random is the default value None, the standard random.random () is used. "
If random is None: randbelow = self._randbelow for i in reversed (range (1, len (x): # pick an element in x [: iTh1] with which to exchange x [I] j = randbelow (iTh1) x [I], x [j] = x [j], x [I] else: _ int = int for i in reversed (range (1) Len (x)): # pick an element in x [: iTun1] with which to exchange x [I] j = _ int (random () * (iTun1)) x [I], x [j] = x [j], x [I]
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
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.