Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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_Loops_Dictionary - Fatal编程技术网

Python 在不同长度上平行循环

Python 在不同长度上平行循环,python,loops,dictionary,Python,Loops,Dictionary,其思想是在迭代字典变量的每个键时,从每个键中获取第一个整数,然后在下一次迭代中,从列表中获取每个键的第二个整数,然后从每个键中仅获取第三个整数,依此类推 我有理想的输出,但是,我想使它尽可能普遍。这意味着,与静态索引顺序应用程序不同,我需要两个并行循环。(也许izip就是解决方案) 我的代码: dictionary = {0:[9, 9, 10, 8, 8, 8, 8], 1: [12,9,8,9,9,6,7], 2:[12, 12, 12, 10, 8, 9, 6], 3:[11, 10, 9

其思想是在迭代字典变量的每个键时,从每个键中获取第一个整数,然后在下一次迭代中,从列表中获取每个键的第二个整数,然后从每个键中仅获取第三个整数,依此类推

我有理想的输出,但是,我想使它尽可能普遍。这意味着,与静态索引顺序应用程序不同,我需要两个并行循环。(也许izip就是解决方案)

我的代码:

dictionary = {0:[9, 9, 10, 8, 8, 8, 8], 1: [12,9,8,9,9,6,7], 2:[12, 12, 12, 10, 8, 9, 6], 3:[11, 10, 9, 12, 11, 10, 9]}

horizontal_dict = {}      
l1 = [] 
l2 = [] 
l3 =[] 
l4 = [] 
l5 =[] 
l6= []
l7 = []
for i in range(len(dictionary)):
    l1.append(dictionary[i][0])
    l2.append(dictionary[i][1])
    l3.append(dictionary[i][2])
    l4.append(dictionary[i][3])
    l5.append(dictionary[i][4])
    l6.append(dictionary[i][5])
    l7.append(dictionary[i][6])
    
    horizontal_dict[1] = l1
    horizontal_dict[2] = l2
    horizontal_dict[3] = l3
    horizontal_dict[4] = l4
    horizontal_dict[5] = l5
    horizontal_dict[6] = l6
    horizontal_dict[7] = l7        
词典结果:

dictionary
Out[38]: 
{0: [9, 9, 10, 8, 8, 8, 8],
 1: [12, 9, 8, 9, 9, 6, 7],
 2: [12, 12, 12, 10, 8, 9, 6],
 3: [11, 10, 9, 12, 11, 10, 9]}
预期结果:

horizontal_dict
Out[37]: 
{1: [9, 12, 12, 11],
 2: [9, 9, 12, 10],
 3: [10, 8, 12, 9],
 4: [8, 9, 10, 12],
 5: [8, 9, 8, 11],
 6: [8, 6, 9, 10],
 7: [8, 7, 6, 9]}

使用循环:

dictionary = {
    0: [9, 9, 10, 8, 8, 8, 8],
    1: [12, 9, 8, 9, 9, 6, 7],
    2: [12, 12, 12, 10, 8, 9, 6],
    3: [11, 10, 9, 12, 11, 10, 9, 13]
}

new = {}

for n in range(max(len(v) for v in dictionary.values())):
    new[n] = []
    for k, lst in dictionary.items():
        if n < len(lst):
            new[n].append(lst[n])
        else:
            # no element at index n
            # use a default value!
            new[n].append(-1)

print(new)
使用循环:

dictionary = {
    0: [9, 9, 10, 8, 8, 8, 8],
    1: [12, 9, 8, 9, 9, 6, 7],
    2: [12, 12, 12, 10, 8, 9, 6],
    3: [11, 10, 9, 12, 11, 10, 9, 13]
}

new = {}

for n in range(max(len(v) for v in dictionary.values())):
    new[n] = []
    for k, lst in dictionary.items():
        if n < len(lst):
            new[n].append(lst[n])
        else:
            # no element at index n
            # use a default value!
            new[n].append(-1)

print(new)

您只需一行就可以做到这一点:

dictionary = {0:[9, 9, 10, 8, 8, 8, 8], 1: [12,9,8,9,9,6,7], 2:[12, 12, 12, 10, 8, 9, 6], 3:[11, 10, 9, 12, 11, 10, 9]} 
horizontal_dict={key:list(data) for data,key in zip(zip(*dictionary.values()),dictionary.keys())}

您只需一行就可以做到这一点:

dictionary = {0:[9, 9, 10, 8, 8, 8, 8], 1: [12,9,8,9,9,6,7], 2:[12, 12, 12, 10, 8, 9, 6], 3:[11, 10, 9, 12, 11, 10, 9]} 
horizontal_dict={key:list(data) for data,key in zip(zip(*dictionary.values()),dictionary.keys())}

itertools.zip_longest()
?可能类似于此,但在我的例子中,如果您查看代码,因为itertools.zip_longest会生成“none”值以弥补差异,因此带有['none'值的列表索引将导致错误@约翰科勒曼你是如何决定有七个列表的?有一个自定义函数,它将整数输入作为参数,从存储的数据帧计算迭代次数,但必须以正确的方式旋转,正如我在上面的代码解决方案/示例中解释的那样@python_user
itertools.zip_longest()
?可能类似于此,但在我的例子中,如果您查看代码,由于itertools.zip_longest生成“none”值以弥补差异,因此具有['none']值的列表索引将导致错误@约翰科勒曼你是如何决定有七个列表的?有一个自定义函数,它将整数输入作为参数,从存储的数据帧计算迭代次数,但必须以正确的方式旋转,正如我在上面的代码解决方案/示例中解释的那样@这个答案是正确的!如果没有人给我任何其他的提示或其他解决方法,我会接受的。谢谢@Maurice Meyer!这个答案是正确的!如果没有人给我任何其他的提示或其他解决方法,我会接受的。谢谢@Maurice Meyer!好读!我更新它,使其只在一行中工作-漂亮且可读!我将其更新为只在一行中工作