Python 为循环列出/定义和嵌套

Python 为循环列出/定义和嵌套,python,while-loop,nested-loops,nested-lists,deque,Python,While Loop,Nested Loops,Nested Lists,Deque,我是Python新手,现在遇到了一些障碍。我使用的是python 3,代码如下: from collections import deque databases=input("Enter databases: ") db_list = deque(databases.split()) node1list = [] node2list = [] node3list = [] numDatabases = len(db_list) while len(db_list) > 0:

我是Python新手,现在遇到了一些障碍。我使用的是python 3,代码如下:

from collections import deque

databases=input("Enter databases: ")

db_list = deque(databases.split())
node1list = []
node2list = []
node3list = []

numDatabases = len(db_list)

while len(db_list) > 0:
    if len(db_list) > 0:
        node1list.append(db_list.popleft())
    if len(db_list) > 0:
        node2list.append(db_list.popleft())
    if len(db_list) > 0:
        node3list.append(db_list.popleft())

print("Paste the following in Node 1's file")
print("--------------------------------------------")
print("[[INSTANCE]")
print("#  Keep a blank space after the colon character.")
print("#")
print("group1:", end="")
for db in node1list:
    print(" " + db, end="")

print("\n\nPaste the following in Node 2's file")
print("--------------------------------------------")
print("[[INSTANCE]")
print("#  Keep a blank space after the colon character.")
print("#")
print("group1: ", end="")
for db in node2list:
    print(" " + db, end="")

print("\n\nPaste the following in Node 3's file")
print("--------------------------------------------")
print("[[INSTANCE]")
print("#  Keep a blank space after the colon character.")
print("#")
print("group1: ", end="")
for db in node3list:
    print(" " + db, end="")
当我运行代码时,我得到如下输出:

Enter databases: one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen twenty twentyone twentytwo twentythree twentyfour twentyfive twentysix twentyseven twentyeight twentynine thirty thirtyone thirtytwo thirtythree thirtyfour
----------------------------------------------------------------------------------------------

Paste the following in Node 1's file
--------------------------------------------
[[INSTANCE]
#  Keep a blank space after the colon character.
#
group1: one four seven ten thirteen sixteen nineteen twentytwo twentyfive twentyeight thirtyone thirtyfour

Paste the following in Node 2's file
--------------------------------------------
[[INSTANCE]
#  Keep a blank space after the colon character.
#
group1:  two five eight eleven fourteen seventeen twenty twentythree twentysix twentynine thirtytwo

Paste the following in Node 3's file
--------------------------------------------
[[INSTANCE]
#  Keep a blank space after the colon character.
#
group1:  three six nine twelve fifteen eighteen twentyone twentyfour twentyseven thirty thirtythree
Enter databases: one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen twenty twentyone twentytwo twentythree twentyfour twentyfive twentysix twentyseven twentyeight twentynine thirty thirtyone thirtytwo thirtythree thirtyfour

----------------------------------------------------------------------------------------------

Paste the following in Node 1's file
--------------------------------------------
[[INSTANCE]
#  Keep a blank space after the colon character.
#
group1: one four seven ten thirteen
group2: sixteen nineteen twentytwo twentyfive twentyeight
group3: thirtyone thirtyfour

Paste the following in Node 2's file
--------------------------------------------
[[INSTANCE]
#  Keep a blank space after the colon character.
#
group1: two five eight eleven fourteen
group2: seventeen twenty twentythree twentysix twentynine
group3: thirtytwo

Paste the following in Node 3's file
--------------------------------------------
[[INSTANCE]
#  Keep a blank space after the colon character.
#
group1: three six nine twelve fifteen
group2: eighteen twentyone twentyfour twentyseven thirty
group3: thirtythree
#!/usr/bin/env python


from __future__ import print_function


from funcy import chunks


s = raw_input("Enter databases: ")

nodes = chunks(5, s.split())

for i, node in enumerate(nodes):
    print("Paste the following in Node 1's file")
    print("--------------------------------------------")
    print("[[INSTANCE]")
    print("#  Keep a blank space after the colon character.")
    print("#")
    print("group{0:d}:".format(i), end="")
    for db in node:
        print(" " + db, end="")
def chunks(l, n):
    """ Yield successive n-sized chunks from l.
    """
    for i in xrange(0, len(l), n):
        yield l[i:i+n]
但是我需要group1最多只接收5个数据库,然后自动开始将它们插入group2。每个组最多只能容纳5个数据库。而且,数据库的数量可能远远不止34个(这个数字是一个未知变量),因此我需要不断增加组的数量来补偿这个未知变量

因此,我希望输出如下所示:

Enter databases: one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen twenty twentyone twentytwo twentythree twentyfour twentyfive twentysix twentyseven twentyeight twentynine thirty thirtyone thirtytwo thirtythree thirtyfour
----------------------------------------------------------------------------------------------

Paste the following in Node 1's file
--------------------------------------------
[[INSTANCE]
#  Keep a blank space after the colon character.
#
group1: one four seven ten thirteen sixteen nineteen twentytwo twentyfive twentyeight thirtyone thirtyfour

Paste the following in Node 2's file
--------------------------------------------
[[INSTANCE]
#  Keep a blank space after the colon character.
#
group1:  two five eight eleven fourteen seventeen twenty twentythree twentysix twentynine thirtytwo

Paste the following in Node 3's file
--------------------------------------------
[[INSTANCE]
#  Keep a blank space after the colon character.
#
group1:  three six nine twelve fifteen eighteen twentyone twentyfour twentyseven thirty thirtythree
Enter databases: one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen twenty twentyone twentytwo twentythree twentyfour twentyfive twentysix twentyseven twentyeight twentynine thirty thirtyone thirtytwo thirtythree thirtyfour

----------------------------------------------------------------------------------------------

Paste the following in Node 1's file
--------------------------------------------
[[INSTANCE]
#  Keep a blank space after the colon character.
#
group1: one four seven ten thirteen
group2: sixteen nineteen twentytwo twentyfive twentyeight
group3: thirtyone thirtyfour

Paste the following in Node 2's file
--------------------------------------------
[[INSTANCE]
#  Keep a blank space after the colon character.
#
group1: two five eight eleven fourteen
group2: seventeen twenty twentythree twentysix twentynine
group3: thirtytwo

Paste the following in Node 3's file
--------------------------------------------
[[INSTANCE]
#  Keep a blank space after the colon character.
#
group1: three six nine twelve fifteen
group2: eighteen twentyone twentyfour twentyseven thirty
group3: thirtythree
#!/usr/bin/env python


from __future__ import print_function


from funcy import chunks


s = raw_input("Enter databases: ")

nodes = chunks(5, s.split())

for i, node in enumerate(nodes):
    print("Paste the following in Node 1's file")
    print("--------------------------------------------")
    print("[[INSTANCE]")
    print("#  Keep a blank space after the colon character.")
    print("#")
    print("group{0:d}:".format(i), end="")
    for db in node:
        print(" " + db, end="")
def chunks(l, n):
    """ Yield successive n-sized chunks from l.
    """
    for i in xrange(0, len(l), n):
        yield l[i:i+n]

但我完全不知道该怎么做。我认为这可以通过while循环中的嵌套for循环来实现,但我一直无法获得所需的结果。我不确定我是否只是想得太多了,或者也许我只是需要休息一下。有什么建议可以帮助我吗?

如果我正确理解你的问题,这只是一个简单的项目列表,分为几块。使用:

您的代码可以这样重新编写:

Enter databases: one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen twenty twentyone twentytwo twentythree twentyfour twentyfive twentysix twentyseven twentyeight twentynine thirty thirtyone thirtytwo thirtythree thirtyfour
----------------------------------------------------------------------------------------------

Paste the following in Node 1's file
--------------------------------------------
[[INSTANCE]
#  Keep a blank space after the colon character.
#
group1: one four seven ten thirteen sixteen nineteen twentytwo twentyfive twentyeight thirtyone thirtyfour

Paste the following in Node 2's file
--------------------------------------------
[[INSTANCE]
#  Keep a blank space after the colon character.
#
group1:  two five eight eleven fourteen seventeen twenty twentythree twentysix twentynine thirtytwo

Paste the following in Node 3's file
--------------------------------------------
[[INSTANCE]
#  Keep a blank space after the colon character.
#
group1:  three six nine twelve fifteen eighteen twentyone twentyfour twentyseven thirty thirtythree
Enter databases: one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen twenty twentyone twentytwo twentythree twentyfour twentyfive twentysix twentyseven twentyeight twentynine thirty thirtyone thirtytwo thirtythree thirtyfour

----------------------------------------------------------------------------------------------

Paste the following in Node 1's file
--------------------------------------------
[[INSTANCE]
#  Keep a blank space after the colon character.
#
group1: one four seven ten thirteen
group2: sixteen nineteen twentytwo twentyfive twentyeight
group3: thirtyone thirtyfour

Paste the following in Node 2's file
--------------------------------------------
[[INSTANCE]
#  Keep a blank space after the colon character.
#
group1: two five eight eleven fourteen
group2: seventeen twenty twentythree twentysix twentynine
group3: thirtytwo

Paste the following in Node 3's file
--------------------------------------------
[[INSTANCE]
#  Keep a blank space after the colon character.
#
group1: three six nine twelve fifteen
group2: eighteen twentyone twentyfour twentyseven thirty
group3: thirtythree
#!/usr/bin/env python


from __future__ import print_function


from funcy import chunks


s = raw_input("Enter databases: ")

nodes = chunks(5, s.split())

for i, node in enumerate(nodes):
    print("Paste the following in Node 1's file")
    print("--------------------------------------------")
    print("[[INSTANCE]")
    print("#  Keep a blank space after the colon character.")
    print("#")
    print("group{0:d}:".format(i), end="")
    for db in node:
        print(" " + db, end="")
def chunks(l, n):
    """ Yield successive n-sized chunks from l.
    """
    for i in xrange(0, len(l), n):
        yield l[i:i+n]
使用以下示例输出:

$ python bar.py 
Enter databases: a b c d e f g h i j k l m n o p q r s t u v w x y z
Paste the following in Node 1's file
--------------------------------------------
[[INSTANCE]
#  Keep a blank space after the colon character.
#
group0: a b c d ePaste the following in Node 1's file
--------------------------------------------
等等

更新:

顺便说一下,您可以像这样实现

Enter databases: one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen twenty twentyone twentytwo twentythree twentyfour twentyfive twentysix twentyseven twentyeight twentynine thirty thirtyone thirtytwo thirtythree thirtyfour
----------------------------------------------------------------------------------------------

Paste the following in Node 1's file
--------------------------------------------
[[INSTANCE]
#  Keep a blank space after the colon character.
#
group1: one four seven ten thirteen sixteen nineteen twentytwo twentyfive twentyeight thirtyone thirtyfour

Paste the following in Node 2's file
--------------------------------------------
[[INSTANCE]
#  Keep a blank space after the colon character.
#
group1:  two five eight eleven fourteen seventeen twenty twentythree twentysix twentynine thirtytwo

Paste the following in Node 3's file
--------------------------------------------
[[INSTANCE]
#  Keep a blank space after the colon character.
#
group1:  three six nine twelve fifteen eighteen twentyone twentyfour twentyseven thirty thirtythree
Enter databases: one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen twenty twentyone twentytwo twentythree twentyfour twentyfive twentysix twentyseven twentyeight twentynine thirty thirtyone thirtytwo thirtythree thirtyfour

----------------------------------------------------------------------------------------------

Paste the following in Node 1's file
--------------------------------------------
[[INSTANCE]
#  Keep a blank space after the colon character.
#
group1: one four seven ten thirteen
group2: sixteen nineteen twentytwo twentyfive twentyeight
group3: thirtyone thirtyfour

Paste the following in Node 2's file
--------------------------------------------
[[INSTANCE]
#  Keep a blank space after the colon character.
#
group1: two five eight eleven fourteen
group2: seventeen twenty twentythree twentysix twentynine
group3: thirtytwo

Paste the following in Node 3's file
--------------------------------------------
[[INSTANCE]
#  Keep a blank space after the colon character.
#
group1: three six nine twelve fifteen
group2: eighteen twentyone twentyfour twentyseven thirty
group3: thirtythree
#!/usr/bin/env python


from __future__ import print_function


from funcy import chunks


s = raw_input("Enter databases: ")

nodes = chunks(5, s.split())

for i, node in enumerate(nodes):
    print("Paste the following in Node 1's file")
    print("--------------------------------------------")
    print("[[INSTANCE]")
    print("#  Keep a blank space after the colon character.")
    print("#")
    print("group{0:d}:".format(i), end="")
    for db in node:
        print(" " + db, end="")
def chunks(l, n):
    """ Yield successive n-sized chunks from l.
    """
    for i in xrange(0, len(l), n):
        yield l[i:i+n]

大量借用:

而不是使用任何额外的模块,如果您想更正代码的逻辑,只需按如下所示更改while循环块

i = 0
node1list = [[]]
node2list = [[]]
node3list = [[]]

while len(db_list) > 0:
    if len(node1list[i]) < 5:
        node1list[i].append(db_list.pop(0))
    elif len(node2list[i]) < 5:
        node2list[i].append(db_list.pop(0))
    elif len(node3list[i]) < 5:
        node3list[i].append(db_list.pop(0))
    else:
        node1list.append([])
        if len(db_list) > 5:
            node2list.append([])
            if len(db_list) > 10:
                node3list.append([])
        i += 1
i=0
node1list=[[]]
node2list=[[]]
node3list=[[]]
而len(db_列表)>0:
如果len(节点列表[i])<5:
node1list[i].append(db_list.pop(0))
elif len(节点2列表[i])<5:
node2list[i].append(db_list.pop(0))
elif len(节点3列表[i])<5:
node3list[i].append(db_list.pop(0))
其他:
node1list.append([])
如果len(db_列表)>5:
node2list.append([])
如果len(db_列表)>10:
node3list.append([])
i+=1

数据库按您在此处看到的方式分布的原因是,用户根据从大到小的大小输入数据库列表。因此,这种类型的分布有助于在3个节点上保持所有内容的水平。虽然这在技术上可能是正确的,但它无法处理大于15的项目列表。事实上,如果列表的大小大于15,我很确定这个循环不会因为条件而终止。太好了:)这在技术上是正确的,并且可以处理任意长度的列表。但是:我仍然觉得最好在这里使用良好的代码重用。无论是
funcy
还是
chunk
都符合我的答案。这段代码的逻辑似乎比我发布的要好,但重复的不是节点,而是节点中的组。只有3个节点,节点内的组数需要增加。我需要一些类似的内容:node1.group[],node2.group[]和node3.group[]。节点中的组数必须有一个上限,否则可以在单个节点中容纳完整的
db_列表。我更新了代码,考虑到所有三个节点之间的平均分布,遵循索引的优先级。我喜欢使用块的想法。。。请记住,重复的不是节点,而是节点中的组。只有3个节点,节点内的组数需要增加。我需要像这样的东西:node1.group[],node2.group[],和node3.group[]。也许我的第一个模式可以像以前一样填充节点,然后我可以将它们分割成块。这样的事情可能吗?node1groups=chunks(5,node1list.split()),node2groups=chunks(5,node2list.split()),node3groups=chunks(5,node3list.split())。