我有一个包含重复元素的列表。我想计算每个元素的出现次数
示例 - >
list1= ["a", "b", "b", "c", "d", "d", "d", "a", "a"]
输出 - >
result = ["a1", "b2", "c1", "d3", "a2"]
即。输出格式应为<value><count>
我尝试过这个解决方案,但它没有给出预期的输出
def find_repetitive_elem(a):
dict_obj = {}
arr_obj = []
for i in range(0, len(a)-1):
count = 1
if a[i] == a[i+1]
set_val = "%s%s" % (a[i], count+1)
else:
set_val = "%s%s" % (a[i], count)
arr_obj.append(set_val)
我正在尝试使用算法而不是python包来实现它!
答案 0 :(得分:4)
您可以使用itertools.groupby
:
from itertools import groupby
list1= ["a", "b", "b", "c", "d", "d", "d", "a", "a"]
answer = [group[0] + str(len(list(group[1]))) for group in groupby(list1)]
print(answer)
<强>输出强>
['a1', 'b2', 'c1', 'd3', 'a2']
答案 1 :(得分:2)
您可以使用groupby
模块中的itertools
来实现此目的:
import itertools
list1= ["a", "b", "b", "c", "d", "d", "d", "a", "a"]
["{}{}".format(key,sum( 1 for _ in group )) for key, group in itertools.groupby( list1 ) ]
当您更新了答案,表明您不想使用Python软件包时,这是我可以想到的最优雅的方式,不使用外部模块:
list1= ["a", "b", "b", "c", "d", "d", "d", "a", "a"]
#output-> result = ["a1", "b2", "c1", "d3", "a2"]
def find_repetitive_elem(array):
output=[]
for letter in array:
existing_entry = {}
new_entry = {}
if len(output)>0:
existing_entry= output.pop();
if letter in existing_entry:
existing_entry[letter] +=1
output.append(existing_entry)
else:
if existing_entry:
output.append(existing_entry)
new_entry[letter]=1
output.append(new_entry)
return output
output = find_repetitive_elem(list1)
print ["{}{}".format(key,value) for key,value in (entry.popitem() for entry in output)]
答案 2 :(得分:2)
不确定你的意思
我正在尝试使用算法而不是python来实现它 包的!
这不使用itertools(尽管这是一种更好的方法)
def compact(inval):
last_char = None
outval = []
count = 0
for l in inval + ['dummyvalue']:
if last_char and last_char != l:
outval.append("{}{}".format(last_char, count))
count = 0
last_char = l
count += 1
return outval
letters = ["a", "b", "b", "c", "d", "d", "d", "a", "a"]
print(compact(letters))
输出
letters = ["a", "b", "b", "c", "d", "d", "d", "a", "a"] # yields
['a1', 'b2', 'c1', 'd3', 'a2']
letters = ["a", "b", "b", "c", "c", "d", "e", "d", "d", "d", "a", "a"] # yields
['a1', 'b2', 'c2', 'd1', 'e1', 'd3', 'a2']
答案 3 :(得分:2)
由于您不想使用任何Python的模块,因此这是一个不使用任何模块的解决方案(隐式地除了builtins
)。代码应该能够处理的不仅仅是字符串。
def main():
array = 'a', 'b', 'b', 'c', 'd', 'd', 'd', 'a', 'a'
result = find_repetitive_elements(array)
print(result)
def find_repetitive_elements(array):
result, first, last, count = [], True, None, None
for item in array:
if first:
first, last, count = False, item, 1
elif item == last:
count += 1
else:
result.append('{}{}'.format(last, count))
last, count = item, 1
if not first:
result.append('{}{}'.format(last, count))
return result
if __name__ == '__main__':
main()
答案 4 :(得分:1)
这可能看起来很基本,但这就是我理解这个问题的方法:
<强>代码:强>
list1 = ["a", "b", "b", "c", "d", "d", "d", "a", "a"]
def find_repetitive_elem_continuously(arr):
result = list()
prev_ele = arr[0]
count = 0
for curr_ele in arr:
if curr_ele != prev_ele:
new_arr_ele = str(prev_ele)+""+str(count)
result.append(new_arr_ele)
prev_ele = curr_ele
count = 1
else:
count += 1
new_arr_ele = str(prev_ele)+""+str(count)
result.append(new_arr_ele)
print result
find_repetitive_elem_continuously(list1)
<强>输出:强>
['a1', 'b2', 'c1', 'd3', 'a2']
答案 5 :(得分:0)
这是我未使用itertools.groupby
的尝试。代码依赖于itertools.dropwhile
迭代器,但是替换它很容易,我把它留作练习。
import itertools
list1= ["a", "b", "b", "c", "d", "d", "d", "a", "a"]
result = []
lst = list1.copy()
while len(lst)>0:
current_char = lst[0]
length = len(lst)
lst = list(itertools.dropwhile(lambda x: x==current_char, lst))
result.append("{}{}".format(current_char, length-len(lst)))
print(result)
的代码输出