如何高效列出图像序列?Python中的数字序列比较
我有一个包含9张图片的文件夹:
image_0001, image_0002, image_0003 image_0010, image_0011 image_0011-1, image_0011-2, image_0011-3 image_9999
我想以一种高效的方式列出它们,比如这样(9张图片分成4行显示):
(image_000[1-3], image_00[10-11], image_0011-[1-3], image_9999)
在Python中,有没有办法能简洁地返回一个图片目录,而不需要逐个列出每个文件?
所以,可能的做法是这样的:
列出所有图片,按数字排序,创建一个列表(从头开始依次计数每张图片)。当某张图片缺失时(创建一个新的列表),继续直到原始文件列表结束。这样我就应该得到一些包含连续序列的列表。
我想让这个数字列表更容易阅读和描述。如果我有1000个连续的文件,它可以清晰地列为file[0001-1000],而不是file['0001','0002','0003'等等...]
编辑1(根据建议):给定一个扁平化的列表,你会如何推导出glob模式?
编辑2 我正在尝试将问题分解成更小的部分。这里是部分解决方案的一个例子: data1有效,data2返回0010为64,data3(真实数据)不工作:
# Find runs of consecutive numbers using groupby. The key to the solution
# is differencing with a range so that consecutive numbers all appear in
# same group.
from operator import itemgetter
from itertools import *
data1=[01,02,03,10,11,100,9999]
data2=[0001,0002,0003,0010,0011,0100,9999]
data3=['image_0001','image_0002','image_0003','image_0010','image_0011','image_0011-2','image_0011-3','image_0100','image_9999']
list1 = []
for k, g in groupby(enumerate(data1), lambda (i,x):i-x):
list1.append(map(itemgetter(1), g))
print 'data1'
print list1
list2 = []
for k, g in groupby(enumerate(data2), lambda (i,x):i-x):
list2.append(map(itemgetter(1), g))
print '\ndata2'
print list2
返回:
data1
[[1, 2, 3], [10, 11], [100], [9999]]
data2
[[1, 2, 3], [8, 9], [64], [9999]]
3 个回答
def ranges(sorted_list):
first = None
for x in sorted_list:
if first is None:
first = last = x
elif x == increment(last):
last = x
else:
yield first, last
first = last = x
if first is not None:
yield first, last
这个 increment
函数留给读者自己去练习。
补充:这里有一个例子,展示如何用整数而不是字符串作为输入来使用它。
def increment(x): return x+1
list(ranges([1,2,3,4,6,7,8,10]))
[(1, 4), (6, 8), (10, 10)]
对于输入中的每个连续范围,你会得到一对数字,表示这个范围的开始和结束。如果某个元素不在任何范围内,那么开始和结束的值是一样的。
好的,我觉得你的问题很有趣,就像一个谜题。我把如何“压缩”数字范围留给你自己去解决(标记为TODO),因为有很多方法可以做到这一点,这取决于你想要的格式,以及你是想要元素数量最少,还是字符串描述长度最短。
这个解决方案使用了一个简单的正则表达式(数字字符串)来把每个字符串分成两类:静态和变量。数据分类后,我用groupby把静态数据收集成最长的匹配组,以达到总结的效果。我在结果中混入了整数索引标记(在matchGrouper中),这样我就可以从所有元素中重新选择变化的部分(在unpack中)。
import re
import glob
from itertools import groupby
from operator import itemgetter
def classifyGroups(iterable, reObj=re.compile('\d+')):
"""Yields successive match lists, where each item in the list is either
static text content, or a list of matching values.
* `iterable` is a list of strings, such as glob('images/*')
* `reObj` is a compiled regular expression that describes the
variable section of the iterable you want to match and classify
"""
def classify(text, pos=0):
"""Use a regular expression object to split the text into match and non-match sections"""
r = []
for m in reObj.finditer(text, pos):
m0 = m.start()
r.append((False, text[pos:m0]))
pos = m.end()
r.append((True, text[m0:pos]))
r.append((False, text[pos:]))
return r
def matchGrouper(each):
"""Returns index of matches or origional text for non-matches"""
return [(i if t else v) for i,(t,v) in enumerate(each)]
def unpack(k,matches):
"""If the key is an integer, unpack the value array from matches"""
if isinstance(k, int):
k = [m[k][1] for m in matches]
return k
# classify each item into matches
matchLists = (classify(t) for t in iterable)
# group the matches by their static content
for key, matches in groupby(matchLists, matchGrouper):
matches = list(matches)
# Yield a list of content matches. Each entry is either text
# from static content, or a list of matches
yield [unpack(k, matches) for k in key]
最后,我们添加了足够的逻辑来美化输出,并运行一个示例。
def makeResultPretty(res):
"""Formats data somewhat like the question"""
r = []
for e in res:
if isinstance(e, list):
# TODO: collapse and simplify ranges as desired here
if len(set(e))<=1:
# it's a list of the same element
e = e[0]
else:
# prettify the list
e = '['+' '.join(e)+']'
r.append(e)
return ''.join(r)
fnList = sorted(glob.glob('images/*'))
re_digits = re.compile(r'\d+')
for res in classifyGroups(fnList, re_digits):
print makeResultPretty(res)
我的图片目录是根据你的示例创建的。你可以用下面的列表替换fnList来进行测试:
fnList = [
'images/image_0001.jpg',
'images/image_0002.jpg',
'images/image_0003.jpg',
'images/image_0010.jpg',
'images/image_0011-1.jpg',
'images/image_0011-2.jpg',
'images/image_0011-3.jpg',
'images/image_0011.jpg',
'images/image_9999.jpg']
当我在这个目录下运行时,我的输出看起来是这样的:
StackOverflow/3926936% python classify.py
images/image_[0001 0002 0003 0010].jpg
images/image_0011-[1 2 3].jpg
images/image_[0011 9999].jpg
这里有一个可以实现你想要的功能的代码,使用了你之前添加的代码作为起点:
#!/usr/bin/env python
import itertools
import re
# This algorithm only works if DATA is sorted.
DATA = ["image_0001", "image_0002", "image_0003",
"image_0010", "image_0011",
"image_0011-1", "image_0011-2", "image_0011-3",
"image_0100", "image_9999"]
def extract_number(name):
# Match the last number in the name and return it as a string,
# including leading zeroes (that's important for formatting below).
return re.findall(r"\d+$", name)[0]
def collapse_group(group):
if len(group) == 1:
return group[0][1] # Unique names collapse to themselves.
first = extract_number(group[0][1]) # Fetch range
last = extract_number(group[-1][1]) # of this group.
# Cheap way to compute the string length of the upper bound,
# discarding leading zeroes.
length = len(str(int(last)))
# Now we have the length of the variable part of the names,
# the rest is only formatting.
return "%s[%s-%s]" % (group[0][1][:-length],
first[-length:], last[-length:])
groups = [collapse_group(tuple(group)) \
for key, group in itertools.groupby(enumerate(DATA),
lambda(index, name): index - int(extract_number(name)))]
print groups
这个代码会输出 ['image_000[1-3]', 'image_00[10-11]', 'image_0011-[1-3]', 'image_0100', 'image_9999']
,这正是你想要的结果。
历史背景:我最开始回答这个问题时搞错了方向,正如 @Mark Ransom 在下面指出的那样。为了记录历史,我最初的回答是:
你需要用到 glob。试试这个:
import glob
images = glob.glob("image_[0-9]*")
或者,使用你的例子:
images = [glob.glob(pattern) for pattern in ("image_000[1-3]*",
"image_00[10-11]*", "image_0011-[1-3]*", "image_9999*")]
images = [image for seq in images for image in seq] # flatten the list