使用 random.sample,我如何让它打印彼此相邻的字母
With random.sample, How do i make it print the letters next to eachother
> *When using random.sample to make a password, It comes out something like this ``` ['1', '2', '3'] ``` How do I make it output something
> like this ``` 123 ``` So that I can use it to generate strings of
> passwords that includes letters numbers and symbols. Edit: I should
> probably specify that the output isnt just numbers, Its
> letters+symbols aswell*
好的,所以现在已经解决了,但是现在我有一个新问题:
它提出了这个
File "/usr/lib/python3.8/random.py", line 363, in sample
raise ValueError("Sample larger than population or is negative")
ValueError: Sample larger than population or is negative
>>> A=['1','2','3']
>>> "".join(A)
'123'
join
是将列表的(字符串)元素组合成一个长字符串的方法,每个元素由用于调用 join
.
的字符串分隔
由于 random.sample 正在生成一个字符串格式的数字列表,您可以使用一个简单的 join() 函数来实现这个技巧
divider = ""
YourList = divider.join(YourList)
更新:
要使用随机生成密码,您可以使用
import string
import random
bag = string.ascii_lowercase + string.ascii_uppercase + string.digits + string.punctuation
password_length= 10
password = ''.join([random.choice(bag) for letter_count in range(password_length)])
只将那些 elements/characters 放在您希望密码包含的包中
> *When using random.sample to make a password, It comes out something like this ``` ['1', '2', '3'] ``` How do I make it output something
> like this ``` 123 ``` So that I can use it to generate strings of
> passwords that includes letters numbers and symbols. Edit: I should
> probably specify that the output isnt just numbers, Its
> letters+symbols aswell*
好的,所以现在已经解决了,但是现在我有一个新问题: 它提出了这个
File "/usr/lib/python3.8/random.py", line 363, in sample
raise ValueError("Sample larger than population or is negative")
ValueError: Sample larger than population or is negative
>>> A=['1','2','3']
>>> "".join(A)
'123'
join
是将列表的(字符串)元素组合成一个长字符串的方法,每个元素由用于调用 join
.
由于 random.sample 正在生成一个字符串格式的数字列表,您可以使用一个简单的 join() 函数来实现这个技巧
divider = ""
YourList = divider.join(YourList)
更新:
要使用随机生成密码,您可以使用
import string
import random
bag = string.ascii_lowercase + string.ascii_uppercase + string.digits + string.punctuation
password_length= 10
password = ''.join([random.choice(bag) for letter_count in range(password_length)])
只将那些 elements/characters 放在您希望密码包含的包中