用计数替换空白字符串
replacing blank strings with count
假设我有一个这样的列表:
py = ['','','','','monty','','','','python',]
我想把它映射到这个:
[4,'monty',3,'python']
有谁知道一个聪明的解决办法吗?我能够想出这个来将它转换成这个:
[1,1,1,1,'monty',1,1,1,'python',]
使用:
quotes = [x if x else 1 for x in quotes]
代码:
def convert_quote_list(input_list):
quotes = [x if x else 1 for x in input_list]
counter = 0
ans = []
for each in quotes:
if each == 1:
counter += 1
else:
if counter:
ans.append(counter)
ans.append(each)
counter = 0
return ans
convert_quote_list(['','','','','monty','','','','python',])
convert_quote_list(['monty','','','python',])
输出:
[4, 'monty', 3, 'python']
['monty', 2, 'python']
py = ['','','','','monty','','','','python']
py2 = ['','','','','monty','','','','python', '', '', '']
def conv(lst):
out = []
count = 0
for s in lst:
if s == '': count += 1
else:
if count > 0:
out.append(count)
count = 0
out.append(s)
if count > 0: out.append(count)
return out
print(conv(py))
print(conv(py2))
输出
[4, 'monty', 3, 'python']
[4, 'monty', 3, 'python', 3]
您可以使用 python 的 itertools.groupby 创建按列表中的值分组的迭代器列表。然后使用列表理解和短路评估,获取迭代器列表的长度或字符串值
import itertools
py = ['','','','','monty','','','','python',]
out = [(k == 1 and len(list(v))) or k for k, v in (grp for grp in itertools.groupby(py, lambda x: x or 1))]
print out
[4, 'monty', 3, 'python']
编辑:经过深思熟虑,代码可能难以阅读,因此您可以这样做:
import itertools
out = []
for k, v in itertools.groupby(py, lambda x: x or 1):
if k == 1:
out.append(len(list(v)))
else:
out.append(k)
print out
我认为 是最好的:最易读且最容易理解。我修复了它,以防最后一个元素为空并改为用作生成器(它会比使用 append
更有效):
def blank_to_count(iterable):
counter = 0
for val in iterable:
if val == '':
counter += 1
else:
if counter > 0: yield counter # yield count of blank elements
counter = 0
yield val # yield current non-blank element
if counter > 0: yield counter # in case last element was blank
py = ['','','','','monty','','','','python',]
print(list(blank_to_count(py))) # [4, 'monty', 3, 'python']
py = ['monty','','','','python']
print(list(blank_to_count(py))) # ['monty', 3, 'python']
py = ['','','','','monty','','','','python','','']
print(list(blank_to_count(py))) # [4, 'monty', 3, 'python', 2]
假设我有一个这样的列表:
py = ['','','','','monty','','','','python',]
我想把它映射到这个:
[4,'monty',3,'python']
有谁知道一个聪明的解决办法吗?我能够想出这个来将它转换成这个:
[1,1,1,1,'monty',1,1,1,'python',]
使用:
quotes = [x if x else 1 for x in quotes]
代码:
def convert_quote_list(input_list):
quotes = [x if x else 1 for x in input_list]
counter = 0
ans = []
for each in quotes:
if each == 1:
counter += 1
else:
if counter:
ans.append(counter)
ans.append(each)
counter = 0
return ans
convert_quote_list(['','','','','monty','','','','python',])
convert_quote_list(['monty','','','python',])
输出:
[4, 'monty', 3, 'python']
['monty', 2, 'python']
py = ['','','','','monty','','','','python']
py2 = ['','','','','monty','','','','python', '', '', '']
def conv(lst):
out = []
count = 0
for s in lst:
if s == '': count += 1
else:
if count > 0:
out.append(count)
count = 0
out.append(s)
if count > 0: out.append(count)
return out
print(conv(py))
print(conv(py2))
输出
[4, 'monty', 3, 'python']
[4, 'monty', 3, 'python', 3]
您可以使用 python 的 itertools.groupby 创建按列表中的值分组的迭代器列表。然后使用列表理解和短路评估,获取迭代器列表的长度或字符串值
import itertools
py = ['','','','','monty','','','','python',]
out = [(k == 1 and len(list(v))) or k for k, v in (grp for grp in itertools.groupby(py, lambda x: x or 1))]
print out
[4, 'monty', 3, 'python']
编辑:经过深思熟虑,代码可能难以阅读,因此您可以这样做:
import itertools
out = []
for k, v in itertools.groupby(py, lambda x: x or 1):
if k == 1:
out.append(len(list(v)))
else:
out.append(k)
print out
我认为 append
更有效):
def blank_to_count(iterable):
counter = 0
for val in iterable:
if val == '':
counter += 1
else:
if counter > 0: yield counter # yield count of blank elements
counter = 0
yield val # yield current non-blank element
if counter > 0: yield counter # in case last element was blank
py = ['','','','','monty','','','','python',]
print(list(blank_to_count(py))) # [4, 'monty', 3, 'python']
py = ['monty','','','','python']
print(list(blank_to_count(py))) # ['monty', 3, 'python']
py = ['','','','','monty','','','','python','','']
print(list(blank_to_count(py))) # [4, 'monty', 3, 'python', 2]