简体   繁体   English

大家好,我只是想知道我的代码有什么问题?

[英]Hey everyone, I was just wondering what was wrong with my code?

Im trying to create a function that records the amount of times a value in a list is repeated in a row.我试图创建一个 function 来记录列表中的值在一行中重复的次数。 After about an hour of deliberation, I was able to reach a stable-looking conclusion, yet i am unable to receive the desired outcome经过大约一个小时的考虑,我得出了一个看似稳定的结论,但我无法得到想要的结果

lst = [1,2,2,2,3,1,1]

def compress(numlist):
    intervals,x=0,0
    for i in numlist:   
        if i == numlist[x]:
            intervals+=1   
        elif i != numlist[x]:
            print((intervals,numlist[x]), end=" ")
            x+=intervals
            intervals=1

    
    
compress(lst)

Here, I was trying to get the function to "compress" the list of integers When I run the code, the outcome is:在这里,我试图让 function 来“压缩”整数列表当我运行代码时,结果是:

(1, 1) (3, 2) (1, 3)

While the desired outcome is:虽然期望的结果是:

(1, 1) (3, 2) (1, 3) (2,1)

You only show a run when you find a value that is not equal to the previous one.只有当您发现一个值不等于前一个值时,您才显示运行。 There is no value after the last value, of course, so the last run is never printed.当然,最后一个值之后没有任何值,因此永远不会打印最后一次运行。 Just add a line after the loop to print the last run.只需在循环后添加一行即可打印最后一次运行。

if intervals != 0:
    print((intervals,numlist[x]))

By the way, you don't need the elif conditional.顺便说一下,你不需要elif条件。 All you need there is an else , since if i == numlist[x] , then the only other thing possible is i != numlist[x] .您只需要一个else ,因为如果i == numlist[x] ,那么唯一可能的就是i != numlist[x]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 对于 euler 4 号项目,我只是想知道我的代码有什么问题。 一直在敲我的大脑,我不知道怎么做,这是在 python - for project euler number 4 i am just wondering what is wrong with my code. have been knacking my brain and i do not know how, this is on python 我不知道我的代码有什么问题(刚开始 python) - I dont know what is wrong with my code (just started python) 嘿,有人能帮我找出我的代码有什么问题来解决欧拉问题 50(连续质数和),它只适用于低于一百 - hey,can someone help me to find out whats wrong with my code to solve Euler problem 50(Consecutive prime sum), its just work for below one-hundred 大家好,我在 Django 中对查询集进行排序时遇到问题 - Hey everyone I have problem in sorting queryset in Django 大家好。 我在 python 上的代码有问题 - Hey guys. I am having problem on my code on python 我不知道我的代码有什么问题。 当我运行代码时它只是在一个循环中并且不会停止循环 - i dont know what is wrong with my code. When i run the code it is just in a loop and wont stop the loop 嘿伙计们,我在这段代码中做错了什么。 我正在尝试将 13195 的所有主要因素 append 到一个空列表,但它给了我一个空列表 - Hey guys what am i doing wrong in this code . i am trying to append all the prime factors of 13195 to an empty list but its giving me an empty list 我想知道为什么我的程序在 40 和 50 小时内给出错误的输出? - I am wondering why my program is giving wrong outputs for over 40 and 50 hours? 错误auctions.models.Listing.DoesNotExist:列表匹配查询不存在。 我刚开始学习 django,我的代码有什么问题? - error auctions.models.Listing.DoesNotExist: Listing matching query does not exist. I am just starting learning django, what is wrong in my code? 想知道用海龟绘制棋盘的 python 代码发生了什么? 有什么帮助吗? - wondering what happened to my python code that draw a checkerboard with turtle? any helps?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM