Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #27886
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Subject | Re: Probability Algorithm |
| Date | 2012-08-25 16:47 -0400 |
| Organization | > Bestiaria Support Staff < |
| References | <CALZWPGh9ng4NoTRB+h0gLpNhEzJoyg+JGXOUjy=TPgqznh7HzQ@mail.gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3814.1345927660.4697.python-list@python.org> (permalink) |
On Sun, 26 Aug 2012 00:03:07 +0800, ??? <yueyoum@gmail.com> declaimed
the following in gmane.comp.python.general:
> Hi, All,
>
> I have a problem of probability algorithm
>
Smells like homework -- but since you did provide something that
could be turned in as your work, I'm going to go out on a limb
>
> The goal is obtain a list which contains three items. as the *FinalList*
>
> There has Four source lists. *
> ALIST, BLIST, CLIST, DLIST
>
> There are all Unknown length. They contains unique elements*
> ( In fact, there are all empty at the program beginning, when running,
> there growing )
>
theLists = { "A" : [],
"B" : [],
"C" : [],
"D" : [] }
# do whatever is needed to populate the lists
# I presume reading some file(s) and doing
...
theLists[listName].append(listValue)
> Choose items form this source lists. pick up random items to generate the
> FinalList
> Ensure The Following Requirements
>
> In the FinalList,
> probability of ALIST's item appeared is 43%
> probability of BLIST's item appeared is 37%
> probability of CLIST's item appeared is 19%
> probability of DLIST's item appeared is 1%
>
>
<snip>
theProbabilities = { "A" : 43,
"B" : 37+43,
"C" : 19+37+43,
"D" : 1+19+37+43 }
> while a_picked_times < 43:
Why keep a counter? Rather than an iterated loop
for i in range(43):
> item = choice(ALIST)
> ALIST.remove(item)
Do you really want to remove an item from the source list?
Technically, a requirement that an item appears from ALIST 43% of the
time does NOT prohibit it being the SAME ITEM. {This also answers you
problem about only working if the lists are long... as long as the list
contains at least ONE item, you can pick that item to meet the
probability}
>
> if item in already_picked_list:
> continue
>
This is meaningless with regards to the previous comments: if you
had removed the item from the source list, it will never appear again
(if it did, it was from a different position in the source list, or from
a different source list overall -- and if you don't want duplicates from
within a source list, you should remove them when building the source
list)
> slot.append(item)
> a_picked_times += 1
>
<snip>
Given that "theLists" contains the four lists:
result = []
while len(result) < 3:
#using this test handles the case of sublist being empty
x = random.randint(0, 99)
if x < theProbabilities["A"]:
clist = theLists["A"]
elif x < theProbabilities["B"]:
clist = theLists["B"]
elif x < theProbabilities["C"]:
clist = theLists["C"]
else:
clist = theLists["D"]
if len(clist):
#is sublist is empty, nothing gets appended, loop goes on
result.append(random.choice(clist))
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Probability Algorithm Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-08-25 16:47 -0400
csiph-web