私はちょうどPythonを学び始めました。私はタプルのリストにcsvファイルのデータを格納する必要があります:各行の値を表すタプル、すべての行を格納するリスト 私が問題を抱えている機能は、リストをフィルタリングする必要があるときです。基本的には、条件を満たすものだけを含むリストのコピーを作成します。私は正常にすべてのタプルをリストに追加しましたが、タプルを新しいリストに追加する必要がある場合、それは機能しません。Python 3、リストにタプルを追加する方法
def filterRecord():
global filtered
filtered = list()
try:
if int(elem[2])>= int(command[-1]): #the condition
#if I print(elem) here, all results are correct
filtered.append(tuple(elem)) #tuples do not add into the list
#len(filtered) is 0
except ValueError:
pass
def main():
infile = open('file.csv')
L = list()
for line in infile:
parseLine() #a function store each row into tuple
for line in stdin:
command = line.split() #process user input, multiple lines
for elem in L:
if command == 0:
filterRecord()
私が実行すると、プログラムは応答しません。私が強制停止した場合、トレースバックは常にfor line in stdin
です。また、私はこのプログラムでcsvモジュールを使用することはできません。
フィルタ基準は何ですか:あなたは、次にような何かを行うことができ
? –
@WillemVanOnsem elem [2]が入力した数値以上の場合 –