如何缩短代码以创建多个列表?

时间:2018-12-03 15:53:34

标签: python list

我要制作12个经过相同过程的列表。不幸的是,我无法弄清楚如何使它们全部循环执行,而且我被迫重复12次相同的代码并占用大量文本。

这只是我必须编写的代码的一小部分。

  l1 = []
  l1.append(random.choice(easy))
  if "none" in l1:
    l1.remove("none")
  else:
    psblnsrs.append(l1[0])
    easy.remove(l1[0])
  l1.append(random.choice(special))
  if "none" in l1:
    l1.remove("none")
  elif len(l1) >1:
    usblhks.append(l1[1])
  else:
    usblhks.append(l1[0])
  while sum(len(l1) for l1 in l1) < 12:
    l1.append(random.choice(junk))
  random.shuffle(l1)
  l2 = []
  l2.append(random.choice(easy))
  if "none" in l2:
    l2.remove("none")
  else:
    psblnsrs.append(l2[0])
    easy.remove(l2[0])
  l2.append(random.choice(special))
  if "none" in l2:
    l2.remove("none")
  elif len(l2) >1:
    usblhks.append(l2[1])
  else:
    usblhks.append(l2[0])
  while sum(len(l2) for l2 in l2) < 12:
    l2.append(random.choice(junk))
  random.shuffle(l2)

请记住,需要创建十二个列表,这只是两个

我不太熟悉编码大型循环并正确命名变量。我想要这样的东西:

for i in range(12):
  l(i) = []
  l(i).append ...

有没有办法使这项工作或类似的方式使这项工作?

此外,如果代码难以理解,则源材料为here

4 个答案:

答案 0 :(得分:1)

功能可能派上用场

def make_list(inp_list1=psblnsrs, inp_list2=usblhks, easy_list=easy, special_list=special, junk_list=junk):
    l1 = []
    l1.append(random.choice(easy_list))
    if "none" in l1:
      l1.remove("none")
    else:
      psblnsrs.append(l1[0])
      easy.remove(l1[0])
    l1.append(random.choice(special_list))
    if "none" in l1:
      l1.remove("none")
    elif len(l1) >1:
      usblhks.append(l1[1])
    else:
      usblhks.append(l1[0])
    while sum(len(l1) for l1 in l1) < 12:
      l1.append(random.choice(junk_list))
    return random.shuffule(l1)

l = []
for i in range(12):
  l.append(make_list())

答案 1 :(得分:0)

考虑以下代码

#Create a list to contain lists
myLists = []

#Add to our list 12 empty lists
for i in range(12):
    myLists.append([])

#Loop over each list and add 12 to each list.
for currentList in myLists:
    currentList.append(12)

#Print out the results
print(myLists)

通过使用for currentList in myLists,我们可以遍历“列表列表”中的每个列表,并在移至下一个列表之前对currentList执行操作。因此,您的代码将依次对每个列表执行相同的操作。

答案 2 :(得分:0)

正如khelwood指出的那样,十二个列表的更好数据结构是将它们存储在另一个列表中。这样,您可以使用super_list[0], super_list[1]访问它们,更重要的是,您可以使用for sublist in super_list: ...遍历它们,而不必完全显式地引用它们。

这样,您只需编写一次逻辑即可。您要做的就是定义一个函数:

def list_update(current_list):
   '''
   List management logic code here.
   '''
   ...
   return current_list

然后在代码的主要逻辑中:

for sub_list in super_list:
    sub_list = list_update(sub_list)

这样,当您必须对处理这些列表的方式进行更改时,您不必将更改写入12次,而只需编写一次。

答案 3 :(得分:0)

我认为您可能可以像您正在做的那样做。

    lists = []
    for list in lists:
        list = []
        list.append(random.choice(easy))
        if "none" in list:
            list.remove("none")
        else:
            psblnsrs.append(list[0])
            easy.remove(list[0])
        list.append(random.choice(special))
        if "none" in list:
            list.remove("none")
        elif len(list) >1:
            usblhks.append(list[1])
        else:
            usblhks.append(list[0])
        while sum(len(list) for list in list) < 12:
            list.append(random.choice(junk))
        random.shuffle(list)

除非我缺少有关代码在上游的工作方式的信息,否则您应该能够做到这一点,最终将得到一个列表列表,您可以通过它们的索引进行引用。 (例如,lists(2))