´ð¸´: how to remove \n in the list

P

Penny Y.

lines[:] = [line.rstrip('\n') for line in lines]

why not just:

lines = [line.rstrip('\n') for line in lines]


what's the difference between lines[:] and lines here? Thanks.

-----ÓʼþÔ­¼þ-----
·¢¼þÈË: [email protected]
[mailto:p[email protected]] ´ú±í Gabriel
Genellina
·¢ËÍʱ¼ä: 2008Äê4ÔÂ14ÈÕ 12:59
ÊÕ¼þÈË: (e-mail address removed)
Ö÷Ìâ: Re: how to remove \n in the list

En Mon, 14 Apr 2008 01:41:55 -0300, reetesh nigam
hi,
l=['5\n', '2\n', '7\n', '3\n', '6\n']

how to remove \n from the given list

l is is very poor name... I'll use lines instead:

lines[:] = [line.rstrip('\n') for line in lines]
 
E

Eric Brunel

(please avoid top-posting... corrected)
-----邮件原件-----
å‘件人: [email protected]
[mailto:p[email protected]] 代表 Gabriel
Genellina
å‘é€æ—¶é—´: 2008å¹´4月14æ—¥ 12:59
收件人: (e-mail address removed)
主题: Re: how to remove \n in the list

En Mon, 14 Apr 2008 01:41:55 -0300, reetesh nigam
hi,
l=['5\n', '2\n', '7\n', '3\n', '6\n']

how to remove \n from the given list

l is is very poor name... I'll use lines instead:

lines[:] = [line.rstrip('\n') for line in lines]
---

why not just:

lines = [line.rstrip('\n') for line in lines]


what's the difference between lines[:] and lines here? Thanks.
Compare:
lines = ['5\n', '2\n', '7\n', '3\n', '6\n']
lines2 = lines
lines = [line.rstrip('\n') for line in lines]
lines ['5', '2', '7', '3', '6']
lines2
['5\n', '2\n', '7\n', '3\n', '6\n']

with:
lines = ['5\n', '2\n', '7\n', '3\n', '6\n']
lines2 = lines
lines[:] = [line.rstrip('\n') for line in lines]
lines ['5', '2', '7', '3', '6']
lines2
['5', '2', '7', '3', '6']

Assigning to lines[:] changes the original list. Assigning to lines
rebinds the name to the result of the list comprehension, but doesn't
affect the original list.

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top