Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:寻找更有效的方法将列表排序到子列表中,以解释丢失的输入_Python_List_Python 3.x_Matrix - Fatal编程技术网

Python:寻找更有效的方法将列表排序到子列表中,以解释丢失的输入

Python:寻找更有效的方法将列表排序到子列表中,以解释丢失的输入,python,list,python-3.x,matrix,Python,List,Python 3.x,Matrix,我对Python3还是有点陌生,虽然我有一些可以工作的东西,但我认为它可以更高效、更可读 例如,如果我有输入: A1, B1, C1, A2, B2, A3, C3, C4 我想将其转换为列(以便最终将其放入excel电子表格),如下所示: 1 A B C 2 A B None 3 A None C 4 None None C locations = ["A", "B", "C"] log = [["A",1],["B"

我对Python3还是有点陌生,虽然我有一些可以工作的东西,但我认为它可以更高效、更可读

例如,如果我有输入:

A1, B1, C1, A2, B2, A3, C3, C4 
我想将其转换为列(以便最终将其放入excel电子表格),如下所示:

 1   A    B    C
 2   A    B    None
 3   A    None C
 4   None None C
locations  = ["A", "B", "C"]
log        = [["A",1],["B",1],["C",1],["A",2],["B",2],["A",3],["C",3],["C",4]]
day        = [ [] for x in range(10) ]  # can I dynamically allocate this as I go?

i = 0

for index, element in enumerate(log):
    if (log[index][1] != log[index-1][1] and index != 0):  # if the number changes
        for place in locations:                           
            if place not in day[i]:                        # if something's missing
                day[i].insert(locations.index(place),None) # insert a None where its missing
        i += 1

    if element[0] in locations:                            
        day[i].append(element[0])

for place in locations:     # the loop ends without doing the last list so I call 
    if place not in day[i]: # this again, is there a way to keep it in the loop?
        day[i].insert(locations.index(place),None) 

day = [x for x in day if x != []]   # strips empty lists from list

columns = list(zip(*day))           # transposes matrix 
我的代码如下所示:

 1   A    B    C
 2   A    B    None
 3   A    None C
 4   None None C
locations  = ["A", "B", "C"]
log        = [["A",1],["B",1],["C",1],["A",2],["B",2],["A",3],["C",3],["C",4]]
day        = [ [] for x in range(10) ]  # can I dynamically allocate this as I go?

i = 0

for index, element in enumerate(log):
    if (log[index][1] != log[index-1][1] and index != 0):  # if the number changes
        for place in locations:                           
            if place not in day[i]:                        # if something's missing
                day[i].insert(locations.index(place),None) # insert a None where its missing
        i += 1

    if element[0] in locations:                            
        day[i].append(element[0])

for place in locations:     # the loop ends without doing the last list so I call 
    if place not in day[i]: # this again, is there a way to keep it in the loop?
        day[i].insert(locations.index(place),None) 

day = [x for x in day if x != []]   # strips empty lists from list

columns = list(zip(*day))           # transposes matrix 
我的输出是:

 [('A', 'A', 'A', None), ('B', 'B', None, None), ('C', None, 'C', 'C')]
所以我的问题是:我怎样才能使这更有效?我可以一边走一边在列表中分配列表吗?我如何把它都保持在for循环中


提前谢谢

下面是一个在读取日志时构建阵列的示例:

days = []
for loc, day in log:
    for i in range(len(days), day):
        days.append([i+1] + [None for _ in locations])
    days[day - 1][1 + locations.index(loc)] = loc

print(days)

[1,'A','B','C'],[2,'A','B',无],[3,'A',无,'C'],[4,无,无,'C']

我会制作一个以位置元组为键的字典,并从默认值开始

columns = ("A","B","C")
rows = (1,2,3,4)
table = { (col, row):None for col in columns for row in rows}
然后,您可以通过循环日志来编辑表,。。将表中与日志中的值匹配的位置键/值转换为相应的日志/表列值

log = [["A",1],["B",1],["C",1],["A",2],["B",2],["A",3],["C",3],["C",4]]

for cell in log:
    if tuple(cell) in table:
    # if tuple(cell) in table.keys(): if python 2.7
        table[tuple(cell)] = cell[0]

print [ tuple(table[col,row] for row in rows) for col in columns]

[('A', 'A', 'A', None), ('B', 'B', None, None), ('C', None, 'C', 'C')]

我不确定这是否更有效,但它更简洁:

>>> locations  = ["A", "B", "C"]
>>> log        = [["A",1],["B",1],["C",1],["A",2],["B",2],["A",3],["C",3],["C",4]]
>>> maxi=max(i for [_,i] in log)
>>> d = {i:list() for i in locations} #d={'B': [], 'A': [], 'C': []}
>>> for [letter,i] in log:
...   d[letter].append(i)
... #d={'B': [1, 2], 'A': [1, 2, 3], 'C': [1, 3, 4]}
>>> [tuple(letter if i in d[letter] else None for i in range(1,maxi+1)) for letter in locations]
[('A', 'A', 'A', None), ('B', 'B', None, None), ('C', None, 'C', 'C')]

哇!真干净。非常感谢你。