使用For循环查找列表中的第N个元素

时间:2015-07-12 22:26:27

标签: python

想象一下,我想在另一个列表中找到第2,第3和第4个元素。

position = [2,3,4]

Sample_List = ['a','b','c','d','e']

循环会返回结果:

['c','d','e']

3 个答案:

答案 0 :(得分:5)

简单:

the_elements = [Sample_List[i] for i in position]

答案 1 :(得分:1)

您也可以使用

elements = map(lambda k:Sample_List[k], position)

在python3上,您需要将其转换为列表

elements = list(map(lambda k:Sample_List[k], position))

答案 2 :(得分:1)

使用operator.itemgetter的另一种方法:

Caused by: org.apache.activemq.transport.InactivityIOException: Cannot send, channel has already failed: tcp://ip address:8161
    at org.apache.activemq.transport.AbstractInactivityMonitor.doOnewaySend(AbstractInactivityMonitor.java:297)
    at org.apache.activemq.transport.AbstractInactivityMonitor.oneway(AbstractInactivityMonitor.java:286)
    at org.apache.activemq.transport.TransportFilter.oneway(TransportFilter.java:85)
    at org.apache.activemq.transport.WireFormatNegotiator.oneway(WireFormatNegotiator.java:104)
    at org.apache.activemq.transport.MutexTransport.oneway(MutexTransport.java:68)
    at org.apache.activemq.transport.ResponseCorrelator.asyncRequest(ResponseCorrelator.java:81)
    at org.apache.activemq.transport.ResponseCorrelator.request(ResponseCorrelator.java:86)
    at org.apache.activemq.ActiveMQConnection.syncSendPacket(ActiveMQConnection.java:1409)
    ... 21 more