python取随机数相加,Python-从列表中提取随机数.用指定的长度和总和填充新列表...

I am trying to create a function where:

The output list is generated from random numbers from the input list

The output list is a specified length and adds to a specified sum

ex. I specify that I want a list that is 4 in length and adds up to 10. random numbers are pulled from the input list until the criteria is satisfied.

I feel like I am approaching this problem all wrong trying to use recursion. Any help will be greatly appreciated!!!

EDIT: for more context on this problem.... Its going to be a random enemy generator.

The end goal input list will be coming from a column in a CSV called XP. (I plan to use pandas module). But this CSV will have a list of enemy names in the one column, XP in another, Health in another, etc. So the end goal is to be able to specify the total number of enemies and what the sum XP should be between those enemies and have the list generate with the appropriate information. For ex. 5 enemies with a total of 200 XP between them. The result is maybe -> Apprentice Wizard(50 xp), Apprentice Wizard(50 xp), Grung(50), Xvart(25 xp), Xvart(25 xp). The output list will actually need to include all of the row information for the selected items. And it is totally fine to have duplicated in the output as seen in this example. That will actually make more sense in the narrative of the game that this is for.

import random

from random import *

lis = [1,2,3,4,5,6,7,8,9,10]

output = []

def query (total, numReturns, myList, counter):

random_index = randrange(len(myList)-1)

i = myList[random_index]

h = myList[i]

# if the problem hasn't been solved yet...

if len(output) != numReturns and sum(output) != total:

print(output)

# if the length of the list is 0 (if we just started), then go ahead and add h to the output

if len(output) == 0 and sum(output) + h != total:

output.append(h)

query (total, numReturns, myList, counter)

#if the length of the output is greater than 0

if len(output) > 0:

# if the length plus 1 is less than or equal to the number numReturns

if len(output) +1 <= numReturns:

print(output)

#if the sum of list plus h is greater than the total..then h is too big. We need to try another number

if sum(output) + h > total:

# start counter

for i in myList:# try all numbers in myList...

print(output)

print ("counter is ", counter, " and i is", i)

counter += 1

print(counter)

if sum(output) + i == total:

output.append(i)

counter = 0

break

if sum(output) + i != total:

pass

if counter == len(myList):

del(output[-1]) #delete last item in list

print(output)

counter = 0 # reset the counter

else:

pass

#if the sum of list plus h is less than the total

if sum(output) + h < total:

output.append(h) # add h to the list

print(output)

query (total, numReturns, myList, counter)

if len(output) == numReturns and sum(output) == total:

print(output, 'It worked')

else:

print ("it did not work")

query(10, 4, lis, 0)

解决方案

I guess that it would be better to get first all n-size combinations of given array which adds to specified number, and then randomly select one of them. Random selecting and checking if sum is equal to specified value, in pessimistic scenario, can last indefinitely.

from itertools import combinations as comb

from random import randint

x = [1,1,2,4,3,1,5,2,6]

def query(arr, total, size):

combs = [c for c in list(comb(arr, size)) if sum(c)==total]

return combs[randint(0, len(combs))]

#example 4-item array with items from x, which adds to 10

print(query(x, 10, 4))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值