如何在python中以3组为一组访问列表? [英] How do you access a list in group of 3 in python?

查看:215
本文介绍了如何在python中以3组为一组访问列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

origCodon = ([orig[i: i + groupSize] for i in range(len(orig) + 1 - groupSize)])
patCodon = ([pat[i: i + groupSize] for i in range(len(pat) + 1 - groupSize)])
print (patCodon)
origCode = []
patCode = []
for p in patCodon:
    for d in dna:
         if d == p:
              x = dna[p]
              print (p)
              patCode.append(x)

上面的代码将两个列表分成三个列表,但是当我检查每个元素时,它将创建一个新的三个列表,每次都沿一个元素移动.

The code above takes two lists and splits them into groups of three, but when I go to check each individual element, it makes a new list of three, moving along one element each time.

即这是一个清单:

['AAC', 'ACT', 'CTG', 'TGC', 'GCA', 'CAG', 'AGC', 'GCT', 'CTC', 'TCA']

但这是它要检查的元素:

But these are the elements it checks:

AAC
ACT
CTG
TGC
GCA
CAG
AGC
GCT
CTC
TCA

如何做到这一点,以便每三人一组被检查,然后转到下一个?

How do I make it so that each group of three is checked and then it moves on to the next?

我的列表分为三个组(成为列表中的项目),我想检查每个项目中对应的氨基酸(在字典中),但是程序会不断创建新列表,例如用户输入AAATTT,然后程序检查:

My list is split into groups of three (becoming items in the list), I want to check each of those items for their corresponding amino acid (in a dictionary), but the program keeps making new lists, e.g. the user enters AAATTT, then the program checks:

AAA
AAT
ATT
TTT

不仅仅是AAA和TTT

rather than just AAA and TTT

推荐答案

执行此操作的方法有两种:切片或共享迭代器.

There are two ways to do this: slices, or a shared iterator.

其他答案显示了slice方法-如果您只知道/记住step = 3到range,我想您可能已经对了:

The other answers show the slice method—which I think you could have gotten correct, if you just knew/remembered the step=3 to range:

[lst[i:i+3] for i in range(0, len(lst), 3)]

此方法的唯一主要缺点是,它仅适用于列表或其他序列,而不适用于一般的可迭代方法.在您当前的代码中,这无关紧要,因为您想要在上调用它的东西是一个列表.

The only major downside of this method is that it only works on a list or other sequence, not a general iterable. In your current code, this doesn't matter, because the thing you want to call it on is a list.

但是值得一提的是:

i = iter(list)
zip(i, i, i)

iter 只是询问序列或其他可迭代项在其内容上进行单遍迭代器.

iter just asks a sequence or other iterable for a single-pass iterator over its contents.

然后 zip 像往常一样按步调前进

Then zip just advances them in lockstep, as usual.

由于zip的所有三个参数都是对完全相同的迭代器的引用,因此当它尝试推进一个迭代器时,它将推进所有这些迭代器. (这就是为什么我们不能只做zip(iter(i), iter(i), iter(i))的原因,那么您将拥有三个单独的迭代器.)

Because all three of zip's arguments are references to the exact same iterator, when it tries to advance one, it advances all of them. (This is why we can't just do zip(iter(i), iter(i), iter(i))—then you'd have three separate iterators.)

但是,如果要按2或5分组怎么办?为zip(i, i)zip(i, i, i, i, i)等编写单独的函数并不是很好.

But what if you want to group by 2, or 5? Writing separate functions for zip(i, i) and zip(i, i, i, i, i) and so on wouldn't be very nice.

如果我们有一个迭代器的n引用序列,则可以使用*args语法,如本教程中

If we had a sequence of n references of the iterator, we could use *args syntax, as described in the tutorial under Unpacking Argument Lists, to just call zip(*sequence).

通过使用 *重复运算符:[i]*n. (如果您不明白为什么最后用n引用一个迭代器而不是n单独的迭代器,请阅读

And we can easily get such a sequence by using the * repetition operator: [i]*n. (If you don't understand why that ends up with n references to one iterator, instead of n separate iterators, read the Python FAQ's entry on How do I create a multidimensional list?.)

您可以将所有内容放到一个直线上:

And you can put that all together into a one-liner:

zip(*[iter(lst)]*n)


如果有剩余的部分组,则会将其删除,因为这是zip的作用.因此,如果您希望在这种情况下做一些不同的事情,只需将zip替换为其他函数即可,例如,用空格填充部分组,只需:


If there's a partial group left over, this will drop it, because that's what zip does. So if you'd rather do something different in that case, you can just replace zip with a different function—e.g., to pad the partial group with spaces, just:

itertools.zip_longest(*[iter(lst)]*3, fillvalue=' ')

文档中的 itertools食谱具有一个函数调用程序grouper为您执行此操作.

The itertools recipes in the docs have a function caller grouper which does this for you.

这篇关于如何在python中以3组为一组访问列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆