2016-11-04 14 views
0

リストに新しい行を追加しようとしていますが、最後の行だけがリストに複製されるように見えます。 私は自分の問題を示す小さなテストスクリプトを作った。 私がしたいのは、元のリストにNIGHTSがあり、元の日付に1日を追加するだけで、元のリストに数多くの行を追加することです。これから私はメールを送るための日報を作成しますので、私たちはB & Bでその日の予定を知り、温水ボイラーをオンまたはオフに切り替えます。Pythonリストに新しい行を追加すると、最後の行だけがリストに追加されます

これはPythonでの私のテストスクリプトです

#!/usr/bin/env python 
# -*- coding: UTF-8 -*- 

from datetime import datetime, timedelta 

NIGHTS = 12 # number of nights a guest stays @ our B&B 
CHECKIN = 0 # date the guest checks in 
aList = [] 

""" 
contents test agenda.dat 
    check in       nights 
    V         V 
"2016-10-27,1,*,K4,A1,*,D4,*,PD,PF,0,*,7,TEST4,remark" 
"2016-10-28,1,*,K4,A1,*,D4,*,PD,PF,0,*,1,TEST1,remark" 
"2016-10-29,1,*,K4,A1,*,D4,*,PD,PF,0,*,1,TEST2,remark" 
"2016-10-30,1,*,K4,A1,*,D4,*,PD,PF,0,*,2,TEST3,remark" 

copy past this into agenda.dat and you have your file 

""" 

# 
# --------- convert date --------- # 
def date(cDate=None, format='%Y-%m-%d'): 
    if not cDate: 
     dDate = datetime.today().date() 
    else: 
     dDate = datetime.strptime(cDate, '%Y-%m-%d') 

    return dDate 

# 
# --------- get contents agenda -------- # 
# 
def read_agenda(): 

    aBooking=[] 

    with open("agenda.dat", "r") as f: 
     next(f) # skip header line 

     for line in f: 
      line = line.strip() 
      aBooking.append(line.split(",")) 

    return aBooking 


aList = read_agenda() 

# checking out contents of aList 
###### 
for x in aList: 
    print x 

print "=============" 
# it checks out 

# thought using an empty list would solve the problem, not 
bList = [] 

newline = [] # just declaring everything would help? 
n = 0   # not 

####### 
for x in aList: 
    n = int(float(x[NIGHTS])) 
    newline = x 

    # the first agenda-data line contains '7' so n = 7 

    while n > 1: 
     cDatum = date(newline[CHECKIN]).date() + timedelta(days=1) 
     newline[CHECKIN] = "{:%Y-%m-%d}".format(cDatum) 
     newline[NIGHTS] = str(1) 

     # just to check if the conversion went ok, yes 
     print newline 
     # output: 
     # ['2016-10-28', '1', '*', 'K4', 'A1', '*', 'D4', '*', 'PD', 
     # 'PF', '0', '*', '1', 'TEST1', 'remark'] 

     # and now it happens adding a line to this aray ends in 
     # replicating n lines with the same contents 
     # this should not be, a new line should contain a new date 
     bList.append(newline) 
     n -=1 


################ 
print "==== bList =========" 
for y in bList: 
    print y 
# this prints the same line 7 times! 

print "=== aList ==========" 
for x in aList: 
    print x 
# and yes the original data is still intact except for the 7 this turned 
# into 1 as desired 

質問: は私が間違って何をしますかここ 私は、最初のレコード と最後の1のための2行の日付をインクリメントして7行を取得する必要があります。

答えて

0

内側のループwhile n > 1:は、同じnewlineオブジェクトを変更してそれをbListに追加し続けます。だから、あなたはを持っていないので、あなたはと同じnewlineという7つの参照を得るでしょう。newlineのオブジェクトはbListにはありません。

あなたはxからコピーした新しいnewlineオブジェクトを(作成することwhile n > 1:前に、その

newline = x 

ラインを取り除くと

while n > 1: 
    newline = x[:] 

にループの開始を変える取得することによってこの問題を解決することができます)を繰り返します。

この解決策は、xが文字列のリストである場合に機能します。ただし、xが実際にリストのリストである場合、それらの内部リストのコピーも作成する必要があります。

+0

ありがとうございますが、それで修正されませんでした。最後の日付の6倍の代わりに、最初のdatを6回コピーします。 これは私の友人が解決策を得た方法です。 (0,15)の範囲で0の を入力してください:#コピーレコード a [z] = x [z] Pythonianではないかもしれませんが、問題を修正しました。 改行= xが元のレコードo.s.l.t。 – LMQR15

+0

、@ LMQR15については申し訳ありません。明らかに、あなたが[mcve]を投稿しなかったので、提案された変更をテストできませんでした。ところで、 'x [z]'が文字列かint型の場合、 'a [z] = x [z]'は動作しますが、 'x [z]'がリストでない場合、それは 'x [z]'の名前と同じオブジェクトに対して 'a [z]'を別の名前にするだけです。この記事は役に立ちましたか:SOベテランのNed Batchelderによって書かれた[Pythonの名前と値についての事実と神話](http://nedbatchelder.com/text/names.html) –

+0

例の上にコメントを付けてagenda.datを作成すると、その例を確認しようとしました。 あなたのリンクとコメントをありがとう、本当に何がうまく行ったのか理解するのに役立ちます。私はちょうど初心者ですが、すごく楽しいです! – LMQR15

関連する問題