使用多个循环引用列表元素
Working With Multiple Loops Referencing Elements Of Lists
我对我遇到的错误做了一些示例。我实际上需要代码做的是为每个键选择两个任务并将其打印在键旁边。当我 运行 代码时,起初它似乎可以工作,但后来却没有。谁能帮忙。这是输出和代码示例
实际输出
1 + 1 head
1 + 1 head
2 + 2 head
1 + 1 head
2 + 2 leg
1 + 1 head
2 + 2 nose
1 + 1 head
2 + 2 tail
1 + 1 head
list index out of range
pop from empty list
list index out of range
list index out of range
list index out of range
list index out of range
list index out of range
list index out of range
预期输出
1 + 1 head
2 + 2 head
3 + 3 leg
4 + 4 leg
5 + 5 nose
6 + 6 nose
7 + 7 tail
8 + 8 tail
代码
from concurrent.futures import ThreadPoolExecutor, as_completed
keys = ['head','leg','nose','tail']
quest = ['1 + 1','2 + 2','3 + 3','4 + 4','5 + 5','6 + 6','7 + 7' , '8 + 8']
count = []
def solve(i):
try:
for h in quest():
if len(count) != 2:
q = keys[0]
print(i,q)
count.append(q.strip())
except Exception as e:
print(e)
t = 2
def run():
thread = []
with ThreadPoolExecutor(max_workers=t) as exe:
for i in quest:
threads.append(exe.submit(solve,i))
run
>>> keys = ['head', 'leg', 'nose', 'tail']
>>> quest = ['1 + 1', '2 + 2', '3 + 3', '4 + 4', '5 + 5', '6 + 6', '7 + 7', '8 + 8']
>>> doubled_keys= sum([[*t] for t in zip(keys, keys)], [])
>>> pairs = [*zip(quest, doubled_keys)]
>>> for q, k in pairs:
... print(q, k)
...
1 + 1 head
2 + 2 head
3 + 3 leg
4 + 4 leg
5 + 5 nose
6 + 6 nose
7 + 7 tail
8 + 8 tail
我对我遇到的错误做了一些示例。我实际上需要代码做的是为每个键选择两个任务并将其打印在键旁边。当我 运行 代码时,起初它似乎可以工作,但后来却没有。谁能帮忙。这是输出和代码示例
实际输出
1 + 1 head
1 + 1 head
2 + 2 head
1 + 1 head
2 + 2 leg
1 + 1 head
2 + 2 nose
1 + 1 head
2 + 2 tail
1 + 1 head
list index out of range
pop from empty list
list index out of range
list index out of range
list index out of range
list index out of range
list index out of range
list index out of range
预期输出
1 + 1 head
2 + 2 head
3 + 3 leg
4 + 4 leg
5 + 5 nose
6 + 6 nose
7 + 7 tail
8 + 8 tail
代码
from concurrent.futures import ThreadPoolExecutor, as_completed
keys = ['head','leg','nose','tail']
quest = ['1 + 1','2 + 2','3 + 3','4 + 4','5 + 5','6 + 6','7 + 7' , '8 + 8']
count = []
def solve(i):
try:
for h in quest():
if len(count) != 2:
q = keys[0]
print(i,q)
count.append(q.strip())
except Exception as e:
print(e)
t = 2
def run():
thread = []
with ThreadPoolExecutor(max_workers=t) as exe:
for i in quest:
threads.append(exe.submit(solve,i))
run
>>> keys = ['head', 'leg', 'nose', 'tail']
>>> quest = ['1 + 1', '2 + 2', '3 + 3', '4 + 4', '5 + 5', '6 + 6', '7 + 7', '8 + 8']
>>> doubled_keys= sum([[*t] for t in zip(keys, keys)], [])
>>> pairs = [*zip(quest, doubled_keys)]
>>> for q, k in pairs:
... print(q, k)
...
1 + 1 head
2 + 2 head
3 + 3 leg
4 + 4 leg
5 + 5 nose
6 + 6 nose
7 + 7 tail
8 + 8 tail