Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何操作二维列表_Python - Fatal编程技术网

Python 如何操作二维列表

Python 如何操作二维列表,python,Python,我需要帮助在2d列表中进行计算GCSE NEAnon考试评估,我需要做的是在一行上显示每首歌曲标题的第一个字母一首歌曲连同艺术家姓名,例如AC/DC“Back in Black”将是“B I B”AC/DC 我通常被困在如何操纵数组以显示我需要的内容上,我使用过几个网站,如snakify和其他一些网站 这是我目前的计划: stageone=[['another one bites the dust','Queen', 'smoke on the water','Deep P

我需要帮助在2d列表中进行计算GCSE NEAnon考试评估,我需要做的是在一行上显示每首歌曲标题的第一个字母一首歌曲连同艺术家姓名,例如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)

我需要这首歌是独立的,而不是整个阵列。每首歌需要每个单词的第一个字母,而不是整个单词。我不知道如何编写我程序的这一部分,非常感谢您的帮助。但是,歌曲艺术家名称必须是完整的,而不是像歌曲标题那样的单个首字母

定义stageone时,请将其列为如下列表:

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

然后,代替最后一行中的printstageone,执行以下操作:

for entry in stageone:
    shortTitle = ' '.join([word[0] for word in entry[0].split(' ')])
    print(shortTitle, entry[1])
更新:为了一次得到一个提示,你需要一些方法来选择游戏第一阶段的条目,我想这可能是一个随机索引。然后,您只需删除我之前给出的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])
请注意,有很多方法可以使这个问题更简洁,但最初的问题让我觉得更详细的答案比最小的解决方案更好。例如,@calestini在我的第一个响应中评论了一个一行程序来替换For循环

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

这是一个很好的解决方案-res将是一个类似[[歌曲提示,乐队名称],[歌曲提示,乐队名称]]的列表,然后您可以根据需要打印提示。我不会改变我的原始答案,因为我不喜欢在一行中使用两个列表理解。我很难阅读代码。

您的歌曲列表语法不正确。不能在由单引号括起的字符串中使用非转义单引号。如果您希望访问列表中的一首歌曲,请使用“不停止believin”而不是“不停止believin”,也可以使用索引stageone[0][1]。这将打印“另一个咬了灰尘”。不得不使用[0][1]因为你在列表中有一个列表。同样,既然歌曲要与艺术家结合,为什么不使用元组列表,比如[“另一个咬了灰尘”,“女王”,“水上的烟”,“深紫色”…等等]还有-从什么意义上说,你的列表是2-D?您似乎有一个无意义的二维列表,其中外部列表只包含一个列表。使用stageone=[['另一个咬了灰尘','Queen'],['水上的烟','Deep Purple'],…]或使用一行代码res=[''。在i[0]中为x加入[x[0],在i[0]中为x加入[x],在stageone中为i[1].@dshort您向我展示的代码行在我的代码范围内工作,非常有用,谢谢。但是在我的游戏中,你必须猜出歌曲的标题。使用您的代码,我如何一次只能显示一个?答案已更新,以反映@calestini和OP的评论