我们说我有一个这样的列表:
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]
答案 0 :(得分:1)
<强>代码:强>
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']
答案 1 :(得分:1)
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]
答案 2 :(得分:1)
我认为Rob's solution是最好的:最易读且最容易理解。我修复了它的情况,最后一个元素是空白的,并改为工作为生成器(它比使用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]
答案 3 :(得分:0)
您可以使用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