2016-04-03 33 views
0

我創建的列表理解給我提供以下內容:使用從輸入值()的列表理解創建追加單個列表

listoflists = [[]爲i的範圍(252×5)]

然後,我將變量newlists中的列表簡化爲僅包含範圍(周)中的列表數,這是一個動態變量。

我想在指定範圍的下一個循環中附加每個單獨的列表,使其追加過程在每個列表達到指定長度後移動。這些值是從輸入函數生成的。例如,如果newlists中的第一個列表長度超過5,我希望第五個循環後面的值會附加到下一個列表中,依此類推。我現在擁有的代碼是:

p = 0 
    singlist = [] 
    listoflists = [[] for i in range(252*5)] 
    newlists= [listoflists[i] for i in range(weeks)] 
    while p<(int(people)*weeks): #fix appending process 
     for i in range(int(people)*weeks): 
     weekly =input("Put your hours: ") 
     singlist.append(int(weekly)) 
     p += 1 
     if weekly.isalpha() == True: 
     print("Not a valid amount of time") 

for i in range(0,weeks): 
    while len(newlists[i])<int(people): 
    newlists[i].append(singlist[i])  

但是,此代碼會將相同的值附加到範圍星期中的所有列表中。什麼是解決這個問題的最有效的方法?謝謝!

如果singlist = [10,15,20,25] 願望輸出newlists是:[10,15],[20,25]

如何已經構造的程序:

import sys 
import numpy as np 
import pandas as pd 
from datetime import tzinfo,timedelta,datetime 
import matplotlib.pyplot as plt 
import itertools as it 
from itertools import count,islice 

team = [] 
y = 0 
while y == 0: 
    try: 
     people = input("How many people are on your engagement? ") 
     if people.isdigit() == True: 
      y += 1 
    except: 
     print("Not a number try again") 

z= 0 
while z<int(people): 
     for i in range(int(people)): 
       names = input("Name: ") 
       if names.isalpha() == False: 
        team.append(names) 
        z+=1 
       elif names.isdigit() == True: 
        print("Not a name try again") 



ties = [] # fix looping for more than one person 
e = 0 
while e<int(people): 
    for i in range(int(people)): 
     title = input("What is their title: ") 
     if title.isdigit() == True: 
      print("Not a title try again") 
     else: 
      ties.append(title) 
      e+=1 


values = [] #fix looping for more than one person 
t= 0 
while t <int(people): 
    for i in range(int(people)): 
     charge = input("How much are you charging for them: ") 
     if charge.isalpha() == True: 
      print("Not a valid rate") 
     else: 
      values.append(int(charge)) 
      t +=1 


weeks = int(input("How many weeks are you including: ")) 
days = [] 
x = 0 
while x<weeks: #include a parameter for dates of a 7 day difference to only be permitted 
    try: 
     for i in range(int(weeks)): 
      dates = input("Input the dates (mm/dd/yy): ") 
      dt_start = datetime.strptime(dates,'%m/%d/%y') 
      days.append(dates) 
      x+=1 
    except: 
     print("Incorrect format")    


p = 0 
singlist = [] 
listoflists = [[] for i in range(252*5)] 
newlists= [listoflists[i] for i in range(weeks)] 
while p<(int(people)*weeks): #fix appending process 
    for i in range(int(people)*weeks): 
     weekly =input("Put your hours: ") 
     singlist.append(int(weekly)) 
     p += 1 
     if weekly.isalpha() == True: 
      print("Not a valid amount of time") 


def func(items,n): 
    items = iter(items) 
    for i in it.count(): 
     out = it.islice(items,weeks*i,weeks*i+n) 
     if not out: 
      break 
output = list(func(singlist,weeks)) 
+0

'len()'函數的數量可能表明有更好的方法來做到這一點。你能說出你編寫這段代碼的原始問題嗎? –

+0

我使用我編寫的新代碼編輯了上面的代碼,並希望添加希望能幫助理解的所需輸出;我正在解決的問題是創建一個動態預算工具,其中有多個輸入循環的值匯聚到一個數據框。我想要的輸出是在上面的編輯 –

回答

0
# items = [1,2,3,...n] 
# output = [[1,2],[3,4],..], n = 2 elements each 
items_ = iter(items) 
outiter = iter(lambda: [next(items_) for i in range(n)],[]) 
outlist = list(outiter) 

你可以使用while循環同樣的事情在地方count()[a:b]片操作的列表,而不是islice()。但是使用迭代器非常有效。

+0

謝謝! iter(物品)功能是什麼功能?我剛剛運行代碼,並且收到'module'對象不可調用 –

+0

'iter()'是一個內置函數。你是否得到了該行的錯誤?在口譯員處鍵入'iter',告訴我說什麼。 –

+0

我解決了這個問題,現在不得不重新啓動我的內核,但是我在重啓環境中運行了代碼,現在它凍結在那行代碼中。可能是我的記憶的數量 –

相關問題