English 中文(简体)
Cross-list comprehension in-030
原标题:Cross-list comprehension in Python

请允许我说,我有两份插手清单:

a = [####/boo ,#####/baa ,#####/bee ,####/bii , ####/buu]

编号#为4位数的随机编号。 并且

b = [ boo , aaa , bii ]

我需要知道在名单上打字的条目是什么。 页: 1 我得以完成这项工作,并同时使用in的操作器,对座标进行检查时,目前条目为B。 但是,作为相对新鲜的y子,我几乎是正面的,这并不是书写它的最老或最容易的方法。 因此,是否存在着减少我解决办法的这种区别?

最佳回答

The following code gives you an array with the indexes of a where the part after the slash is an element from b.

a_sep = [x.split( / )[1] for x in a]
idxs = [i for i, x in enumerate(a_sep) if x in b]

To improve performance, make b a set instead of a list.

解散:

>>> a = [ ####/boo ,  ####/baa ,  ####/bee ,  ####/bii ,  ####/buu ]
>>> b = [ boo ,  aaa ,  bii ]
>>> a_sep = [x.split( / )[1] for x in a]
>>> idxs = [i for i, x in enumerate(a_sep) if x in b]
>>> idxs
[0, 3]
>>> [a[i] for i in idxs]
[ ####/boo ,  ####/bii ]

如果你希望直接获得这些要素,而不是指数:

>>> a = [ ####/boo ,  ####/baa ,  ####/bee ,  ####/bii ,  ####/buu ]
>>> b = [ boo ,  aaa ,  bii ]
>>> [x for x in a if x.split( / )[1] in b]
[ ####/boo ,  ####/bii ]
问题回答

ThiefMaster的回答是好的,地雷将非常相似,但如果你不了解指数,你可以做一个捷径:

>>> a = [ ####/boo ,  ####/baa ,  ####/bee ,  ####/bii ,  ####/buu ]
>>> b = [ boo ,  aaa ,  bii ]
>>> [x for x in a if x.split( / )[1] in b]
[ ####/boo ,  ####/bii ]

Again, if b is a set, that will improve performance for large numbers of elements.

import random
a=[str(random.randint(1000,9999))+ / +e for e in [ boo , baa , bee , bii , buu ]]

b = [ boo ,  aaa ,  bii ]

c=[x.split( / )[-1] for x in a if x.split( / )[-1] in b]

print c

prints:

[ boo ,  bii ]

或者,如果你想要全部入境:

print [x for x in a if x.split( / )[-1] in b]

prints:

[ 3768/boo ,  9110/bii ]
>>> [i for i in a for j in b if j in i]
[ ####/boo ,  ####/bii ]

我们应该做你们想要的、leg的和老的。

正如其他答复所示,你可以利用既定行动来更快地做到这一点。 下面是这样做的一种方式:

>>> a_dict = dict((item.split( / )[1], item) for item in a)
>>> common = set(a_dict) & set(b)
>>> [a_dict[i] for i in common]
[ ####/boo ,  ####/bii ]




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签