Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何修改输出_Python_String_List_Output - Fatal编程技术网

Python 如何修改输出

Python 如何修改输出,python,string,list,output,Python,String,List,Output,有人能告诉我如何修改输出,使空行不会出现吗?运行程序后得到的输出是: def noalpha(s): noa = '' for c in s: if not (c in noa or c.isalpha()): noa += c return noa def flines(t, ww): s=noalpha(t) lines=t.lower().splitlines() lst=[] for i

有人能告诉我如何修改输出,使空行不会出现吗?运行程序后得到的输出是:

def noalpha(s):

    noa = ''
    for c in s:
        if not (c in noa or c.isalpha()):
            noa += c
    return noa

def flines(t, ww):

    s=noalpha(t)
    lines=t.lower().splitlines()
    lst=[]
    for i in range(len(lines)):
        for caratteri in s:
            lines[i]=lines[i].replace(caratteri, ' ')
        wrdst=lines[i].split()
        l= 'Line ' +str(i)+': ' 
        False
        for k in ww:
            k=k.lower()
            num=wrdst.count(k)

            if num>0:
                l+='"'+ str(k) + '"=' + str(num)+' '
                True
        if True:
            lst+=[l]


    return lst 

虽然这段代码并不适合做您希望做的事情,但您需要更改这些行

['Line 0: "to"=1 ', 'Line 1: "the"=1 ', 'Line 2: "the"=1 ', 'Line 3: "to"=1 ', 'Line 4: "so"=2 ', 'Line 5: ', 'Line 6: ', 'Line 7: "to"=1 "the"=1 ', 'Line 8: "to"=1 "the"=2 ', 'Line 9: ', 'Line 10: ', 'Line 11: "to"=1 "the"=1 ', 'Line 12: ', 'Line 13: ', 'Line 14: "so"=1 ', 'Line 15: "to"=1 ', 'Line 16: ', 'Line 17: ', 'Line 18: "to"=1 "the"=2 ', 'Line 19: "the"=1 ', 'Line 20: "the"=4 ', 'Line 21: ', 'Line 22: ', 'Line 23: ', 'Line 24: ', 'Line 25: ', 'Line 26: ', 'Line 27: "the"=1 ', 'Line 28: ', 'Line 29: ', 'Line 30: "the"=1 ', 'Line 31: ']
如果你把它改成

if num>0:
    l+='"'+ str(k) + '"=' + str(num)+' '
    True
if True:
    lst+=[l]

然后,如果您在for循环的开始处重新初始化flag=False,它应该会产生您想要的结果。通过执行if-True,您要说的是始终附加到列表中,而不是当列表中确实有某些内容时

Erm,为什么有一个
False
True
不知从哪里冒出来?这样它就只在循环为真时填充列表这样的代码只工作它打印出没有参数wwGames Braniac的单词的行你是什么意思?在/output中的一些示例?是的,这非常有效你能解释为什么必须为变量指定true和false属性吗?另外,我的输出中的另一个小问题是它在不同的行上打印:[“第0行:”to“=1”,“第1行:”the“=1”,“第2行:”to“=1”,“第3行:”to“=1”,“第4行:”so“=2”,“第7行:”to“=1”,“第8行:”to“=1”,“第11行:”to“=1”,“第14行:”so“=1”,“第15行:”to“=1”,“第18行:”to“=1”to“=2”,“第19行:“=1”,“第20行:”第“=4”,“第27行:”第“=1”,“第30行:”第“=1']行将始终评估为真我编辑了我的答案以反映这一点。不确定您的意思,我的it打印在不同的行上,请详细说明。非常感谢,我解决了这个问题
if num>0:
    l+='"'+ str(k) + '"=' + str(num)+' '
    flag = True
if flag:
    lst+=[l]