python - list/deque and nested for-loop -
python - list/deque and nested for-loop -
i'm new python , running bit of road block. i'm using python 3 , have next code:
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 next in node 1's file") print("--------------------------------------------") print("[[instance]") print("# maintain blank space after colon character.") print("#") print("group1:", end="") db in node1list: print(" " + db, end="") print("\n\npaste next in node 2's file") print("--------------------------------------------") print("[[instance]") print("# maintain blank space after colon character.") print("#") print("group1: ", end="") db in node2list: print(" " + db, end="") print("\n\npaste next in node 3's file") print("--------------------------------------------") print("[[instance]") print("# maintain blank space after colon character.") print("#") print("group1: ", end="") db in node3list: print(" " + db, end="")
when run code, output this:
enter databases: 1 2 3 4 5 6 7 8 9 10 11 twelve 13 14 15 16 seventeen 18 19 20 twentyone twentytwo twentythree twentyfour twentyfive twentysix twentyseven twentyeight twentynine 30 thirtyone thirtytwo thirtythree thirtyfour ---------------------------------------------------------------------------------------------- paste next in node 1's file -------------------------------------------- [[instance] # maintain blank space after colon character. # group1: 1 4 7 10 13 16 19 twentytwo twentyfive twentyeight thirtyone thirtyfour paste next in node 2's file -------------------------------------------- [[instance] # maintain blank space after colon character. # group1: 2 5 8 11 14 seventeen 20 twentythree twentysix twentynine thirtytwo paste next in node 3's file -------------------------------------------- [[instance] # maintain blank space after colon character. # group1: 3 6 9 twelve 15 18 twentyone twentyfour twentyseven 30 thirtythree
but need group1 take maximum of 5 databases, , automatically start plugging them group2. each grouping can hold 5 databases. also, number of databases far more 34 (that number unknown variable), need ability maintain increasing number of groups compensate unknown variable.
so output this:
enter databases: 1 2 3 4 5 6 7 8 9 10 11 twelve 13 14 15 16 seventeen 18 19 20 twentyone twentytwo twentythree twentyfour twentyfive twentysix twentyseven twentyeight twentynine 30 thirtyone thirtytwo thirtythree thirtyfour ---------------------------------------------------------------------------------------------- paste next in node 1's file -------------------------------------------- [[instance] # maintain blank space after colon character. # group1: 1 4 7 10 13 group2: 16 19 twentytwo twentyfive twentyeight group3: thirtyone thirtyfour paste next in node 2's file -------------------------------------------- [[instance] # maintain blank space after colon character. # group1: 2 5 8 11 14 group2: seventeen 20 twentythree twentysix twentynine group3: thirtytwo paste next in node 3's file -------------------------------------------- [[instance] # maintain blank space after colon character. # group1: 3 6 9 twelve 15 group2: 18 twentyone twentyfour twentyseven 30 group3: thirtythree
but i'm @ finish loss @ how this. think can achieved nested for-loop within while-loop haven't been able results need. i'm not sure if i'm on thinking things or maybe need take break lol. suggestions can help me out?
if understand problem correctly simple list of items split chunks. using funcy:
>>> funcy import chunks >>> s = "one 2 3 4 5 6 7 8 9 10 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
your code re-written this:
#!/usr/bin/env python __future__ import print_function funcy import chunks s = raw_input("enter databases: ") nodes = chunks(5, s.split()) i, node in enumerate(nodes): print("paste next in node 1's file") print("--------------------------------------------") print("[[instance]") print("# maintain blank space after colon character.") print("#") print("group{0:d}:".format(i), end="") db in node: print(" " + db, end="")
with next sample output:
$ python bar.py come in databases: b c d e f g h j k l m n o p q r s t u v w x y z paste next in node 1's file -------------------------------------------- [[instance] # maintain blank space after colon character. # group0: b c d epaste next in node 1's file --------------------------------------------
and on...
update:
by way can implement chunks
this:
def chunks(l, n): """ yield successive n-sized chunks l. """ in xrange(0, len(l), n): yield l[i:i+n]
liberally borrowed from: how split list evenly sized chunks in python?
python while-loop nested-loops nested-lists deque
Comments
Post a Comment