如何使用列表中的每个项目仅循环一次列表以在​​Python中的另一个列表的开头插入?

时间:2017-02-21 05:48:35

标签: python list loops tuples

我是Python的新手,在完成作业任务时遇到了麻烦。我已经定义了一个除了以下区域之外运行良好的函数:我正在处理10个字符串的列表,我需要将此列表中的每个字符串仅插入到10个字符串的相应随机列表的开头我使用sys.argv从终端中删除了一个文件(意味着有10个文件,我的列表中有一个字符串对应每个文件)。我知道我必须使用.insert(0, new_string)方法从我的10个字符串列表中插入新字符串。

这是我的功能的摘录,展示了我迄今为止所做的尝试:

randomlist = (list(map(choice, old_list)))
new_list = ["string1", "string2", "string3", "string4", "string5", "string6", "string 7", "string8", "string9", "string10"]
for new_string in new_list:
    randomlist.insert(0, new_string)

当我运行此代码时,我得到一个列表的输出,其中new_list中的每个元素都插入到randomlist元素之前。当它应该只有11时,这会产生20个元素的列表。

输出的外观如下所示(使用sys.argv将函数应用于10个文件中的每个文件后)rstring对应于randomlist的字符串:< / p>

["string1", "rstring1", "rstring2", "rstring3", "rstring4", "rstring5", "rstring6", "rstring7", "rstring8", "rstring9", "rstring10"]
["string2", "rstring1", "rstring2", "rstring3", "rstring4", "rstring5", "rstring6", "rstring7", "rstring8", "rstring9", "rstring10"]
["string3", "rstring1", "rstring2", "rstring3", "rstring4", "rstring5", "rstring6", "rstring7", "rstring8", "rstring9", "rstring10"]
["string4", "rstring1", "rstring2", "rstring3", "rstring4", "rstring5", "rstring6", "rstring7", "rstring8", "rstring9", "rstring10"]
["string5", "rstring1", "rstring2", "rstring3", "rstring4", "rstring5", "rstring6", "rstring7", "rstring8", "rstring9", "rstring10"]
["string6", "rstring1", "rstring2", "rstring3", "rstring4", "rstring5", "rstring6", "rstring7", "rstring8", "rstring9", "rstring10"]
["string7", "rstring1", "rstring2", "rstring3", "rstring4", "rstring5", "rstring6", "rstring7", "rstring8", "rstring9", "rstring10"]
["string8", "rstring1", "rstring2", "rstring3", "rstring4", "rstring5", "rstring6", "rstring7", "rstring8", "rstring9", "rstring10"]
["string9", "rstring1", "rstring2", "rstring3", "rstring4", "rstring5", "rstring6", "rstring7", "rstring8", "rstring9", "rstring10"]
["string10", "rstring1", "rstring2", "rstring3", "rstring4", "rstring5", "rstring6", "rstring7", "rstring8", "rstring9", "rstring10"]

我也尝试了其他的东西,但这是我可以获得输出的最简单的方法。作为回顾,我需要使用new_list中的每个元素只有一次(按从左到右的顺序)并插入它到我randomlist的开头。我正在使用的每个文件都有randomlist,我通过sys.argv调用它们。我相信它会涉及某种类型的for循环和可能的元组,但我不知道#39;知道如何让他们做我需要他们做的事。此外,我不允许硬编码任何东西。

2 个答案:

答案 0 :(得分:0)

在您的代码中,您将新元素插入到同一个唯一列表randomlist中,这就是为什么它们最终堆积起来的原因。

您可以在item的开头(不影响randomlist本身)添加元素randomlist,如下所示:

[item] + randomlist

要一次创建所有输出,只需将其嵌套在列表解析中:

all_outputs = [[item] + randomlist for item in new_list]
for output in all_outputs:
    print output

或者如果您只需要打印它们,您可以这样做:

for item in new_list:
    print [item] + randomlist

答案 1 :(得分:0)

试试这个,

import random

list1 = ["rstring1", "rstring2", "rstring3", "rstring4", "rstring5", "rstring6", "rstring7", "rstring8", "rstring9", "rstring10"]
list2 = ["rstring1", "rstring2", "rstring3", "rstring4", "rstring5", "rstring6", "rstring7", "rstring8", "rstring9", "rstring10"]
list3 = ["rstring1", "rstring2", "rstring3", "rstring4", "rstring5", "rstring6", "rstring7", "rstring8", "rstring9", "rstring10"]
list4 = ["rstring1", "rstring2", "rstring3", "rstring4", "rstring5", "rstring6", "rstring7", "rstring8", "rstring9", "rstring10"]
list5 = ["rstring1", "rstring2", "rstring3", "rstring4", "rstring5", "rstring6", "rstring7", "rstring8", "rstring9", "rstring10"]
list6 = ["rstring1", "rstring2", "rstring3", "rstring4", "rstring5", "rstring6", "rstring7", "rstring8", "rstring9", "rstring10"]
list7 = ["rstring1", "rstring2", "rstring3", "rstring4", "rstring5", "rstring6", "rstring7", "rstring8", "rstring9", "rstring10"]
list8 = ["rstring1", "rstring2", "rstring3", "rstring4", "rstring5", "rstring6", "rstring7", "rstring8", "rstring9", "rstring10"]
list9 = ["rstring1", "rstring2", "rstring3", "rstring4", "rstring5", "rstring6", "rstring7", "rstring8", "rstring9", "rstring10"]
list10 = ["rstring1", "rstring2", "rstring3", "rstring4", "rstring5", "rstring6", "rstring7", "rstring8", "rstring9", "rstring10"]

lists = [list1, list2, list3, list4, list5, list6, list7, list8, list9, list10]

new_list = ["string1", "string2", "string3", "string4", "string5", "string6", "string7", "string8", "string9", "string10"]
for new_string in new_list:
    randomlist = random.choice(lists) #Selects a random list from pool
    randomlist.insert(0, new_string)
    lists.remove(randomlist) #Removes list from pool once it has been selected

print list1
print list2
print list3
print list4
print list5
print list6
print list7
print list8
print list9
print list10