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 to randomly disrupt the sorting of lists in Python

2025-01-17 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.

Share To

Internet Technology

Wechat

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

12
Report