如何在循环中使用数字列表作为索引?

huangapple go评论60阅读模式
英文:

How do I use a list of numbers as indexes in a loop?

问题

以下是翻译好的部分:

我有一个包含多个单行字母(在这种情况下是DNA序列)的文件。

list = ["AATCGATCGG", "GACTCGGTTA", "AGGGGTACTA"]

我有一些感兴趣的索引,我想要查看每行的这些位置,我已经将它们放在一个列表中。

idx = [7, 8, 9]

我想让代码遍历列表中的每一行,并查看每行中idx中的位置。如何获得一个数字列表,以便在循环中作为单独的索引值进行读取?
编辑:我希望输出看起来类似于以下内容:

CGG
TTA
CTA

当我使用下面的代码查看特定索引时,会得到该位置的输出。

for line in list: print(line[7])

但是,当我尝试在特定索引的位置使用索引列表时,使用下面代码的变种:

for line in list: print(line[idx])

我会收到指示列表项未被单独读取的错误。

"TypeError: byte indices must be integers or slices, not list"
"TypeError: string indices must be integers"

我如何在for循环内遍历列表?

英文:

I have a file with multiple single-lines of letter (in this case, DNA sequences).

list = ["AATCGATCGG","GACTCGGTTA","AGGGGTACTA"]

I have indexes of interest that I would like to look at for each line, which I have put in a list.

idx = [7,8,9]

I would like to code to go through each line of list and look at each of the positions in idx within the line. How can I get a list of numbers to be read as individual index values within a loop?
Edit: I would like the output to look similar to what is below:

CGG
TTA
CTA

When I look at a specific index, using the code below, I get output for that position.

for line in list:
print(line[7])

However, when I try to use the list of indexes in place of the specific index, using a variation of the code below:

for line in list:
print(line[idx])

I get errors indicating that the the list items are not being read individually.

"TypeError: byte indices must be integers or slices, not list"
"TypeError: string indices must be integers"

How do I get a list to be iterated through within a for loop?

答案1

得分: 2

我不确定您希望的输出格式是什么。下面的代码显示了两种替代方法。请注意,list 在Python中具有特定的用途,最好不要用作名称。

my_list = ["AATCGATCGG", "GACTCGGTTA", "AGGGGTACTA"]

idx = [7, 8, 9]

for line in my_list:
    my_extract = 
for i in idx]
my_string = "".join(my_extract) print(my_extract) print(my_string)

输出结果如下:

['C', 'G', 'G']
CGG
['T', 'T', 'A']
TTA
['C', 'T', 'A']
CTA
英文:

I am not sure how you want the output formatting. The code below shows two alternatives. Note list has a specific use in Python so is best not used as a name.

my_list = ["AATCGATCGG","GACTCGGTTA","AGGGGTACTA"]

idx = [7,8,9]

for line in my_list:
    my_extract = 
for i in idx] my_string = "".join(my_extract) print(my_extract) print(my_string)

gives:

['C', 'G', 'G']
CGG
['T', 'T', 'A']
TTA
['C', 'T', 'A']
CTA

答案2

得分: 0

你可以使用普通的for循环结合list comprehension

dna_lists = ["AATCGATCGG","GACTCGGTTA","AGGGGTACTA"]
for line in dna_lists:
    print("".join(
for i in idx]))

以下的列表推导形式使用了序列中的索引字母。

for i in idx]

"".join(list)形式用于从列表成员创建字符串。

英文:

You can use list comprehension with a regular for loop.

dna_lists = ["AATCGATCGG","GACTCGGTTA","AGGGGTACTA"]
for line in dna_lists:
    print("".join(
for i in idx])

The following list comprehension forms a new list using the indexed letters in the sequence.

for i in idx]

The "".join(list) form is used for creating a string from the list members.

答案3

得分: -1

为确保列表中的项目被逐个阅读,您需要执行以下操作:

for i in list:

或者

list = []
b = list[1:2]
c = list[2:3]
d = list[3:4]
e = list[4:5]
f = list[5:6]
g = list[6:7]

print(b, c, d, e, f, g)
英文:

To make sure the items in the list are being read individually you need to do :

for i in list :

or

list = [ ]
b = list[1:2]
c = list[2:3]
d = list[3:4]
e = list[4:5]
f = list[5:6]
g = list[6:7]

print(b,c,d,e,f,g)

huangapple
  • 本文由 发表于 2023年6月22日 05:41:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76527343.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定