【问题标题】:Python Slicing ListPython 切片列表
【发布时间】:2013-07-02 20:34:39
【问题描述】:

我有一个二维列表

a= [['Turn', 'the', 'light', 'out', 'now'], ['Now', ',', 'I', 'll', 'take', 'you', 'by', 'the', 'hand'], ['Hand', 'you', 'anoth', 'drink'], ['Drink', 'it', 'if', 'you', 'can'], ['Can', 'you', 'spend', 'a', 'littl', 'time'], ['Time', 'is', 'slip', 'away'], ['Away', 'from', 'us', ',', 'stay'], ['Stay', 'with', 'me', 'I', 'can', 'make'], ['Make', 'you', 'glad', 'you', 'came']]

有没有一种简单的方法可以从此列表中获取一系列值?

如果我写:

print a[0][0] 

我得到“转身”,但有没有办法从列表中切出多个事物并创建一个新列表?如果我有一个值 i=0 和 n=2,我希望创建一个从 a=[i][n] 开始的新列表,并在该索引的任一侧创建一个包含列表项的新列表。

感谢您的帮助

【问题讨论】:

  • 对不起,你能举一个你想要的输出的例子吗?如果是简单的切片,那就很明显了......

标签: python list 2d slice


【解决方案1】:

也许你的意思

print a[0][0:2]

print [d[0] for d in a]

【讨论】:

    猜你喜欢
    • 2017-06-24
    • 2015-02-19
    • 2014-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-16
    • 2014-11-02
    • 1970-01-01
    相关资源
    最近更新 更多