英文:
count how many ones are in first consecutive repetition in list
问题
For example my list is l=[1113213211] and I want the program to print how many "characters" are in first consecutive repetition of ones, I say ones because they are the the first but it can be any number. For example if list is l=[1113213211] I want my program to print: there are 3 ones then 1 three then 1 two then 1 one then 1 three then 1 two then 2 ones. How can I do that in Python3?
例如,我的列表是l=[1113213211],我希望程序打印出第一个连续重复的"字符"有多少个,我说"字符"是因为它们是第一个,但可以是任何数字。例如,如果列表是l=[1113213211],我希望我的程序打印出:有3个1,然后是1个3,然后是1个2,然后是1个1,然后是1个3,然后是1个2,然后是2个1。如何在Python3中实现这个?
英文:
For example my list is l=[1113213211] and I want the program to print how many "characters" are in first consecutive repetition of ones, I say ones because they are the the first but it can be any number. For example if list is l=[1113213211] I want my program to print: there are 3 ones then 1 three then 1 two then 1 one then 1 three then 1 two then 2 ones. How can I do that in Python3?
P.S. That list I mentioned before can be different. It can be l=[12325228961112333] or something else.
答案1
得分: 2
你可以使用 itertools.groupby
如下所示,
>>> x = [1113213211]
>>> import itertools
>>> g = itertools.groupby(''.join(str(v) for v in x))
>>> for k, grp in g:
... print(f'{k} 连续出现 {len(list(grp))} 次')
...
1 连续出现 3 次
3 连续出现 1 次
2 连续出现 1 次
1 连续出现 1 次
3 连续出现 1 次
2 连续出现 1 次
1 连续出现 2 次
英文:
You could use itertools.groupby
like,
>>> x = [1113213211]
>>> import itertools
>>> g = itertools.groupby(''.join(str(v) for v in x))
>>> for k,grp in g:
... print(f'{k} is present {len(list(grp))} times consequitively')
...
1 is present 3 times consequitively
3 is present 1 times consequitively
2 is present 1 times consequitively
1 is present 1 times consequitively
3 is present 1 times consequitively
2 is present 1 times consequitively
1 is present 2 times consequitively
答案2
得分: 0
你想要的是迭代数字并检查它是否与上一个相同,然后根据情况采取行动。以下代码应该可以实现这个目标:
number = 1113213211
number = [int(d) for d in str(number)] # 将数字拆分为数字列表
my_list = [] # 用于存储表示(数字,出现次数)的列表
lastSeen = None
for i in number: # 遍历所有数字
if lastSeen is None: # 初始情况
lastSeen = [i, 1]
else:
if i == lastSeen[0]: # 如果相同:加1
lastSeen[1] += 1
else: # 如果不同,将其添加到列表中
my_list.append(lastSeen)
lastSeen = [i, 1]
print(my_list)
# [[1, 3], [3, 1], [2, 1], [1, 1], [3, 1], [2, 1]]
如果你只需要翻译,请忽略代码注释。
英文:
What you want is to iterate over the number and check if it is the same as the last one and do something accordingly. The following code should do it:
number = 1113213211
number = [int(d) for d in str(number)] # split number into digits
list = [] # to store lists that represent (number, times seen)
lastSeen = None
for i in number: # iterate over all digits
if lastSeen == None: # initial case
lastSeen = [i,1]
else:
if i == lastSeen[0]: # if the same: add 1
lastSeen[1] +=1
else: # if not the same, add it to the list
list.append(lastSeen)
lastSeen = [i,1]
print (list)
# [[1, 3], [3, 1], [2, 1], [1, 1], [3, 1], [2, 1]]
答案3
得分: 0
以下是翻译好的代码部分:
from itertools import groupby
x = [1,1,1,1,2,2,2,3,3,2,2,1,1]
[(k,len(list(g))) for k,g in groupby(x)]
[(1, 4), (2, 3), (3, 2), (2, 2), (1, 2)]
英文:
itertools groupby was made for this job:
from itertools import groupby
x = [1,1,1,1,2,2,2,3,3,2,2,1,1]
[(k,len(list(g))) for k,g in groupby(x)]
[(1, 4), (2, 3), (3, 2), (2, 2), (1, 2)]
答案4
得分: 0
这是否是您正在寻找的内容?
l = '12325228961112333';
def count_characters(s):
answer = "有"
count = 0
character = s[0]
for ch in s:
if(ch == character):
count += 1
else:
answer += ("{} {}个 ").format(count, character)
character = ch
count = 1
answer += ("{} {}个 ").format(count, character)
return answer
print(count_characters(l))
英文:
Was this the sort of thing you were looking for?
l = '12325228961112333'
def count_characters(s):
answer = "There are "
count = 0
character = s[0]
for ch in s:
if(ch == character):
count += 1
else:
answer += ("{} {}s ").format(count, character)
character = ch
count = 1
answer += ("{} {}s ").format(count, character)
return answer
print(count_characters(l))
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论