how to exclude some numbers from a list

2019-07-24 10:25发布

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 of this sequence to be any number except zero? or any number in a specific range? so that the smallest value is 1, e.g.. Or even to assign this condition when choosing the random number from the sequence.

Neither powerlaw_sequence(100,exponent=2.0,range(1,20)) nor powerlaw_sequence(100,exponent=2.0,xmin=1) work. Thank you

2条回答
仙女界的扛把子
2楼-- · 2019-07-24 10:46

Maybe 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

查看更多
何必那么认真
3楼-- · 2019-07-24 10:48

I don't know numpy, so perhaps there is 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 of W by the number of elements that are filtered out because of the condition x != 0.

查看更多
登录 后发表回答