为了账号安全,请及时绑定邮箱和手机立即绑定

如何操作二维列表

如何操作二维列表

HUX布斯 2022-04-23 21:53:29
我需要帮助为我的计算 GCSE NEA(未经检查的评估)处理二维列表,我需要做的是在一行(一首单曲)上显示每首歌曲标题的第一个字母以及艺术家姓名,例如 AC/ DC 'Back in Black' 将是 'B i B' AC/DC我通常坚持如何操作数组以显示我需要的内容,并且我使用了几个网站,例如 snakify 和其他一些网站到目前为止,这是我的程序:stageone=[['another one bites the dust','Queen',           'smoke on the water','Deep Purple',           'Stairway to heaven','Led Zeppelin',           'Carry on wayward son','Kansas',           'Don't stop believin','Journey']]mainmenu=input("Welcome to the game please select an option-s for start the game,ts for top scores,ys for your scores,n for new game")if mainmenu=='s':  play=input("do you want to continue you last game-c or do you want to start   a new game-n?")  if play=='s':    difficulty=input("What difficulty do you want to play? you can choose     from; easy,medium,difficult and legend")    if difficulty=='easy':       print("The easy mode of this game contains five stages,each with five        questions")       sure=input("Are you sure you want to play this gamemode?")       if sure=='y':       print(stageone)我需要这首歌是独立的,而不是整个阵列。每首歌都需要每个单词的第一个字母,而不是整个单词。我不知道如何编写程序的这一部分,我们将不胜感激。歌曲艺人名但必须是完整的而不是单一的,首字母如歌名
查看完整描述

1 回答

?
哈士奇WWW

TA贡献1799条经验 获得超6个赞

当您定义 stageone 时,使其成为如下列表的列表:


stageone = [[title, band], [title, band], [title, band]]


然后,而不是print(stageone)在最后一行做:


for entry in stageone:

    shortTitle = ' '.join([word[0] for word in entry[0].split(' ')])

    print(shortTitle, entry[1])

更新:要一次获得一个提示,您需要一些方法来选择一个条目stage one(对于游戏,我想这可能是一个随机索引)。然后你只需删除for我之前给出的循环并像这样使用你选择的条目


i = #some code to select an entry in stageone

entry = stageone[i]

shortTitle = ' '.join([word[0] for word in entry[0].split(' ')])

print(shortTitle, entry[1])

请注意,有很多方法可以使它更紧凑,但最初的问题让我认为更冗长的答案比最小的解决方案更好。例如,@calestinifor在我的第一个回复中评论了一个单行来替换循环,


res = [[' '.join([x[0] for x in i[0].split()]), i[1]] for i in stageone ]

这是一个很好的解决方案 -res将是一个列表[[song hint, band name], [song hint, band name]],然后您可以根据需要打印提示。我没有改变我原来的答案,因为我不喜欢在一行中使用两个列表推导(我很难阅读代码)。


查看完整回答
反对 回复 2022-04-23
  • 1 回答
  • 0 关注
  • 83 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信