2016-09-01 4 views
2

内のインデックスから文字を削除し、私は、各第四指標の最後の2つの文字を削除する方法リストで、リストに?だから、あまり混乱用語で:</p> <p>...それは簡単なようだが、私は、私の人生のために把握することはできません本当に簡単な質問複数のリスト

f = ['yes', 'no', 'tot', 'foop\n'] 
p = ['ick', 'throw', 'tamp', 'lap\n'] 

L = [] 

L.append(f) 
L.append(p) 

だから今、Lは次のとおりです。

L = [['yes', 'no', 'tot', 'foop\n'], ['ick', 'throw', 'tamp', 'lap\n']] 

は、どのように私は4回ごとのインデックスの最後に\ nを取り除くでしょうループを書くことになって行くだろう、両方のリストの中で(それは私が上のリストにある2つのリスト以上に拡張することもできます)?

最後のインデックスの最後に\ nを付けずに、Lを返すだけです。

+0

は、それがすべてのリストのそれは4つの要素を持っています固定されていますか? – ameyCU

+1

'L.append([x。strip()for f in x]) ' – ozgur

答えて

1
f = ['yes', 'no', 'tot', 'foop\n'] 
p = ['ick', 'throw', 'tamp', 'lap\n'] 

L = [] 

L.append(f) 
L.append(p) 

for i in range(len(L)): 
    L[i][3] = L[i][3].rstrip('\n') # Use rstrip to strip the specified character(s) from the right side of the string. 
print L 

>>[['yes', 'no', 'tot', 'foop'], ['ick', 'throw', 'tamp', 'lap']] 
+1

これらの回答は素晴らしかったが、これは最も速く、最も簡単に働いた。ありがとうございました! – Destroxia

1
f = ['yes', 'no', 'tot', 'foop\n'] 
p = ['ick', 'throw', 'tamp', 'lap\n'] 

L = [] 

L.append(f) 
L.append(p) 
#iterate through every element in the list and 
#apply simple slicing 
for x in L: 
    x[-1] = x[-1][:-1] 

出力 - すでにコメントで述べた

[['yes', 'no', 'tot', 'foop'], ['ick', 'throw', 'tamp', 'lap']] 
0

f = ['yes', 'no', 'tot', 'foop\n'] 
p = ['ick', 'throw', 'tamp', 'lap\n'] 

L = [] 

L.append([item.strip() for item in f]) 
L.append([item.strip() for item in p]) 

または1つのステップで:

f = ['yes', 'no', 'tot', 'foop\n'] 
p = ['ick', 'throw', 'tamp', 'lap\n'] 

L = [] 

for lst in [f, p]: 
    L.append([item.strip() for item in lst]) 

またはそれより短い:

f = ['yes', 'no', 'tot', 'foop\n'] 
p = ['ick', 'throw', 'tamp', 'lap\n'] 

L = [[item.strip() for item in l] for l in [f, p]] 

あなたの質問には文字通り答えます。あなたはにリストのリストから、具体的ストリップ特定のインデックスが必要な場合:デフォルトでは

L = [['yes', 'no', 'tot', 'foop\n'], ['ick', 'throw', 'tamp', 'lap\n']] 
for lst in L: 
    lst[3] = lst[3].strip() 

を、空白文字からstrip()ストリップは、thisを参照してください。項目から最後の二つのシンボルを除去するためi[3::4]

:リスト使用からこの部分をそれぞれ第四の要素を取得するための

+0

ああ、downvote。説明する気に? –

0
f = ['yes', 'no', 'tot', 'foop\n'] 
p = ['ick', 'throw', 'tamp', 'lap\n'] 
L = [] 
L.append(f) 
L.append(p) 

for i in L: 
    i[3] = i[3].rstrip('\n') 

>>> [['yes', 'no', 'tot', 'foop'], ['ick', 'throw', 'tamp', 'lap']] 
+1

このような結果が得られます。 '' '' '' '' '' '' '' '' '' '' '' '' '' '[' 'はい' '、' tamp ']]] ' –

+0

ええ、私はちょうど実現しました。私は例を修正した。ありがとう。 – user6037143

+1

このコードは問題を解決するのに役立つかもしれませんが、質問に答える_why_および/または_how_を説明しません。この追加の文脈を提供することは、長期的な教育的価値を大幅に改善するだろう。どのような制限や仮定が適用されるかなど、あなたの答えを解説してください。 –

0

x[:-1]

出力スクリプト:

for i in L: 
    i[3::4] = [ x[:-1] for x in i[3::4]] 

結果:

[[ 'はい'、 'いいえ'、 'TOT'、] 'foop'、[ '嫌'、 'スロー'、 'タンピング'、 'ラップ']]

+0

エレガントな解決策:) –

+0

'[3 :: 4]'のポイントは何ですか? – vaultah

+0

'[3 :: 4]'はリストから4番目の要素を返します。 '' ''、 ''、 ''、 ''、 ''、 ''、 ''、 ''、 ''、 '' n '、' lap \ n '] ' – Serbin

1

あなたは削除する場合

>>> f = ['yes', 'no', 'tot', 'foop\n'] 
>>> p = ['ick', 'throw', 'tamp', 'lap\n'] 
>>> [x[0:3]+[x[3].rstrip()] for x in [f]+[p]] 
[['yes', 'no', 'tot', 'foop'], ['ick', 'throw', 'tamp', 'lap']] 

またはmap同等:のみリスト内包と第四のインデックスから新しい行と空白

>>> list(map(lambda x: x[0:3]+[x[3].rstrip()],[f]+[p])) 
[['yes', 'no', 'tot', 'foop'], ['ick', 'throw', 'tamp', 'lap']] 
関連する問題

 関連する問題