Python 如何缩短代码以生成多个列表?
我正在制作12份清单,都经过同样的过程。不幸的是,我不知道如何让它们都在一个循环中完成这项工作,我被迫重复同样的代码12次,并占用了大量的文本 下面是我必须编写的代码的一小部分Python 如何缩短代码以生成多个列表?,python,list,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.
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)
有没有一种方法可以使这项工作或类似的方式使这项工作
此外,如果代码很难理解,则源材料是。函数可能会派上用场
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())
def制作列表(inp列表1=psblnsrs,inp列表2=usblhks,易列表=易,特殊列表=特殊,垃圾列表=垃圾):
l1=[]
l1.追加(随机选择(简单列表))
如果l1中为“无”:
l1.删除(“无”)
其他:
psblnsrs.append(l1[0])
简单。删除(l1[0])
l1.追加(随机选择(特殊列表))
如果l1中为“无”:
l1.删除(“无”)
elif len(l1)>1:
usblhks.append(l1[1])
其他:
usblhks.append(l1[0])
当总和(l1中l1的len(l1))小于12时:
l1.追加(随机选择(垃圾列表))
返回random.shuffule(l1)
l=[]
对于范围(12)内的i:
l、 追加(生成列表())
考虑以下代码
#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)
通过在MyList中为currentList使用
,
,我们可以在“列表列表”中的每个列表上迭代,并在转到下一个列表之前对currentList执行操作。因此,您的代码会依次对每个列表执行相同的操作。正如khelwood指出的那样,十二个列表的更好数据结构是将它们存储在另一个列表中。这样,您就可以使用super\u list[0]、super\u list[1]
访问它们,更重要的是,您可以使用super\u 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次,只需写一次 我想你可能会像你的朋友那样做
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)
list=[]
对于列表中的列表:
列表=[]
list.append(随机选择(简单))
如果列表中的“无”:
列表。删除(“无”)
其他:
psblnsrs.append(列表[0])
简单。删除(列表[0])
list.append(随机选择(特殊))
如果列表中的“无”:
列表。删除(“无”)
elif len(列表)>1:
usblhks.append(列表[1])
其他:
usblhks.append(列表[0])
当总和(列表中列表的len(列表))小于12时:
list.append(随机选择(垃圾))
随机。随机(列表)
除非我遗漏了一些关于代码如何在上游工作的内容,否则您应该能够做到这一点,您将得到一个列表列表,您可以通过它们的索引来引用这些列表。(例如,列表(2))您可能不应该有12个变量名为
l1、l2、…、l12
。那是在要求不必要的麻烦。您可以将它们全部替换为一个包含12个元素的列表。我会考虑将前17行左右的内容合并到一个函数中,然后您可以创建一个变量列表,如选择=[l1,l2,l3,l4,l5,l6,…l12]
,然后您可以在选项中调用lst的函数:
@khelwood我让所有12个列表在自己的行上打印它们的内容。这就需要我列出一个庞大的清单,并将其分成几个部分12ths@G.Anderson这可能行得通,你能举个例子吗?@BeardedPancake不需要这样。您可以有一个包含12个元素的列表,其中每个元素都有自己的列表。它不涉及将列表拆分为十二个。列表本身不需要创建输入,两个变量psblnsrs
和usblhks
表示“可能的答案”和“可用的黑客”,它们是列表中的项目。我希望代码用于生成列表l1
,并使用一个循环使用与您相同的流程生成12个列表,但我如何将我为当前列表编写的代码合并到一起?@BeardedPancake我恐怕不理解您的代码的目的,因此我无法提供完整的示例。如果你能解释一下12张清单需要包含什么,我也许能帮上忙。如果您提供了一个完整的可执行示例,将会有所帮助。@BeardPancake一般来说,您应该能够替换l1
的实例,l2
etc与currentList
。在我编写的程序中,我应该用什么来替换l1
,以获得我需要制作的12个不同列表?用currentList
替换l1
。一旦进入for循环,currentList将在每次迭代中成为下一个列表hmm…这似乎有效,但我如何在列表中调用列表以进行打印?是lists[][]
还是lists[[]]
lists[]]是访问列表中项目的正确语法。
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)