python 我不知道为什么这个嵌套的for循环的某些部分会被跳过/错过[关闭]

xriantvc  于 2023-05-27  发布在  Python
关注(0)|答案(1)|浏览(117)

**关闭。**此题需要debugging details。目前不接受答复。

编辑问题以包括desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将帮助其他人回答这个问题。
昨天关门了。
Improve this question
我正在测试给我的这段代码,发现它不能正常工作。我在想为什么我没有

iPriority: 1 Task= {'Zone': ['A'], 'Priority': 1, 'SoundFile': 'DeebanSinging', 'Volume': 100}"

我只看到B区(位置0),然后G区(位置2)发生。
该代码应该从外部连接获取命令,排序到优先级列表,按队列将音频播放到某个区域,或者在有更高优先级时进行覆盖。

# https://www.online-python.com/online_python_compiler
# Online Python - IDE, Editor, Compiler, Interpreter
DebugPrint = True

# This is a list of tasks external API can push, append to this list
ListQTasks = [
    {'Zone':['B'],'Priority':1, 'SoundFile':'BrettSinging', 'Volume':100},
    {'Zone':['A'],'Priority':1, 'SoundFile':'DeebanSinging', 'Volume':100},
    {'Zone':['A'],'Priority':3, 'SoundFile':'AlexPlayingGuitar', 'Volume':100},
    {'Zone':['B'],'Priority':5, 'SoundFile':'DanielPlayingPiano', 'Volume':100},
    {'Zone':['F'],'Priority':4, 'SoundFile':'Sound5', 'Volume':100},
    {'Zone':['G'],'Priority':1, 'SoundFile':'Sound6', 'Volume':100},
    {'Zone':['E'],'Priority':4, 'SoundFile':'Sound7', 'Volume':100},
    {'Zone':['E'],'Priority':5, 'SoundFile':'Sound8', 'Volume':100},
]

# This is Active Tread players playing
ListActiveThreads = [
    {'Zone':['A'], 'Priority':5, 'SoundFile':'OldSoundA', 'Volume':100},
    {'Zone':['B'], 'Priority':5, 'SoundFile':'OldSoundB', 'Volume':100},
    {'Zone':['E'], 'Priority':5, 'SoundFile':'OldSoundE', 'Volume':100}
]

# This test code to periodically ingest the ListQTasks to DictListPriorityQSorting
DictListPriorityQSorting = {1:[],2:[],3:[],4:[],5:[],6:[]}
for ListIdx in range(0, len(ListQTasks)):
    Qtask = ListQTasks[ListIdx]
    DictListPriorityQSorting[Qtask['Priority']].append(Qtask)
ListQTasks.clear()

# print current Sorted Dict
if DebugPrint:
    print('\n\nDictListPriorityQSorting########################')
    print(DictListPriorityQSorting[1])
    print(DictListPriorityQSorting[2])
    print(DictListPriorityQSorting[3])
    print(DictListPriorityQSorting[4])
    print(DictListPriorityQSorting[5])
    print('################################################')

    print('\n\nListActiveThreads#######')  
    print(ListActiveThreads)
    print('\n\n########################')
    
    print('\nfor iPriority in range(1,6):')

DictListPriorityQSortingEdit = DictListPriorityQSorting.copy()
for iPriority in range(1,6):
    #print('DictListPriorityQSorting[iPriority]:',DictListPriorityQSorting[iPriority])
    if len(DictListPriorityQSorting[iPriority]) > 1:
        iPriorityListTask = DictListPriorityQSorting[iPriority]
        print('iPriorityListTask:',iPriorityListTask)
        for Task in iPriorityListTask:
            if DebugPrint:
                print('iPriority:',iPriority,'Task=',Task) #Debug
            ZoneInActive = False
            ListActiveThreadsEdit = ListActiveThreads.copy()
            for ActiveThreadIdx in range(0,len(ListActiveThreads)):
                ActiveThread = ListActiveThreads[ActiveThreadIdx]
                ZoneOverrideDone = False

                if Task['Zone'] == ActiveThread['Zone']:
                    ZoneInActive = True
                    if DebugPrint:
                        print('#########ZONEMATCHFOUND:iPriority:',iPriority,'\nTask=',Task,'\nActiveThread=',ActiveThread,'\n#######################') #Debug
                    if Task['Priority'] < ActiveThread['Priority']:
                        if DebugPrint:
                            print('->>',ActiveThread,'\n####OVERRIDE to####\n->>', Task)
                        #Deeban add your override call functions here
                        #ListActiveThreads[ActiveThreadIdx] = Task
                        ListActiveThreadsEdit.remove(ActiveThread)
                        ListActiveThreadsEdit.append(Task)
                        #ActiveThread = Task
                        DictListPriorityQSortingEdit[iPriority].remove(Task)
                        ZoneOverrideDone = True
                        #break
            if not ZoneInActive:
                # Deeban add your new Task call functions here
                pass
                #if DebugPrint:
                #    print('######################ZoneNotInActive',ActiveThread)
                #    print(Task)
                #ListActiveThreadsEdit.append(Task)
                #DictListPriorityQSortingEdit[iPriority].remove(Task)
        ListActiveThreads = ListActiveThreadsEdit #FixCodeDeeban

DictListPriorityQSorting = DictListPriorityQSortingEdit.copy()
#DictListPriorityQSorting = DictListPriorityQSortingEdit.copy()
    
if DebugPrint:
    print('\n\nDictListPriorityQSorting########################')
    print(DictListPriorityQSorting[1])
    print(DictListPriorityQSorting[2])
    print(DictListPriorityQSorting[3])
    print(DictListPriorityQSorting[4])
    print(DictListPriorityQSorting[5])
    print('################################################')

    print('\n\nListActiveThreads#######')  
    print(ListActiveThreads)
    print('\n\n########################')

打印机控制台:

#DictListPriorityQSorting########################
[{'Zone': ['B'], 'Priority': 1, 'SoundFile': 'BrettSinging', 'Volume': 100}, {'Zone': ['A'], 'Priority': 1, 'SoundFile': 'DeebanSinging', 'Volume': 100}, {'Zone': ['G'], 'Priority': 1, 'SoundFile': 'Sound6', 'Volume': 100}]
[]
[{'Zone': ['A'], 'Priority': 3, 'SoundFile': 'AlexPlayingGuitar', 'Volume': 100}]
[{'Zone': ['F'], 'Priority': 4, 'SoundFile': 'Sound5', 'Volume': 100}, {'Zone': ['E'], 'Priority': 4, 'SoundFile': 'Sound7', 'Volume': 100}]
[{'Zone': ['B'], 'Priority': 5, 'SoundFile': 'DanielPlayingPiano', 'Volume': 100}, {'Zone': ['E'], 'Priority': 5, 'SoundFile': 'Sound8', 'Volume': 100}]
################################################

ListActiveThreads#######
[{'Zone': ['A'], 'Priority': 5, 'SoundFile': 'OldSoundA', 'Volume': 100}, {'Zone': ['B'], 'Priority': 5, 'SoundFile': 'OldSoundB', 'Volume': 100}, {'Zone': ['E'], 'Priority': 5, 'SoundFile': 'OldSoundE', 'Volume': 100}]

########################

for iPriority in range(1,6):
iPriorityListTask: [{'Zone': ['B'], 'Priority': 1, 'SoundFile': 'BrettSinging', 'Volume': 100}, {'Zone': ['A'], 'Priority': 1, 'SoundFile': 'DeebanSinging', 'Volume': 100}, {'Zone': ['G'], 'Priority': 1, 'SoundFile': 'Sound6', 'Volume': 100}]
**iPriority: 1 Task= {'Zone': ['B'], 'Priority': 1, 'SoundFile': 'BrettSinging', 'Volume': 100}**
#########ZONEMATCHFOUND:iPriority: 1 
Task= {'Zone': ['B'], 'Priority': 1, 'SoundFile': 'BrettSinging', 'Volume': 100} 
ActiveThread= {'Zone': ['B'], 'Priority': 5, 'SoundFile': 'OldSoundB', 'Volume': 100} 
#######################
->> {'Zone': ['B'], 'Priority': 5, 'SoundFile': 'OldSoundB', 'Volume': 100} 
####OVERRIDE to####
->> {'Zone': ['B'], 'Priority': 1, 'SoundFile': 'BrettSinging', 'Volume': 100}
**iPriority: 1 Task= {'Zone': ['G'], 'Priority': 1, 'SoundFile': 'Sound6', 'Volume': 100}**
iPriorityListTask: [{'Zone': ['F'], 'Priority': 4, 'SoundFile': 'Sound5', 'Volume': 100}, {'Zone': ['E'], 'Priority': 4, 'SoundFile': 'Sound7', 'Volume': 100}]
iPriority: 4 Task= {'Zone': ['F'], 'Priority': 4, 'SoundFile': 'Sound5', 'Volume': 100}
iPriority: 4 Task= {'Zone': ['E'], 'Priority': 4, 'SoundFile': 'Sound7', 'Volume': 100}
#########ZONEMATCHFOUND:iPriority: 4 
Task= {'Zone': ['E'], 'Priority': 4, 'SoundFile': 'Sound7', 'Volume': 100} 
ActiveThread= {'Zone': ['E'], 'Priority': 5, 'SoundFile': 'OldSoundE', 'Volume': 100} 
#######################
->> {'Zone': ['E'], 'Priority': 5, 'SoundFile': 'OldSoundE', 'Volume': 100} 
####OVERRIDE to####
->> {'Zone': ['E'], 'Priority': 4, 'SoundFile': 'Sound7', 'Volume': 100}
iPriorityListTask: [{'Zone': ['B'], 'Priority': 5, 'SoundFile': 'DanielPlayingPiano', 'Volume': 100}, {'Zone': ['E'], 'Priority': 5, 'SoundFile': 'Sound8', 'Volume': 100}]
iPriority: 5 Task= {'Zone': ['B'], 'Priority': 5, 'SoundFile': 'DanielPlayingPiano', 'Volume': 100}
#########ZONEMATCHFOUND:iPriority: 5 
Task= {'Zone': ['B'], 'Priority': 5, 'SoundFile': 'DanielPlayingPiano', 'Volume': 100} 
ActiveThread= {'Zone': ['B'], 'Priority': 5, 'SoundFile': 'OldSoundB', 'Volume': 100} 
#######################
iPriority: 5 Task= {'Zone': ['E'], 'Priority': 5, 'SoundFile': 'Sound8', 'Volume': 100}
#########ZONEMATCHFOUND:iPriority: 5 
Task= {'Zone': ['E'], 'Priority': 5, 'SoundFile': 'Sound8', 'Volume': 100} 
ActiveThread= {'Zone': ['E'], 'Priority': 4, 'SoundFile': 'Sound7', 'Volume': 100} 
#######################

DictListPriorityQSorting########################
[{'Zone': ['A'], 'Priority': 1, 'SoundFile': 'DeebanSinging', 'Volume': 100}, {'Zone': ['G'], 'Priority': 1, 'SoundFile': 'Sound6', 'Volume': 100}]
[]
[{'Zone': ['A'], 'Priority': 3, 'SoundFile': 'AlexPlayingGuitar', 'Volume': 100}]
[{'Zone': ['F'], 'Priority': 4, 'SoundFile': 'Sound5', 'Volume': 100}]
[{'Zone': ['B'], 'Priority': 5, 'SoundFile': 'DanielPlayingPiano', 'Volume': 100}, {'Zone': ['E'], 'Priority': 5, 'SoundFile': 'Sound8', 'Volume': 100}]
################################################

ListActiveThreads#######
[{'Zone': ['A'], 'Priority': 5, 'SoundFile': 'OldSoundA', 'Volume': 100}, {'Zone': ['B'], 'Priority': 5, 'SoundFile': 'OldSoundB', 'Volume': 100}, {'Zone': ['E'], 'Priority': 4, 'SoundFile': 'Sound7', 'Volume': 100}]

########################

** Process exited - Return Code: 0 **
Press Enter to exit terminal

我不知道为什么

iPriority: 1 Task= {'Zone': \['A'\], 'Priority': 1, 'SoundFile': 'DeebanSinging', 'Volume': 100}

当我在代码中有DictListPriorityQSortingEdit\[iPriority\].remove(Task)时,从来没有发生过,一旦删除它,我就看到了

iPriority: 1 Task= {'Zone': \['A'\], 'Priority': 1, 'SoundFile': 'DeebanSinging', 'Volume': 100}
wooyq4lh

wooyq4lh1#

固定编码

#https://www.online-python.com/online_python_compiler
# Online Python - IDE, Editor, Compiler, Interpreter
from copy import deepcopy
DebugPrint = True

# This is a list of tasks external API can push. Append to this list
ListQTasks = [
    {'Zone':['B'],'Priority':1, 'SoundFile':'BrettSinging', 'Volume':100},
    {'Zone':['A'],'Priority':1, 'SoundFile':'DeebanSinging', 'Volume':100},
    {'Zone':['A'],'Priority':3, 'SoundFile':'AlexPlayingGuitar', 'Volume':100},
    {'Zone':['B'],'Priority':5, 'SoundFile':'DanielPlayingPiano', 'Volume':100},
    {'Zone':['F'],'Priority':4, 'SoundFile':'Sound5', 'Volume':100},
    {'Zone':['G'],'Priority':1, 'SoundFile':'Sound6', 'Volume':100},
    {'Zone':['E'],'Priority':4, 'SoundFile':'Sound7', 'Volume':100},
    {'Zone':['E'],'Priority':5, 'SoundFile':'Sound8', 'Volume':100},
]

# This is the Active Thread players playing
ListActiveThreads = [
    {'Zone':['A'], 'Priority':5, 'SoundFile':'OldSoundA', 'Volume':100},
    {'Zone':['B'], 'Priority':5, 'SoundFile':'OldSoundB', 'Volume':100},
    {'Zone':['E'], 'Priority':5, 'SoundFile':'OldSoundE', 'Volume':100}
]

# This test code to periodically ingest the ListQTasks to DictListPriorityQSorting
DictListPriorityQSorting = {1:[],2:[],3:[],4:[],5:[],6:[]}
for ListIdx in range(0, len(ListQTasks)):
    Qtask = ListQTasks[ListIdx]
    DictListPriorityQSorting[Qtask['Priority']].append(Qtask)
ListQTasks.clear()

# Pprint current Sorted Dict
if DebugPrint:
    print('\n\nDictListPriorityQSorting########################')
    print(DictListPriorityQSorting[1])
    print(DictListPriorityQSorting[2])
    print(DictListPriorityQSorting[3])
    print(DictListPriorityQSorting[4])
    print(DictListPriorityQSorting[5])
    print('################################################')

    print('\n\nListActiveThreads#######')
    print(ListActiveThreads)
    print('\n\n########################')

    print('\nfor iPriority in range(1,6):')

DictListPriorityQSortingEdit = deepcopy(DictListPriorityQSorting)
for iPriority in range(1,6):
    #print('DictListPriorityQSorting[iPriority]:',DictListPriorityQSorting[iPriority])
    if len(DictListPriorityQSorting[iPriority]) > 1:
        iPriorityListTask = DictListPriorityQSorting[iPriority]
        print('iPriorityListTask:',iPriorityListTask)
        for Task in iPriorityListTask:
            if DebugPrint:
                print('iPriority:',iPriority,'Task=',Task) #Debug
            ZoneInActive = False
            ListActiveThreadsEdit = deepcopy(ListActiveThreads)
            for ActiveThreadIdx in range(0,len(ListActiveThreads)):
                ActiveThread = ListActiveThreads[ActiveThreadIdx]
                ZoneOverrideDone = False

                if Task['Zone'] == ActiveThread['Zone']:
                    ZoneInActive = True
                    if DebugPrint:
                        print('#########ZONEMATCHFOUND:iPriority:',iPriority,'\nTask=',Task,'\nActiveThread=',ActiveThread,'\n#######################') #Debug
                    if Task['Priority'] < ActiveThread['Priority']:
                        if DebugPrint:
                            print('->>',ActiveThread,'\n####OVERRIDE to####\n->>', Task)
                        #Deeban add your override call functions here
                        #ListActiveThreads[ActiveThreadIdx] = Task
                        ListActiveThreadsEdit.remove(ActiveThread)
                        ListActiveThreadsEdit.append(Task)
                        #ActiveThread = Task
                        DictListPriorityQSortingEdit[iPriority].remove(Task)
                        ZoneOverrideDone = True
                        #break
            if not ZoneInActive:
                #Deeban add your new Task call functions here
                pass
                #if DebugPrint:
                #    print('######################ZoneNotInActive',ActiveThread)
                #    print(Task)
                #ListActiveThreadsEdit.append(Task)
                #DictListPriorityQSortingEdit[iPriority].remove(Task)
        ListActiveThreads = deepcopy(ListActiveThreadsEdit) #FixCodeDeeban

DictListPriorityQSorting = deepcopy(DictListPriorityQSortingEdit)
#DictListPriorityQSorting = DictListPriorityQSortingEdit.copy()

if DebugPrint:
    print('\n\nDictListPriorityQSorting########################')
    print(DictListPriorityQSorting[1])
    print(DictListPriorityQSorting[2])
    print(DictListPriorityQSorting[3])
    print(DictListPriorityQSorting[4])
    print(DictListPriorityQSorting[5])
    print('################################################')

    print('\n\nListActiveThreads#######')
    print(ListActiveThreads)
    print('\n\n########################')

代码打印:

DictListPriorityQSorting########################
[{'Zone': ['B'], 'Priority': 1, 'SoundFile': 'BrettSinging', 'Volume': 100}, {'Zone': ['A'], 'Priority': 1, 'SoundFile': 'DeebanSinging', 'Volume': 100}, {'Zone': ['G'], 'Priority': 1, 'SoundFile': 'Sound6', 'Volume': 100}]
[]
[{'Zone': ['A'], 'Priority': 3, 'SoundFile': 'AlexPlayingGuitar', 'Volume': 100}]
[{'Zone': ['F'], 'Priority': 4, 'SoundFile': 'Sound5', 'Volume': 100}, {'Zone': ['E'], 'Priority': 4, 'SoundFile': 'Sound7', 'Volume': 100}]
[{'Zone': ['B'], 'Priority': 5, 'SoundFile': 'DanielPlayingPiano', 'Volume': 100}, {'Zone': ['E'], 'Priority': 5, 'SoundFile': 'Sound8', 'Volume': 100}]
################################################

ListActiveThreads#######
[{'Zone': ['A'], 'Priority': 5, 'SoundFile': 'OldSoundA', 'Volume': 100}, {'Zone': ['B'], 'Priority': 5, 'SoundFile': 'OldSoundB', 'Volume': 100}, {'Zone': ['E'], 'Priority': 5, 'SoundFile': 'OldSoundE', 'Volume': 100}]

########################

for iPriority in range(1,6):
iPriorityListTask: [{'Zone': ['B'], 'Priority': 1, 'SoundFile': 'BrettSinging', 'Volume': 100}, {'Zone': ['A'], 'Priority': 1, 'SoundFile': 'DeebanSinging', 'Volume': 100}, {'Zone': ['G'], 'Priority': 1, 'SoundFile': 'Sound6', 'Volume': 100}]
iPriority: 1 Task= {'Zone': ['B'], 'Priority': 1, 'SoundFile': 'BrettSinging', 'Volume': 100}
#########ZONEMATCHFOUND:iPriority: 1
Task= {'Zone': ['B'], 'Priority': 1, 'SoundFile': 'BrettSinging', 'Volume': 100}
ActiveThread= {'Zone': ['B'], 'Priority': 5, 'SoundFile': 'OldSoundB', 'Volume': 100}
#######################
->> {'Zone': ['B'], 'Priority': 5, 'SoundFile': 'OldSoundB', 'Volume': 100}
####OVERRIDE to####
->> {'Zone': ['B'], 'Priority': 1, 'SoundFile': 'BrettSinging', 'Volume': 100}
iPriority: 1 Task= {'Zone': ['A'], 'Priority': 1, 'SoundFile': 'DeebanSinging', 'Volume': 100}
#########ZONEMATCHFOUND:iPriority: 1
Task= {'Zone': ['A'], 'Priority': 1, 'SoundFile': 'DeebanSinging', 'Volume': 100}
ActiveThread= {'Zone': ['A'], 'Priority': 5, 'SoundFile': 'OldSoundA', 'Volume': 100}
#######################
->> {'Zone': ['A'], 'Priority': 5, 'SoundFile': 'OldSoundA', 'Volume': 100}
####OVERRIDE to####
->> {'Zone': ['A'], 'Priority': 1, 'SoundFile': 'DeebanSinging', 'Volume': 100}
iPriority: 1 Task= {'Zone': ['G'], 'Priority': 1, 'SoundFile': 'Sound6', 'Volume': 100}
iPriorityListTask: [{'Zone': ['F'], 'Priority': 4, 'SoundFile': 'Sound5', 'Volume': 100}, {'Zone': ['E'], 'Priority': 4, 'SoundFile': 'Sound7', 'Volume': 100}]
iPriority: 4 Task= {'Zone': ['F'], 'Priority': 4, 'SoundFile': 'Sound5', 'Volume': 100}
iPriority: 4 Task= {'Zone': ['E'], 'Priority': 4, 'SoundFile': 'Sound7', 'Volume': 100}
#########ZONEMATCHFOUND:iPriority: 4
Task= {'Zone': ['E'], 'Priority': 4, 'SoundFile': 'Sound7', 'Volume': 100}
ActiveThread= {'Zone': ['E'], 'Priority': 5, 'SoundFile': 'OldSoundE', 'Volume': 100}
#######################
->> {'Zone': ['E'], 'Priority': 5, 'SoundFile': 'OldSoundE', 'Volume': 100}
####OVERRIDE to####
->> {'Zone': ['E'], 'Priority': 4, 'SoundFile': 'Sound7', 'Volume': 100}
iPriorityListTask: [{'Zone': ['B'], 'Priority': 5, 'SoundFile': 'DanielPlayingPiano', 'Volume': 100}, {'Zone': ['E'], 'Priority': 5, 'SoundFile': 'Sound8', 'Volume': 100}]
iPriority: 5 Task= {'Zone': ['B'], 'Priority': 5, 'SoundFile': 'DanielPlayingPiano', 'Volume': 100}
#########ZONEMATCHFOUND:iPriority: 5
Task= {'Zone': ['B'], 'Priority': 5, 'SoundFile': 'DanielPlayingPiano', 'Volume': 100}
ActiveThread= {'Zone': ['B'], 'Priority': 5, 'SoundFile': 'OldSoundB', 'Volume': 100}
#######################
iPriority: 5 Task= {'Zone': ['E'], 'Priority': 5, 'SoundFile': 'Sound8', 'Volume': 100}
#########ZONEMATCHFOUND:iPriority: 5
Task= {'Zone': ['E'], 'Priority': 5, 'SoundFile': 'Sound8', 'Volume': 100}
ActiveThread= {'Zone': ['E'], 'Priority': 4, 'SoundFile': 'Sound7', 'Volume': 100}
#######################

DictListPriorityQSorting########################
[{'Zone': ['G'], 'Priority': 1, 'SoundFile': 'Sound6', 'Volume': 100}]
[]
[{'Zone': ['A'], 'Priority': 3, 'SoundFile': 'AlexPlayingGuitar', 'Volume': 100}]
[{'Zone': ['F'], 'Priority': 4, 'SoundFile': 'Sound5', 'Volume': 100}]
[{'Zone': ['B'], 'Priority': 5, 'SoundFile': 'DanielPlayingPiano', 'Volume': 100}, {'Zone': ['E'], 'Priority': 5, 'SoundFile': 'Sound8', 'Volume': 100}]
################################################

ListActiveThreads#######
[{'Zone': ['A'], 'Priority': 5, 'SoundFile': 'OldSoundA', 'Volume': 100}, {'Zone': ['B'], 'Priority': 5, 'SoundFile': 'OldSoundB', 'Volume': 100}, {'Zone': ['E'], 'Priority': 4, 'SoundFile': 'Sound7', 'Volume': 100}]

########################

错误

复制错误

list.copy()没有创建未绑定列表,但copy.deepcopy(list)创建了。

循环React

for循环跳过了旧索引1,因为它变成了索引0,并移动到旧索引2(新索引1)。

  • 不要删除列表中任何可通过循环迭代的元素(但可以替换所有列表中的元素)*

for 循环演示

旧列表(在索引0上运行的循环):

  1. BrettSinging
  2. DeebanSinging
  3. Sound6
    新列表(在索引1上运行的循环):
  4. DeebanSinging(索引0,循环跳过它)
  5. Sound6

相关问题