2017-01-27 7 views
0

前の質問に続き、私は次のコードを持っています。なんらかの理由で、currentlikesがインクリメントされていても、リストの値は変更されません。コメントは私が何をしようとしているのかを説明します:リストの値を変更しますか?

print("PRINT THE CURRENT FILM ROW", allfilms[3]) 
print("PRINT THE CURRENT LIKES", allfilms[3].split(",")[4]) 
currentlikes=allfilms[3].split(",")[4] #this retrieves the current likes from field 
print(currentlikes) 
currentlikes=+1 #this increments the current likes stored in the list 
print(currentlikes) 
allfilms[3].split(",")[4]=currentlikes #this attempts to change the value from 0 to 1 in the respective field 
print(allfilms)#this should print the updated list with the incremented +1 in the Third Row, Fourth Field (0 to 1) 

出力は以下の通りです。前述のように、現在の好きなものを変更しますが、最後にallfilmsを印刷すると、関連フィールドは更新されません。

PRINT THE CURRENT FILM ROW 3,Sci-Fi,Star Trek, PG, 0 PRINT THE CURRENT LIKES 0 0 1 ['0,Genre, Title, Rating, Likes', '1,Sci-Fi,Out of the Silent Planet, PG, 0', '2,Sci-Fi,Solaris, PG,0', '3,Sci-Fi,Star Trek, PG, 0', '4,Sci-Fi,Cosmos, PG, 0', '5,Drama, The English Patient, 15, 0', '6,Drama, Benhur, PG, 0', '7,Drama, The Pursuit of Happiness, 12, 0', '8,Drama, The Thin Red Line, 18, 0', '9,Romance, When Harry met Sally, 12, 0', "10,Romance, You've got mail, 12, 0", '11,Romance, Last Tango in Paris, 18, 0', '12,Romance, Casablanca, 12, 0']

+5

'allfilms [3] .splitは( " ")は'新しいリストを返し、あなたはどこでも –

+0

allfilmsは、[3] .splitは("、") '持っ'の戻り値をこのリストを格納していません'allfilms'や' allfilms [3] 'への接続は残っていません。 'allfilms'を自動的に変更することはありません。 – user2357112

+3

** currentlikes = + 1 **は入力ミスですか? –

答えて

1

allfilms[3].split(",")与えられた文字列を分割し、文字列から接続されていない新しいリストを返します。

リストに参加し、文字列をallfilms[3]に上書きする必要があります。

>>> x = '3,Sci-Fi,Star Trek, PG, 0'; 
>>> record = x.split(',') 
>>> record[4] = 1 
>>> x 
'3,Sci-Fi,Star Trek, PG, 0' 
>>> record 
['3', 'Sci-Fi', 'Star Trek', ' PG', 1] 
>>> x = ','.join(str(i) for i in record) 
>>> x 
'3,Sci-Fi,Star Trek, PG,1' 
+0

OPは 'like'フィールドをインクリメントしたいので、 'record [4] = int(レコード[4] .strip())+ 1'を提案します。 –

関連する問題