英文:
An iterator that prints part of a string
问题
I want to create an iterator to run and print all the parts with the numbers, how do I access them?
我想创建一个迭代器来运行并打印所有带有数字的部分,如何访问它们?
I marked in black what I needed the narrator to run and print
我用黑色标记了我需要叙述者运行和打印的部分。
英文:
I want to create an iterator to run and print all the parts with the numbers, how do I access them?
I marked in black what I needed the narrator to run and print
答案1
得分: 1
你可以使用两个分割,一个在 -
处,另一个在 ,
处
notes = 'sol,250-mi,250-mi,500-fa,250-re,250-re,500-do,250-re,250-mi,250-fa,250'
numbers = [temp.split(',')1 for temp in notes.split('-')]
英文:
You can use two splits, one at -
and one at ,
notes = 'sol,250-mi,250-mi,500-fa,250-re,250-re,500-do,250-re,250-mi,250-fa,250'
numbers = [temp.split(',')[1] for temp in notes.split('-')]
答案2
得分: 0
你已经差不多完成了。在你的第一个split
命令之后,duration
是一个字符串列表。
然后,你遍历这些字符串,在每个字符串上调用split
,但是用,
代替-
。
英文:
You're pretty much there already. After your first split
command, duration
is a list of strings.
Then you iterate over those strings and on each of those you call split
again, but with ,
instead of -
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论