簡體   English   中英

列表/雙端隊列和嵌套的for循環

[英]list/deque and nested for-loop

我是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

但是我需要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
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

但是我完全不知道該怎么做。 我認為可以在while循環中使用嵌套的for循環來實現,但我一直無法獲得所需的結果。 我不確定是否只是在思考問題,還是只是需要休息一下。 有什么建議可以幫助我嗎?

如果我正確理解了您的問題,這只是分成幾小塊的簡單項目清單。 使用funcy

>>> from funcy import chunks
>>> s = "one two three four five six seven eight nine ten eleven"
>>> databases = chunks(5, s.split())
>>> databases
[['one', 'two', 'three', 'four', 'five'], ['six', 'seven', 'eight', 'nine', 'ten'], ['eleven']]
>>> databases[0]
['one', 'two', 'three', 'four', 'five']
>>> databases[1]
['six', 'seven', 'eight', 'nine', 'ten']
>>> databases[2]
['eleven']
>>> len(databases[0])
5
>>> len(databases[1])
5
>>> len(databases[2])
1

然后可以像下面這樣重寫您的代碼:

#!/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="")

具有以下示例輸出:

$ 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
--------------------------------------------

等等...

更新:

順便說一句,你可以實現chunks這樣的:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM