Shuffling a list of objects
random.shuffle
should work. Here's an example, where the objects are lists:
from random import shuffle
x = [[i] for i in range(10)]
shuffle(x)
print(x)
# print(x) gives [[9], [2], [7], [0], [4], [5], [3], [1], [8], [6]]
Note that shuffle
works in place, and returns None
.
More generally in Python, mutable objects can be passed into functions, and when a function mutates those objects, the standard is to return None
(rather than, say, the mutated object).
Shuffling a list inside an object in Python
random.shuffle
performs the shuffle operation on the list argument - it doesn't return a new list. You need to generate a new list first of all, and then use shuffle
to shuffle it:
new_list = self.number_of_packs * self.cards
random.shuffle(new_list)
print(new_list)
Randomizing a list in Python
from random import shuffle
list1 = [1,2,3,4,5]
shuffle(list1)
print list1
---> [3, 1, 2, 4, 5]
Shuffle the list of Objects java stream
You can do in-place shuffling. For that, you don't need to create extra Word
objects.
private void shuffle(List<Word> words) {
List<String> meanings = words.stream()
.map(Word::getMeaning)
.collect(Collectors.toList());
Collections.shuffle(meanings);
for(int i = 0; i < words.size(); i++) {
words.get(i).setMeaning(meanings.get(i));
}
}
Shuffle ArrayList of objects
Hi Sumit i dont know why there are few downvote it is because you have not informed that you are new to Java Language
Please find my answer below
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
final class Product {
private final String name;
private final Double price;
private final Double discount;
Product(String name, Double price, Double discount) {
this.name = name;
this.price = price;
this.discount = discount;
}
@Override
public String toString() {
return "{name=" + name + ", price=" + price + ", discount=" + discount + "}";
}
}
public final class Main {
public static void main(String[] args) {
List<Product> products = new ArrayList<>();
products.add(new Product("Watch", 45.99, 9.99));
products.add(new Product("Keyboard", 21.99, 3.99));
products.add(new Product("Mouse", 99.99, 4.99));
System.out.println("Original items Before shuffle are");
products.forEach(System.out::println);
Collections.shuffle(products);
System.out.println("Original items After shuffle are");
products.forEach(System.out::println);
}
}
How to lightly shuffle a list in python
to show what some of these solutions are doing I find it helps to run a monte-carlo algorithm many times and look at the distribution
first a tidied up version of @meta4's solution as it was the most fleshed out:
from random import randrange
def partial_shuffle(l, factor=5):
n = len(l)
for _ in range(factor):
a, b = randrange(n), randrange(n)
l[b], l[a] = l[a], l[b]
which we can run many times by doing:
import numpy as np
n = 8
orig = list(range(n))
occur = np.zeros((n, n), int)
for _ in range(100000):
x = orig[:]
partial_shuffle(x,1)
occur[orig,x] += 1
if we print out the occurrences table as percentages we get:
[[33.5 9.6 9.5 9.4 9.4 9.6 9.5 9.5]
[ 9.6 33.2 9.7 9.5 9.6 9.6 9.4 9.4]
[ 9.5 9.6 33.2 9.5 9.6 9.5 9.6 9.5]
[ 9.5 9.3 9.6 33.4 9.5 9.5 9.5 9.6]
[ 9.4 9.6 9.4 9.6 33.3 9.5 9.7 9.5]
[ 9.6 9.5 9.6 9.6 9.4 33.3 9.5 9.6]
[ 9.4 9.7 9.5 9.5 9.5 9.6 33.2 9.7]
[ 9.5 9.5 9.6 9.5 9.7 9.5 9.6 33.2]]
each row represents the probability of the item moving to the column. in this case (when n=8
) the algorithm will tend to leave elements where they were ~33% of the time, and then pick the remainder uniformly
I can then run (a tidied up) version of pjs's code with:
from random import gauss
orderliness = 2
occur = np.zeros((n, n), int)
for _ in range(100000):
x = sorted(orig, key=lambda i: gauss(i * orderliness, 1))
occur[orig,x] += 1
which gives very different output:
[[91.9 7.9 0.1 0. 0. 0. 0. 0. ]
[ 7.9 84.1 7.8 0.1 0. 0. 0. 0. ]
[ 0.1 7.8 84.1 7.9 0.1 0. 0. 0. ]
[ 0. 0.1 7.9 84.1 7.7 0.1 0. 0. ]
[ 0. 0. 0.1 7.7 84.2 7.8 0.1 0. ]
[ 0. 0. 0. 0.1 7.9 84.2 7.7 0.1]
[ 0. 0. 0. 0. 0.1 7.7 84.2 7.9]
[ 0. 0. 0. 0. 0. 0.1 7.9 91.9]]
i.e. items tend to remain close to where they started
this sort of table is great at detecting bias in the distribution, which there doesn't seem to be evidence of above. but, for example, with Artyom's solution (shuffle(x, lambda: random() / 5)
) gives the following:
[[ 0. 37.4 0. 0. 0. 16.7 23.8 22.1]
[ 0. 0. 100. 0. 0. 0. 0. 0. ]
[ 0. 0. 0. 100. 0. 0. 0. 0. ]
[ 0. 0. 0. 0. 100. 0. 0. 0. ]
[ 1.7 0. 0. 0. 0. 83.3 11.9 3. ]
[ 9. 7.4 0. 0. 0. 0. 64.2 19.4]
[ 26.7 17.9 0. 0. 0. 0. 0. 55.5]
[ 62.6 37.4 0. 0. 0. 0. 0. 0. ]]
which probably isn't what the OP wanted. the high probability off diagonal represents rotating the array by one element
Randomize a List<T>
Shuffle any (I)List
with an extension method based on the Fisher-Yates shuffle:
private static Random rng = new Random();
public static void Shuffle<T>(this IList<T> list)
{
int n = list.Count;
while (n > 1) {
n--;
int k = rng.Next(n + 1);
T value = list[k];
list[k] = list[n];
list[n] = value;
}
}
Usage:
List<Product> products = GetProducts();
products.Shuffle();
The code above uses the much criticised System.Random method to select swap candidates. It's fast but not as random as it should be. If you need a better quality of randomness in your shuffles use the random number generator in System.Security.Cryptography like so:
using System.Security.Cryptography;
...
public static void Shuffle<T>(this IList<T> list)
{
RNGCryptoServiceProvider provider = new RNGCryptoServiceProvider();
int n = list.Count;
while (n > 1)
{
byte[] box = new byte[1];
do provider.GetBytes(box);
while (!(box[0] < n * (Byte.MaxValue / n)));
int k = (box[0] % n);
n--;
T value = list[k];
list[k] = list[n];
list[n] = value;
}
}
A simple comparison is available at this blog (WayBack Machine).
Edit: Since writing this answer a couple years back, many people have commented or written to me, to point out the big silly flaw in my comparison. They are of course right. There's nothing wrong with System.Random if it's used in the way it was intended. In my first example above, I instantiate the rng variable inside of the Shuffle method, which is asking for trouble if the method is going to be called repeatedly. Below is a fixed, full example based on a really useful comment received today from @weston here on SO.
Program.cs:
using System;
using System.Collections.Generic;
using System.Threading;
namespace SimpleLottery
{
class Program
{
private static void Main(string[] args)
{
var numbers = new List<int>(Enumerable.Range(1, 75));
numbers.Shuffle();
Console.WriteLine("The winning numbers are: {0}", string.Join(", ", numbers.GetRange(0, 5)));
}
}
public static class ThreadSafeRandom
{
[ThreadStatic] private static Random Local;
public static Random ThisThreadsRandom
{
get { return Local ?? (Local = new Random(unchecked(Environment.TickCount * 31 + Thread.CurrentThread.ManagedThreadId))); }
}
}
static class MyExtensions
{
public static void Shuffle<T>(this IList<T> list)
{
int n = list.Count;
while (n > 1)
{
n--;
int k = ThreadSafeRandom.ThisThreadsRandom.Next(n + 1);
T value = list[k];
list[k] = list[n];
list[n] = value;
}
}
}
}
Shuffling the list inside JSON Array
public static JSONArray shuffleJsonArray (JSONArray array) throws JSONException {
// Implementing Fisher–Yates shuffle
Random rnd = new Random();
for (int i = array.length() - 1; i >= 0; i--)
{
int j = rnd.nextInt(i + 1);
// Simple swap
Object object = array.get(j);
array.put(j, array.get(i));
array.put(i, object);
}
return array;
}
Related Topics
How to Modify List Entries During For Loop
Wait Until Page Is Loaded With Selenium Webdriver For Python
Remove All the Elements That Occur in One List from Another
Word Boundary With Words Starting or Ending With Special Characters Gives Unexpected Results
Read Subprocess Stdout Line by Line
How to Run a Python Script as a Service in Windows
How to Replace Nans by Preceding or Next Values in Pandas Dataframe
How to Search For a String in Text Files
How to Read a Text File into a String Variable and Strip Newlines
Pip Install Failing With: Oserror: [Errno 13] Permission Denied on Directory
How to Search and Replace Text in a File
String Count With Overlapping Occurrences
How to Iterate Over Files in a Given Directory
How to Hide Output of Subprocess
Apply Multiple Functions to Multiple Groupby Columns
Taking Subarrays from Numpy Array With Given Stride/Stepsize