如何在列表中循环多次?

How to loop several times within a list?

我的代码如下所示:

import random
import numpy as np
from operator import itemgetter
import sys

equal_to = {"a":"u_plus", "b":"u_minus", "c":"v_plus", "d":"v_minus"} #bell state

p = 8 #length of generated binary

key1 = [] #list of generated binary 
for i in range(p): 
    temp = random.randint(0,1)
    key1.append(temp) 

tmplist2 = [] #list of random sample_letters
for i in range(p):
    while True:
        attempt = str(random.choice(list(equal_to)))
        tmplist2.append(attempt)

        if attempt == 'b':
            break

#evaluate result of alice binary and bell state
def eva(alice, bell):
    if alice == 1:
        if bell == 'a' or bell == 'b':
            return 1
        elif bell == 'c' or bell == 'd':
            return 0
    elif alice == 0:
        if bell == 'c' or bell == 'd':
            return 1
        elif bell == 'a' or bell == 'b':
            return 0


for_bob = [] #list of generated binary and bell state through logic gate

for k in key1:
    for t in tmplist2:
        e = eva(k, t)
        for_bob.append(e)

#tr = [[eva(k,t) for t in tmplist2] for k in key1] #list comprehension split the key properly
print("generated random binary strings:", key1)
print("generated bell states:", tmplist2)
print("encrypted strings:", for_bob)

它打印出这样的东西:

generated random binary strings:
  [0, 0, 0, 0, 0, 1, 1, 0]
generated bell states:
  ['b', 'c', 'b', 'a', 'a', 'c', 'b', 'b', 'd', 'd', 'd', 'c', 'c', 'b', 'a', 'd', 'd', 'b', 'c', 'c', 'd', 'c', 'd', 'b', 'd', 'c', 'a', 'd', 'c', 'd', 'c', 'c', 'b']
encrypted strings:
  [0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0]

我正在寻找一种在 tmplist2 内循环的方法,如果字符串 'b' 出来,则无需从头开始。

循环应该按如下方式运行: key1[0]应该用'b'求值,返回0。既然'b'出来了,外层循环应该继续下一个元素 key1[1],它应该用 'c''b' 求值,返回 1 0。再次因为 'b' 出来了,外循环将继续到下一个元素 key[2] 并用 'a'[=41= 评估自己]、'a''c''b' ,返回 0 0 1 0。这应该一直持续到 tmplist2 完成。

有人解决了这个问题吗?我正在寻找如何在循环中循环而不是在给出特定条件时返回开头的解决方案。在这种情况下 'b'.

n.b.: 这个程序是用来模拟量子隐形传态的。

主要数据

for_bob = []
key1 = [0, 0, 0, 0, 0, 1, 1, 0]
tmplist2 = ['b', 'c', 'b', 'a', 'a', 'c', 'b', 'b', 'd', 'd', 'd', 'c', 'c', 'b', 'a', 'd', 'd', 'b', 'c', 'c', 'd', 'c', 'd', 'b', 'd', 'c', 'a', 'd', 'c', 'd', 'c', 'c', 'b']

方法一

_tmp = tmplist2[:]
for k in key1:
    while _tmp:
        if _tmp[:1] != ['b']:
            for_bob.append(eva(k, *_tmp[:1]))
            _tmp = _tmp[1:]
        else:
            for_bob.append(eva(k, *_tmp[:1]))
            _tmp = _tmp[1:]
            break

方法二:

_tmp = iter(tmplist2)

for i in key1:
    for j in iter(lambda: next(_tmp), None):
        if j != 'b':
            for_bob.append(eva(i, j))
        else:
            for_bob.append(eva(i, j))
            break
generated random binary strings: [0, 0, 0, 0, 0, 1, 1, 0]
generated bell states: ['b', 'c', 'b', 'a', 'a', 'c', 'b', 'b', 'd', 'd', 'd', 'c', 'c', 'b', 'a', 'd', 'd', 'b', 'c', 'c', 'd', 'c', 'd', 'b', 'd', 'c', 'a', 'd', 'c', 'd', 'c', 'c', 'b']
encrypted strings: [0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0]