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

Python 如何遍历字典中的列表并为每个索引运行命令?

Python 如何遍历字典中的列表并为每个索引运行命令?,python,Python,我有一个字典,索引号是键,值中有5个整数。我想运行一个set命令,以便为所有索引编号逐个设置列表第0个元素的速度,然后根据索引对其他元素重复相同的操作。通过在列表元素范围内运行它,我可以实现同样的效果。有人能帮助实现这一点吗? 下面是字典 dict = {0: [13440, 7000, 8800, 11000, 15000], 1: [14310, 7000, 8800, 11000, 15000], 2: [13410, 7000, 8800, 11000, 15000], 3: [1413

我有一个字典,索引号是键,值中有5个整数。我想运行一个set命令,以便为所有索引编号逐个设置列表第0个元素的速度,然后根据索引对其他元素重复相同的操作。通过在列表元素范围内运行它,我可以实现同样的效果。有人能帮助实现这一点吗? 下面是字典

dict = {0: [13440, 7000, 8800, 11000, 15000], 1: [14310, 7000, 8800, 11000, 15000], 2: [13410, 7000, 8800, 11000, 15000], 3: [14130, 7000, 8800, 11000, 15000], 4: [13380, 7000, 8800, 11000, 15000], 5: [14280, 7000, 8800, 11000, 15000], 6: [13500, 7000, 8800, 11000, 15000], 7: [14190, 7000, 8800, 11000, 15000], 8: [9150, 3000, 3800, 4800, 6000, 8500], 9: [8670, 3000, 3800, 4800, 6000, 8500]}

for index, speeds in dict.items():
    for i in range(len(speeds)):
        cmd = 'set %d speed %s' % (index, speeds[i])
        print(cmd)
我希望cmd能够遍历索引0,1,2,3…8,9的列表的第0个元素。同样,它应该为所有索引的第一个元素逐个运行。因此,基本上应该先设置每个索引编号的第一个速度,然后再设置每个索引编号的第二个速度,依此类推。希望我的问题清楚。 我得到的电流输出是

set 0 speed 13440
set 0 speed 7000
set 0 speed 8800
set 0 speed 11000
set 0 speed 15000
set 1 speed 14310
.
.
. 
set 9 speed 8500
我期待着像这样的事情

set 0 speed 13440
set 1 speed 14310
set 2 speed 13410
set 3 speed 14130
.
.
and so on

您可以确定
dict
中列表的最大长度,并在外部循环中迭代每个索引,同时在内部循环中循环
dict
的键。注意长度小于最大值的列表

编辑:使用在速度变化之间添加睡眠。将变量名更改为更具描述性

import time

dict = {0: [13440, 7000, 8800, 11000, 15000], 1: [14310, 7000, 8800, 11000, 15000], 2: [13410, 7000, 8800, 11000, 15000], 3: [14130, 7000, 8800, 11000, 15000], 4: [13380, 7000, 8800, 11000, 15000], 5: [14280, 7000, 8800, 11000, 15000], 6: [13500, 7000, 8800, 11000, 15000], 7: [14190, 7000, 8800, 11000, 15000], 8: [9150, 3000, 3800, 4800, 6000, 8500], 9: [8670, 3000, 3800, 4800, 6000, 8500]}

# The maximum length of dict's lists of speeds
max_speed_count = max(len(lst) for lst in dict.values())

for speed_index in range(max_speed_count):
    for fan_index in dict:
        # If one of the lists has smaller length, catch the error and continue
        try:
            speed = dict[fan_index][speed_index]
        except IndexError:
            continue
        
        cmd = 'fan_set %d speed %s' % (fan_index, speed)
        print(cmd)

    # Sleep for a minute between speed changes
    time.sleep(60)

您可以确定
dict
中列表的最大长度,并在外部循环中迭代每个索引,同时在内部循环中循环
dict
的键。注意长度小于最大值的列表

编辑:使用在速度变化之间添加睡眠。将变量名更改为更具描述性

import time

dict = {0: [13440, 7000, 8800, 11000, 15000], 1: [14310, 7000, 8800, 11000, 15000], 2: [13410, 7000, 8800, 11000, 15000], 3: [14130, 7000, 8800, 11000, 15000], 4: [13380, 7000, 8800, 11000, 15000], 5: [14280, 7000, 8800, 11000, 15000], 6: [13500, 7000, 8800, 11000, 15000], 7: [14190, 7000, 8800, 11000, 15000], 8: [9150, 3000, 3800, 4800, 6000, 8500], 9: [8670, 3000, 3800, 4800, 6000, 8500]}

# The maximum length of dict's lists of speeds
max_speed_count = max(len(lst) for lst in dict.values())

for speed_index in range(max_speed_count):
    for fan_index in dict:
        # If one of the lists has smaller length, catch the error and continue
        try:
            speed = dict[fan_index][speed_index]
        except IndexError:
            continue
        
        cmd = 'fan_set %d speed %s' % (fan_index, speed)
        print(cmd)

    # Sleep for a minute between speed changes
    time.sleep(60)

你发布的代码有什么具体的问题?附上我得到的输出和所需的输出@kaya3你发布的代码有什么具体的问题?附上我得到的输出和所需的输出@kaya3。这非常有帮助。在这之后还有一个步骤,我不知道如何实现。因此,在使用列表的第0个元素为所有索引设置风扇速度后,我需要引入1分钟的延迟,然后为所有索引的第1个元素运行,以此类推。有没有可能@Božo Stojković?你只需要
导入时间
,然后使用
时间。睡眠
,它需要几秒钟的睡眠时间作为参数@Jayashri认为代码完全符合您所描述的。也许你没有把它复制正确?注意缩进。这非常有帮助。在这之后还有一个步骤,我不知道如何实现。因此,在使用列表的第0个元素为所有索引设置风扇速度后,我需要引入1分钟的延迟,然后为所有索引的第1个元素运行,以此类推。有没有可能@Božo Stojković?你只需要
导入时间
,然后使用
时间。睡眠
,它需要几秒钟的睡眠时间作为参数@Jayashri认为代码完全符合您所描述的。也许你没有把它复制正确?注意压痕。