I get TypeError: choice() got an unexpected keyword argument 'k' , when using python random.choice
I get TypeError: choice() got an unexpected keyword argument 'k' , when using python random.choice
我想在给定的字母表上生成长度为 n 的随机字符串。
import random
alphabet = "ACTG"
n= 10
# print(''.join(random.choice(alphabet) for x in range(n)) ) # work fine
print(''.join(random.choice(alphabet, k=n))) # doesn't work
错误:
Traceback (most recent call last):
File "<input>", line 3, in <module>
TypeError: choice() got an unexpected keyword argument 'k'
正确的方法是choices和s,所以使用random.choices
.
错误来自具有相似名称的两个函数。第一个是 random.choices
with s,第二个是 random.choice
没有 s.
import random
alphabet = "ACTG"
n= 10
# print(''.join(random.choice(alphabet, k=n))) # gives an error
print(''.join(random.choices(alphabet, k=n))) # the correct method, work fine
我想在给定的字母表上生成长度为 n 的随机字符串。
import random
alphabet = "ACTG"
n= 10
# print(''.join(random.choice(alphabet) for x in range(n)) ) # work fine
print(''.join(random.choice(alphabet, k=n))) # doesn't work
错误:
Traceback (most recent call last):
File "<input>", line 3, in <module>
TypeError: choice() got an unexpected keyword argument 'k'
正确的方法是choices和s,所以使用random.choices
.
错误来自具有相似名称的两个函数。第一个是 random.choices
with s,第二个是 random.choice
没有 s.
import random
alphabet = "ACTG"
n= 10
# print(''.join(random.choice(alphabet, k=n))) # gives an error
print(''.join(random.choices(alphabet, k=n))) # the correct method, work fine