For循环使用被调用函数无法正确迭代

时间:2016-05-19 16:58:30

标签: python for-loop

我一直遇到一些问题,让循环迭代,调用函数,然后返回循环并再次迭代。它现在只运行两次。我认为它有一种方法可以解决我在循环中调用函数的方式。有什么我想念的吗?

#!/usr/bin/python
# -*- coding: utf-8 -*-

import os, csv, xlrd, sys
import pandas as pd
import numpy as np
from openpyxl import load_workbook

def PosFinder():

    with open('FinalMutations.csv', 'w') as csvf:
        writer = csv.writer(csvf, delimiter=' ') 
        csvf.close()

    MutFinder()

def MutFinder():
    df = pd.read_csv('mutation-table.csv', sep=None)

    MutationList = df['Seq ID']

    Positions = list(set(MutationList))

    n = len(Positions)

    for i in (0, n):
        print(i)
        MutationPos=Positions[i]
        MutationFound=df[df['Seq ID'].str.contains(MutationPos)]
        FreqCheck(MutationFound)
        i+=1


    print('Program Complete!')


def FreqCheck(MutationFound):

    PFreqs=MutationFound.ix[:,3]
    PFreqs=PFreqs.str.strip('%')
    Freqs= PFreqs.astype(float)

    if len(MutationFound)==1:
        Check = all(i<10.0 for i in Freqs)
        if Check in [False, 'False']:
            ToExcel(MutationFound)
    else:
            Check = all(i<10.0 for i in Freqs)

            if Check in [False, 'False']:
               ConstantFreq(MutationFound)


def ConstantFreq(MutationFound):        

    PFreqs=MutationFound.ix[:,3]
    PFreqs=PFreqs.str.strip('%')
    Freqs= PFreqs.astype(float)
    Flag= all(x==Freqs[0] for x in Freqs)

    if Flag in [False, 'False']:
        RangeCheck(MutationFound, Freqs)

def RangeCheck(MutationFound, Freqs):

    minFreq= Freqs.min()
    maxFreq= Freqs.max()
    netFreq= maxFreq-minFreq

    if netFreq>10:
        ToExcel(MutationFound)


def ToExcel(MutationFound):

    with open('FinalMutations.csv', 'a') as csvf:

        writer = csv.writer(csvf, delimiter=' ') 
        for row in MutationFound:       
                writer.writerow(row)


###Start Program### 
PosFinder()

1 个答案:

答案 0 :(得分:0)

for循环目前需要(0, n),这只是两个值0n的列表。

更改为range(n)以获取所有值01,...,n-1