在Python列表中查找元素的有效方法?

发布于 2024-09-30 23:13:07 字数 592 浏览 6 评论 0原文

我有一个目录中的文件列表。我必须只处理该目录中的某些文件。 filelist 是我想要的文件列表。我该如何实现这一目标?对 bash 解决方案不感兴趣,因为我必须在这个 Python 脚本中完成这一切。非常感谢!

for record in result:
    filelist.append(record[0])

print filelist


for file in os.listdir(sys.argv[1].strip() + "/"):
    for file in filelist: #This doesn't work, how else do I do this? If file equals to my desired file-list, then do something.
        print file

抱歉各位,不知道我是怎么错过这个的!我猜是一大早编码!!模组们,请关闭它,除非有人想要以有效的方式进行操作。

for file in os.listdir(sys.argv[1].strip() + "/"):
    if file in filelist:
        print file

I have a list of files in a directory. I have to process only certain files from that directory. filelist is my desired file-list. How do I go about achieving this? Not interested in a bash solution since I have to do it all in this one Python script. Thanks much!

for record in result:
    filelist.append(record[0])

print filelist


for file in os.listdir(sys.argv[1].strip() + "/"):
    for file in filelist: #This doesn't work, how else do I do this? If file equals to my desired file-list, then do something.
        print file

Sorry guys, not sure how I missed this! Early morning coding I guess!! Mods, please close it unless someone wants to chip in with an efficient way of doing it.

for file in os.listdir(sys.argv[1].strip() + "/"):
    if file in filelist:
        print file

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

眉黛浅 2024-10-07 23:13:07

如果顺序和唯一性不重要,您可以使用 set 交集,这会更高效。

import set
os_list = os.listdir(sys.argv[1].strip() + "/")
for file in set(os_list) & set(filelist):
    #...

改进示例:

import random
import timeit

l = [random.randint(1,10000) for i in range(1000)]
l2 = [random.randint(1,10000) for i in range(1000)]

def f1():
    l3 = []
    for i in l:
        if i in l2:
            l3.append(i)
    return l3

def f2():
    l3 = []
    for i in set(l) & set(l2):
        l3.append(i)
    return l3

t1 = timeit.Timer('f1()', 'from __main__ import f1')
print t1.timeit(100) #2.0850549985

t2 = timeit.Timer('f2()', 'from __main__ import f2')
print t2.timeit(100) #0.0162533142857

If order and uniqueness don't matter, you can use a set intersection, which will be much more efficient.

import set
os_list = os.listdir(sys.argv[1].strip() + "/")
for file in set(os_list) & set(filelist):
    #...

Example of improvement:

import random
import timeit

l = [random.randint(1,10000) for i in range(1000)]
l2 = [random.randint(1,10000) for i in range(1000)]

def f1():
    l3 = []
    for i in l:
        if i in l2:
            l3.append(i)
    return l3

def f2():
    l3 = []
    for i in set(l) & set(l2):
        l3.append(i)
    return l3

t1 = timeit.Timer('f1()', 'from __main__ import f1')
print t1.timeit(100) #2.0850549985

t2 = timeit.Timer('f2()', 'from __main__ import f2')
print t2.timeit(100) #0.0162533142857
森林散布 2024-10-07 23:13:07

听起来你想做一个测试:

for file in os.listdir(sys.argv[1].strip() + "/"):
    if file in filelist:
        # Found a file in the wanted-list.
        print file

Sounds like you want to do a test:

for file in os.listdir(sys.argv[1].strip() + "/"):
    if file in filelist:
        # Found a file in the wanted-list.
        print file
ゃ懵逼小萝莉 2024-10-07 23:13:07

看起来您只想做这样的事情:

for file in os.listdir(sys.argv[1].strip() + "/"): 
    if file in filelist:
        print file 

请注意,我刚刚将第二个 for 更改为 if。但是,由于您询问效率,您可能希望将 filelistlist 更改为 setdict 使 in 运算符更加高效。

It looks like you just want to do something like this:

for file in os.listdir(sys.argv[1].strip() + "/"): 
    if file in filelist:
        print file 

Note that I just changed the second for to an if. However, since you were asking about efficiency, you probably want to change filelist from being a list to being a set or a dict to make the in operator more efficient.

懒的傷心 2024-10-07 23:13:07

像这样的东西:

print [x for x in os.listdir(sys.argv[1].strip() + "/") if x in filelist]

Something like this:

print [x for x in os.listdir(sys.argv[1].strip() + "/") if x in filelist]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文