索引列表列表 [英] Indexing list of lists

查看:82
本文介绍了索引列表列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

之前可能有人问过,但我找不到它。如果我有

a矩形列表列表,比如说,l = [[1,10],[2,20],[3,30]],那么是

那里一个方便的语法,用于检索每个子列表的第i项?

我知道[i [0] for i in l]但是我希望有更多的东西

喜欢l [; 0]。


Hilde

This may have been asked before but I can''t find it. If I have
a rectangular list of lists, say, l = [[1,10],[2,20],[3,30]], is
there a handy syntax for retrieving the ith item of every sublist?
I know about [i[0] for i in l] but I was hoping for something more
like l[;0].

Hilde

推荐答案

嗨************* @ yahoo.de (Hilde Roth)写道:
hi*************@yahoo.de (Hilde Roth) writes:
这可能是之前被问过但我找不到。如果我有一个矩形列表列表,比如l = [[1,10],[2,20],[3,30]],那么有一个方便的语法来检索第i个每个子列表中的项目?
我知道[i [0] for i in l]但是我希望有更多的东西像l [; 0]。
This may have been asked before but I can''t find it. If I have
a rectangular list of lists, say, l = [[1,10],[2,20],[3,30]], is
there a handy syntax for retrieving the ith item of every sublist?
I know about [i[0] for i in l] but I was hoping for something more
like l[;0].




如果你需要那么多东西,看看Numeric(或它的

替换,numarray),或者标准库的数组

模块。

John



If you need that kind of thing a lot, look at Numeric (or its
replacement, numarray), or perhaps the standard library''s array
module.
John


文章< 95 ***************** *********@posting.google.com>,
hi *************@yahoo.de (希尔德罗斯)写道:
In article <95**************************@posting.google.com >,
hi*************@yahoo.de (Hilde Roth) wrote:
这可能是之前被问到但我可以''找到它。如果我有一个矩形列表列表,比如l = [[1,10],[2,20],[3,30]],那么有一个方便的语法来检索第i个每个子列表中的项目?
我知道[i [0] for i in l]但是我希望有更多的东西像l [; 0]。
This may have been asked before but I can''t find it. If I have
a rectangular list of lists, say, l = [[1,10],[2,20],[3,30]], is
there a handy syntax for retrieving the ith item of every sublist?
I know about [i[0] for i in l] but I was hoping for something more
like l[;0].


l = [[1,10],[2,20],[3,30]]
zip(* l)[0]
l = [[1,10],[2,20],[3,30]]
zip(*l)[0]



(1,2,3)


-

David Eppstein http://www.ics.uci.edu/ ~eppstein /

大学加州,欧文,信息学院和计算机科学


(1, 2, 3)

--
David Eppstein http://www.ics.uci.edu/~eppstein/
Univ. of California, Irvine, School of Information & Computer Science


Hilde Roth写道:
Hilde Roth wrote:
这可能是之前被问过但我找不到。如果我有一个矩形列表列表,比如l = [[1,10],[2,20],[3,30]],那么有一个方便的语法来检索第i个每个子列表中的项目?
我知道[i [0] for i in l]但是我希望有更多的东西像l [; 0]。
This may have been asked before but I can''t find it. If I have
a rectangular list of lists, say, l = [[1,10],[2,20],[3,30]], is
there a handy syntax for retrieving the ith item of every sublist?
I know about [i[0] for i in l] but I was hoping for something more
like l[;0].




如果效率不是问题和/或你需要

[列表中项目的[项目[索引]]一次多个索引,你可以

do:



If efficiency is not an issue and/or you need
[item[index] for item in theList] for more than one index at a time, you can
do:

s = [[1,2],[3,4]]
t = zip(* s)
t
[(1,3),(2,4)] t [1]
(2,4)
s = [[1,2],[3,4]]
t = zip(*s)
t [(1, 3), (2, 4)] t[1] (2, 4)




这会创建矩阵的转置(?)副本。如果您只需要

对条目的读取权限,那么创建tupples而不是内部列表的副作用应该没有坏处。


Peter



This creates a transposed (?) copy of the "matrix". The side effect of
creating tupples instead of inner lists should do no harm if you need only
read access to the entries.

Peter


这篇关于索引列表列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆