0

私は問題を理解しようとして多くの時間を無駄にしましたが、運がありませんでした。私の学校でTAに聞いてみましたが、彼は役に立たなかった。私は初心者ですが、間違いが多いので、詳しく説明してもらえたら嬉しいです。とにかく、基本的に私が次の関数でやろうとしていることは次のとおりです。

  • whileループを使用して、random_stringがTEXTに含まれているかどうかを確認し、含まれていない場合はNoneTypeを返します。
  • はいの場合は、forループを使用して、そのTEXTから行を読み取り、リストl1に配置します。
  • 次に、ifステートメントを記述して、random_stringがl1にあるかどうかを確認します。
  • そうである場合は、いくつかの計算を行います。
  • それ以外の場合は次の行を読んでください
  • 最後に、計算全体を返します。

TEXT = open('randomfile.txt')

def random (TEXT, random_string):
    while random_string in TEXT:
        for lines in TEXT:
            l1=TEXT.readline().rsplit()
            if random_string in l1:
                '''
                    do some calculations
                '''
            else:
                TEXT.readline() #read next line???
        return #calculations
    return None
4

3 に答える 3

1

計算が線の関数であると仮定すると、次のようになります。

def my_func(fileobj,random_string,calculation_func):
    return [calculation_func(line) for line in fileobj if random_string in line] or None

それ以外の場合は、これを行うことができます:

def my_func(fileobj,random_string):
    calculated = []
    for line in fileobj:
        if random_string in line:
            #do calculations, append to calculated
    return calculated or None

関数の複雑さが不必要に増えるため、whileループを省略しました。 fileobjバッファなどのファイルのようなオブジェクト、またはによって返されるオブジェクトのようなオブジェクトを想定していますopen

whileループで編集:

def my_func(fileobj,random_string):
    calculated = []
    try:
        while True: #remnant from competitive programming - makes it faster
            line = fileobj.readline()
            if random_string in line:
                #do calculations, append to calculated
    except EOFError:  #catches the error thrown when calling readline after file is empty.
        return calculated or None

編集 2OPの新しい情報を考慮に入れる

def my_func(fileobj,random_string):
    total = 0
    number = 0
    try:
        while True:
            line = fileobj.readline()
            if random_string in line:
                total += float(line.split()[1])
                number += 1
    if total == number == 0:
        return 0 #or whatever default value if random_string isn't in the file
    return total/number

短いバージョン:

def my_func(fileobj,random_string):
    results = [float(line.split()[1]) for line in fileobj if random_string in line]
    return sum(results)/len(results)
于 2012-11-07T05:14:29.250 に答える
1

多分?:

def my_func(ccy):
    with open('randomfile.txt', 'r') as f:
        l1 = [float(line.split()[-1]) for line in f.readlines() if ccy in line]
        if l1:
            return sum(l1) / len(l1)
        else:
            return None
于 2012-11-07T05:06:02.473 に答える
0

あなたの要件を明確にすることができれば:

  • while ループを使用して がファイル内にあるかどうかを確認しrandom_string、そうでない場合は returnを確認しますNone
  • リストにある行を収集random_stringします。
  • 収集された行に対していくつかの計算を実行し、計算結果を返します。

次に、次の手順で開始します。

calculation_lines = []
random_string = 'needle'

with open('somefile.txt') as the_file:
   for line in the_file:
       if random_string in line:
           calculation_lines.append(line)

if not calculation_lines:
   return None # no lines matched

for i in calculation_lines:
    # do some calculations
    result_of_calculations = 42

return result_of_calculations
于 2012-11-07T07:33:18.880 に答える