CS50 DNA:使用 next() 选择 STR
CS50 DNA: Selecting the STRs with next()
我正在研究问题集 6:DNA。我的做法是将不同类型的STR序列保存为“all_sequences”,然后在“all_sequences”中找到每个序列的最大重复次数。
我的问题是:为什么 next() 确保我只会 select csv 的第一行?我知道 [1:] 是删除名称列,但是 next() 如何确保我只 select 第一行?
f = sys.argv[1] # name of CSV file
t = sys.argv[2] # name of text file with dna sequence
# Open the CSV file and read its contents into memory.
with open(f, "r") as database:
index = csv.reader(database)
all_sequences = next(index)[1:]
# Open the DNA sequence and read its contents into memory.
with open(t, "r") as dnaseq:
s = dnaseq.read()
actual = [maxrepeats(s, seq) for seq in all_sequences]
print_match(index, actual)
在您的示例中,index
是一个 csv.reader
object,它是一个迭代器。 next(index)
产生迭代器的下一个元素(显然是一个列表)。然后将列表切片以省略第一个值。
很奇怪 next 只用了一次,因为这只是简单地产生了 index
迭代器的第一行。当更频繁地调用 next 时,它开始有意义。
我正在研究问题集 6:DNA。我的做法是将不同类型的STR序列保存为“all_sequences”,然后在“all_sequences”中找到每个序列的最大重复次数。
我的问题是:为什么 next() 确保我只会 select csv 的第一行?我知道 [1:] 是删除名称列,但是 next() 如何确保我只 select 第一行?
f = sys.argv[1] # name of CSV file
t = sys.argv[2] # name of text file with dna sequence
# Open the CSV file and read its contents into memory.
with open(f, "r") as database:
index = csv.reader(database)
all_sequences = next(index)[1:]
# Open the DNA sequence and read its contents into memory.
with open(t, "r") as dnaseq:
s = dnaseq.read()
actual = [maxrepeats(s, seq) for seq in all_sequences]
print_match(index, actual)
在您的示例中,index
是一个 csv.reader
object,它是一个迭代器。 next(index)
产生迭代器的下一个元素(显然是一个列表)。然后将列表切片以省略第一个值。
很奇怪 next 只用了一次,因为这只是简单地产生了 index
迭代器的第一行。当更频繁地调用 next 时,它开始有意义。