首页 > 解决方案 > Creating lists with groups of characters with python

问题描述

People, one question, how can I create this type of list:

  ['keeab', 'dhabe', 'afebd', 'cabab', 'fkabi', 'kifca', 'bfiai', 'hhjbk', 'jkjij', 'echgf']

Using a character list similar to this:

 ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']

I was using this code but it only generates a group of characters

 lista = [x for x in "abcdefghijk"]
 for i in range(5):
    n = random.choice(lista)
 #salida: agjef

标签: python-3.x

解决方案


code:

import random
lista = "abcdefghijk"
result = []
for i in range(11):
    temp = ""
    for j in range(5):
        temp += random.choice(lista)
    result.append(temp)
print(result)

result:

['fkbjk', 'dcdjf', 'hfbif', 'cbfjb', 'ihikh', 'jjdgi', 'cfjkg', 'idigj', 'kfbaj', 'jgeaa', 'gkjdk']

推荐阅读