问题描述
假设我有一个这样的列表:
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]
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']
2楼
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]
3楼
我认为是最好的:最具可读性和最容易理解 。
我修复了它,以防万一最后一个元素为空并更改为可以用作生成器(然后使用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]
4楼
您可以使用python的创建一个按列表中的值分组的迭代器列表。 然后使用列表理解和短路评估,获得迭代器列表的长度或字符串值
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