使用input()中的值追加从列表推导创建的单个列表

Appending individual lists created from a list comprehension using values from input()

我创建了一个列表理解,为我提供以下内容:

listoflists=[]i在范围内(252*5)]

然后,我将变量newlists中的列表简化为只包含范围(周)内的列表数,这是一个动态变量。

我想在下面的循环中为指定的范围附加每个单独的列表,在每个列表达到指定的长度后,附加过程将在其中移动。这些值由输入函数生成。例如,如果newlists中的第一个列表的长度超过5,我希望第5个循环后面的值附加到下一个列表,依此类推。我目前拥有的代码是:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  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]新列表的期望输出为:【10,15】,【20,25】]

我如何组织计划:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
    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))


1
2
3
4
5
# 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()来完成相同的操作。但是使用迭代器是非常有效的。