简体   繁体   English

python中带有数组/列表的基本循环概念

[英]Basic loop concept with array/list in python

i know this will be a very novice question to ask here but i kind of blank out and need this to get resolved immediately. 我知道这将是一个非常新手的问题,但我有点空白,需要立即解决。

I want to change values from S -> B and so on as mention in below code. 我想从S-> B等更改值,如以下代码中所述。 For that i can create array or list "A = [S, B, N]" or whatever required. 为此,我可以创建数组或列表“ A = [S,B,N]”或任何需要的列表。 But need to traverse in this manner. 但是需要以这种方式遍历。

S --> B 
B --> S
S --> N
N --> B
B --> N
N --> S

I tried creating 2 lists A and B with same values [S, B, N] and traversing through both of them with 我尝试创建2个具有相同值[S,B,N]的列表A和B,并使用

i=0;i<length.A;i++
   j=0;j<length.B;j++

and

i=0;i<length.A;i++
   j=i;j<length.B;j++

But in scenario 1: S changed to B, then it tries changes from S -> N. Which is not possible. 但是在方案1中:S更改为B,然后尝试从S-> N进行更改。这是不可能的。 In Scenario 2: We hit 4 out of 6 but not all 6 required outputs. 在方案2中:我们在6中打了4个,但不是全部6个必需的输出。

I could've researched more but due to lack of time, i couldn't. 我可以做更多的研究,但是由于时间不足,我无法进行研究。

Also I've written the code in pseudo manner. 我也以伪方式编写了代码。

As I understand, you have a specific sequence of states: S -> B -> S -> N -> B -> N -> [begin again] 据我了解,您具有特定的状态序列:S-> B-> S-> N-> B-> N-> [重新开始]

This could be solved with a generator such as: 这可以通过生成器来解决,例如:

import itertools
state = itertools.cycle(['S', 'B', 'S', 'N', 'B', 'N'])

Subsequent calls of next(state) will yield the list in sequence, starting at the beginning when it reaches the end. 随后对next(state)的调用将按顺序产生列表,从列表到达末尾开始。 Example: 例:

>>> next(state)
'S'
>>> next(state)
'B'
>>> next(state)
'S'
>>> next(state)
'N'
>>> next(state)
'B'
>>> next(state)
'N'
>>> next(state)
'S'
>>> next(state)
'B'
>>> next(state)
'S'
>>> next(state)
'N'
>>> next(state)
'B'

I think this should answer your question, I know my list that changes the letters isn't exactly the same, but I think from here you should be able to implement it. 我认为这应该可以回答您的问题,我知道更改字母的列表并不完全相同,但是我认为从这里您应该可以实现它。

It changes each s to b, b to n and n to s. 它将每个s更改为b,将b更改为n,将n更改为s。

lettersToChange = ['s','b','n']

randomLetters = ['k','l','m','n','s','s','b','n','b','n','s','b','n','b','s']
for i in range(len(randomLetters)):
    if randomLetters[i] in lettersToChange:
        pos = lettersToChange.index(randomLetters[i])
        randomLetters[i] = lettersToChange[(pos+1)%3]

So before and after of the randomLetters list: 因此,在randomLetters列表之前和之后:

 ['k', 'l', 'm', 'n', 's', 's', 'b', 'n', 'b', 'n', 's', 'b', 'n', 'b', 's']
 ['k', 'l', 'm', 's', 'b', 'b', 'n', 's', 'n', 's', 'b', 'n', 's', 'n', 'b']

If i understand correctly, you must have an index, because you have to tell apart, which S or B or N should advance to it's next state. 如果我理解正确,则必须有一个索引,因为您必须分辨出哪个S或B或N应该前进到下一个状态。 Something like that: 像这样:

TRAVERSE_ORDER = "SBSNBN"
EXAMPLE_INPUT = ["S", "B", "N"]

def traverse_single_item(state, state_index=0):
    next_state = TRAVERSE_ORDER.find(state, state_index) + 1
    if next_state == len(TRAVERSE_ORDER):
        next_state = 0
    return (TRAVERSE_ORDER[next_state], next_state)

new_state = [traverse_single_item(*elem) for elem in EXAMPLE_INPUT]
print(new_state)

newer_state = [traverse_single_item(*elem) for elem in new_state]
print(newer_state)

newest_state = [traverse_single_item(*elem) for elem in newer_state]
print(newest_state)

If you call the function without an index, it assumes to be the first occurence in the traverse order. 如果您在不使用索引的情况下调用该函数,则该函数假定是遍历顺序中的第一个出现。 Please note the asterisk: *elem. 请注意星号:* elem。 It unpacks the tuple for the function call. 它为函数调用解压缩元组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM