How to exclude some numbers from the list

import networkx as nx import numpy as np import random from networkx.utils import powerlaw_sequence W=powerlaw_sequence(100,exponent=2.5) random.choice(W) 

What if I want the numbers in this sequence to be numbers other than zero? or any number in a certain range? so the smallest value is 1, for example .. Or even assign this condition when choosing a random number from a sequence.

Powerlaw_sequence (100, exponent = 2.0, range (1,20)) and powerlaw_sequence (100, exponent = 2.0, xmin = 1) do not work. Thanks you

+2
source share
2 answers

I don't know numpy, so maybe another possible solution, but the following should work:

 W = [x for x in powerlaw_sequence(100,exponent=2.5) if x != 0] 

However, this reduces the length W by the number of elements that are filtered out due to the condition x != 0 .

+3
source

Perhaps you are looking for a zipf sequence?

 In [1]: import networkx as nx In [2]: nx.utils.zipf_sequence(10,alpha=2.5) Out[2]: [1, 1, 1, 2, 1, 2, 1, 1, 1, 8] 

http://networkx.lanl.gov/reference/generated/networkx.utils.random_sequence.zipf_sequence.html#networkx.utils.random_sequence.zipf_sequence

+1
source

Source: https://habr.com/ru/post/906550/


All Articles