我无法从random.shuffle方法中捕获索引

时间:2021-07-14 02:09:22

I cannot catch index from random.shuffle method. An error happens

我无法从random.shuffle方法中捕获索引。发生错误

TypeError: %d format: a number is required, not list

My code is

我的代码是

if __name__ =="__main__":
    bell_nums = range(1,6)
    pairs = list(itertools.combinations(bell_nums,2))

    for pair in pairs:
        bell_num1=int(pair[0])
        bell_num2 = int(pair[1])
        train_data = np.empty((0,12),float)
        train_label = np.array([])
        test_data = np.empty((0,12),float)
        test_label = np.array([])
        noise_nums = list(range(1,12))
        level_nums = list(range(0,10))
        random.shuffle(noise_nums)

        nfft=2048 
        nceps = 12 

        for noise_nums_index in noise_nums[0:10]:
            random.shuffle(level_nums)
            files_name = glob.glob("learning_sample/%d_%d_%d.wav" % (bell_num1,noise_nums_index,level_nums))
            for file_name in files_name:
                feature = get_feature(files_name,nfft,nceps)
                if len(train_data) ==0:
                    train_data=feature
                else:
                    train_data=np.vstack((train_data,feature))
                train_label=np.append(train_label,bell_num1)
            files_name="learning_sample/%d_%d_%d.wav"% (bell_num1,noise_num,level_nums[8])
            feature = get_feature(file_name,nfft,nceps)
            if len(test_data) ==0:
                test_data=feature
            else:
                test_data=np.vstack((test_data,feature))
            test_label=np.append(test_label,bell_num1)

I think level_nums is list type,so this error happen. But I cannot come up with the way to catch index from random.shuffle method in this case. I wanna name "learning_sample/%d_%d_%d.wav" this file by using noise_nums_index's number and level_nums's number randomly.In this case, how can I do to name this part?How can I write this?Should I use for statement for random.shuffle(level_nums)?

我认为level_nums是列表类型,所以这个错误发生了。但在这种情况下,我无法想出从random.shuffle方法中捕获索引的方法。我想通过随机使用noise_nums_index的数字和level_nums的数字命名“learning_sample /%d_%d_%d.wav”这个文件。在这种情况下,我该如何命名这部分?我怎么写这个?我应该用于语句吗? for random.shuffle(level_nums)?

3 个解决方案

#1


1  

To select a random element in level_nums you should use: random.choice(level_nums)

要在level_nums中选择随机元素,您应该使用:random.choice(level_nums)

In your code:

在你的代码中:

for noise_nums_index in noise_nums[0:10]:
    files_name = glob.glob("learning_sample/%d_%d_%d.wav" % (bell_num1,noise_nums_index,random.choice(level_nums)))

Note that since noise_nums is not define in the code you provide I was no able to check the full code. There might be some other errors.

请注意,由于在您提供的代码中未定义noise_nums,因此我无法检查完整代码。可能还有其他一些错误。

#2


0  

you can use random.randrange method from standard python library.

你可以使用标准python库中的random.randrange方法。

for example:

random.randrange(1, 12, 1)

or random.choice method:

或者random.choice方法:

a = list(range(1,12))
random.choice(a)

#3


0  

level_nums is a list and you are pointing it at a %d format string.

level_nums是一个列表,您将其指向%d格式字符串。

If you are just looking for a random int, and because you are already using numpy have you considered using np.random.choice()? That would remove the need to even use the shuffle method.

如果你只是在寻找一个随机的int,并且因为你已经在使用numpy,你考虑过使用np.random.choice()吗?这将消除甚至使用shuffle方法的需要。

>>> np.random.choice(level_nums)
3
>>> np.random.choice(level_nums)
8

Or just the random int function and get rid of level_nums completely

或者只是随机int函数并完全摆脱level_nums

>>> np.random.randint(1, 11)
6

#1


1  

To select a random element in level_nums you should use: random.choice(level_nums)

要在level_nums中选择随机元素,您应该使用:random.choice(level_nums)

In your code:

在你的代码中:

for noise_nums_index in noise_nums[0:10]:
    files_name = glob.glob("learning_sample/%d_%d_%d.wav" % (bell_num1,noise_nums_index,random.choice(level_nums)))

Note that since noise_nums is not define in the code you provide I was no able to check the full code. There might be some other errors.

请注意,由于在您提供的代码中未定义noise_nums,因此我无法检查完整代码。可能还有其他一些错误。

#2


0  

you can use random.randrange method from standard python library.

你可以使用标准python库中的random.randrange方法。

for example:

random.randrange(1, 12, 1)

or random.choice method:

或者random.choice方法:

a = list(range(1,12))
random.choice(a)

#3


0  

level_nums is a list and you are pointing it at a %d format string.

level_nums是一个列表,您将其指向%d格式字符串。

If you are just looking for a random int, and because you are already using numpy have you considered using np.random.choice()? That would remove the need to even use the shuffle method.

如果你只是在寻找一个随机的int,并且因为你已经在使用numpy,你考虑过使用np.random.choice()吗?这将消除甚至使用shuffle方法的需要。

>>> np.random.choice(level_nums)
3
>>> np.random.choice(level_nums)
8

Or just the random int function and get rid of level_nums completely

或者只是随机int函数并完全摆脱level_nums

>>> np.random.randint(1, 11)
6

相关文章