我創建的列表理解給我提供以下內容:使用從輸入值()的列表理解創建追加單個列表
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))
'len()'函數的數量可能表明有更好的方法來做到這一點。你能說出你編寫這段代碼的原始問題嗎? –
我使用我編寫的新代碼編輯了上面的代碼,並希望添加希望能幫助理解的所需輸出;我正在解決的問題是創建一個動態預算工具,其中有多個輸入循環的值匯聚到一個數據框。我想要的輸出是在上面的編輯 –