Re: May I drop list bracket from list?

2015-04-23 Thread Dave Angel

On 04/23/2015 06:11 AM, subhabrata.bane...@gmail.com wrote:

Dear Group,

I am trying to read a list of files as
list_of_files = glob.glob('C:\Python27\*.*')
Now I am trying to read each one of them,
convert into list of words, and append to a list
as.

list1=[]
for file in list_of_files:
   print file
   fread1=open(file,"r").read()
   fword=fread1.split()
   list1.append(fword)

Here the list is a list of lists, but I want only one list not
list of lists.

I was thinking of stripping it as, str(list1).strip('[]')

but in that case it would be converted to string.

Is there a way to do it. I am using Python27 on Windows7 Professional.
Apology for an indentation error.

If anybody may please suggest.



You're first problem is the name of your variable.  fword implies it's a 
string, but it's really a list.  So when you do:

 list1.append(fword)

you're appending a list to a list, which gives you nested lists.  Sounds 
like you want

 list1.extend(fword)



--
DaveA
--
https://mail.python.org/mailman/listinfo/python-list


Re: May I drop list bracket from list?

2015-04-23 Thread Steven D'Aprano
On Thu, 23 Apr 2015 09:12 pm, Jean-Michel Pichavant wrote:

> If both list1 and fword are lists, you can also write
> 
> list1 = list1 + fword
> or
> list1 += fword


You can, but you shouldn't since it risks being very slow, especially the
first version. Repeated list addition has quadratic behaviour. It's okay if
you only add a few lists, but if there are thousands or millions of them,
it will be horribly slow. Using extend guarantees to modify the list in
place and avoid unnecessary copying of data.


-- 
Steven

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: May I drop list bracket from list?

2015-04-23 Thread Peter Otten
Jean-Michel Pichavant wrote:

> - Original Message -
>> From: "Peter Otten" <__pete...@web.de>
>> To: python-list@python.org
>> Sent: Thursday, 23 April, 2015 12:26:41 PM
>> Subject: Re: May I drop list bracket from list?
>> 
>> subhabrata.bane...@gmail.com wrote:
>> 
>> > Dear Group,
>> > 
>> > list1=[]
>> > for file in list_of_files:
>> >   print file
>> >   fread1=open(file,"r").read()
>> >   fword=fread1.split()
>> >   list1.append(fword)
>> > 
>> > Here the list is a list of lists, but I want only one list not
>> > list of lists.
>> There is also a dedicated extend() method that takes a list (actually
>> an
>> iterable) and appends all items in that list:
>> 
>> list1.extend(fword)
> 
> If both list1 and fword are lists, you can also write
> 
> list1 = list1 + fword
> or
> list1 += fword

Yes, the can of worms is bottomless ;)

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: May I drop list bracket from list?

2015-04-23 Thread Jean-Michel Pichavant
- Original Message -
> From: "Peter Otten" <__pete...@web.de>
> To: python-list@python.org
> Sent: Thursday, 23 April, 2015 12:26:41 PM
> Subject: Re: May I drop list bracket from list?
> 
> subhabrata.bane...@gmail.com wrote:
> 
> > Dear Group,
> > 
> > list1=[]
> > for file in list_of_files:
> >   print file
> >   fread1=open(file,"r").read()
> >   fword=fread1.split()
> >   list1.append(fword)
> > 
> > Here the list is a list of lists, but I want only one list not
> > list of lists.
> There is also a dedicated extend() method that takes a list (actually
> an
> iterable) and appends all items in that list:
> 
> list1.extend(fword)

If both list1 and fword are lists, you can also write

list1 = list1 + fword
or
list1 += fword

JM


-- IMPORTANT NOTICE: 

The contents of this email and any attachments are confidential and may also be 
privileged. If you are not the intended recipient, please notify the sender 
immediately and do not disclose the contents to any other person, use it for 
any purpose, or store or copy the information in any medium. Thank you.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: May I drop list bracket from list?

2015-04-23 Thread Ben Bacarisse
subhabrata.bane...@gmail.com writes:

> I am trying to read a list of files as 
> list_of_files = glob.glob('C:\Python27\*.*')
> Now I am trying to read each one of them, 
> convert into list of words, and append to a list
> as.
>
> list1=[]
> for file in list_of_files:
>   print file
>   fread1=open(file,"r").read()
>   fword=fread1.split()
>   list1.append(fword)
>
> Here the list is a list of lists, but I want only one list not 
> list of lists.

You probably want list.extend(fword) here.  Python's ternimology is a
little quirky here -- in some other languages (ntable the mother of all
list langiages, Lisp), the append function does what Python's extend
does.


-- 
Ben.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: May I drop list bracket from list?

2015-04-23 Thread Peter Otten
subhabrata.bane...@gmail.com wrote:

> On Thursday, April 23, 2015 at 3:57:28 PM UTC+5:30, Peter Otten wrote:
>> 
>> 
>> > Dear Group,
>> > 
>> > I am trying to read a list of files as
>> > list_of_files = glob.glob('C:\Python27\*.*')
>> > Now I am trying to read each one of them,
>> > convert into list of words, and append to a list
>> > as.
>> > 
>> > list1=[]
>> > for file in list_of_files:
>> >   print file
>> >   fread1=open(file,"r").read()
>> >   fword=fread1.split()
>> >   list1.append(fword)
>> > 
>> > Here the list is a list of lists, but I want only one list not
>> > list of lists.
>> > 
>> > I was thinking of stripping it as, str(list1).strip('[]')
>> > 
>> > but in that case it would be converted to string.
>> > 
>> > Is there a way to do it. I am using Python27 on Windows7 Professional.
>> > Apology for an indentation error.
>> > 
>> > If anybody may please suggest.
>> 
>> You have to understand that the append() method always appends a single
>> item to the list, be that a string or a list or whatever. If you want to
>> append words in a list to the list the logical approach is therefore to
>> loop over the words and invoke append for every word
>> 
>> for word in fword:
>> list1.append(word)
>> 
>> There is also a dedicated extend() method that takes a list (actually an
>> iterable) and appends all items in that list:
>> 
>> list1.extend(fword)
> 
> Thanks Peter. I tried an example,
>>> ll = [['a'], ['b'], ['c']]
 l = [x for y in ll for x in y]
> 
> I would try your one,too.
> 
> Regards,
> Subhabrata Banerjee.

The expression

[x for y in ll for x in y]

is called "list comprehension" and is syntactic sugar for

l = []
for y in ll:
for x in y:
l.append(x)

I suggest that you forget about list comprehensions until you are 
comfortable with the traditional approach that uses for loops. Once you have 
understood that the translation into list comprehensions in situations where 
it looks better is mechanical.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: May I drop list bracket from list?

2015-04-23 Thread subhabrata . banerji
On Thursday, April 23, 2015 at 3:57:28 PM UTC+5:30, Peter Otten wrote:
> 
> 
> > Dear Group,
> > 
> > I am trying to read a list of files as
> > list_of_files = glob.glob('C:\Python27\*.*')
> > Now I am trying to read each one of them,
> > convert into list of words, and append to a list
> > as.
> > 
> > list1=[]
> > for file in list_of_files:
> >   print file
> >   fread1=open(file,"r").read()
> >   fword=fread1.split()
> >   list1.append(fword)
> > 
> > Here the list is a list of lists, but I want only one list not
> > list of lists.
> > 
> > I was thinking of stripping it as, str(list1).strip('[]')
> > 
> > but in that case it would be converted to string.
> > 
> > Is there a way to do it. I am using Python27 on Windows7 Professional.
> > Apology for an indentation error.
> > 
> > If anybody may please suggest.
> 
> You have to understand that the append() method always appends a single item 
> to the list, be that a string or a list or whatever. If you want to append 
> words in a list to the list the logical approach is therefore to loop over 
> the words and invoke append for every word
> 
> for word in fword:
> list1.append(word)
> 
> There is also a dedicated extend() method that takes a list (actually an 
> iterable) and appends all items in that list:
> 
> list1.extend(fword)

Thanks Peter. I tried an example,
>> ll = [['a'], ['b'], ['c']]
>>> l = [x for y in ll for x in y]

I would try your one,too.

Regards,
Subhabrata Banerjee. 

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: May I drop list bracket from list?

2015-04-23 Thread Peter Otten
subhabrata.bane...@gmail.com wrote:

> Dear Group,
> 
> I am trying to read a list of files as
> list_of_files = glob.glob('C:\Python27\*.*')
> Now I am trying to read each one of them,
> convert into list of words, and append to a list
> as.
> 
> list1=[]
> for file in list_of_files:
>   print file
>   fread1=open(file,"r").read()
>   fword=fread1.split()
>   list1.append(fword)
> 
> Here the list is a list of lists, but I want only one list not
> list of lists.
> 
> I was thinking of stripping it as, str(list1).strip('[]')
> 
> but in that case it would be converted to string.
> 
> Is there a way to do it. I am using Python27 on Windows7 Professional.
> Apology for an indentation error.
> 
> If anybody may please suggest.

You have to understand that the append() method always appends a single item 
to the list, be that a string or a list or whatever. If you want to append 
words in a list to the list the logical approach is therefore to loop over 
the words and invoke append for every word

for word in fword:
list1.append(word)

There is also a dedicated extend() method that takes a list (actually an 
iterable) and appends all items in that list:

list1.extend(fword)


-- 
https://mail.python.org/mailman/listinfo/python-list


May I drop list bracket from list?

2015-04-23 Thread subhabrata . banerji
Dear Group, 

I am trying to read a list of files as 
list_of_files = glob.glob('C:\Python27\*.*')
Now I am trying to read each one of them, 
convert into list of words, and append to a list
as.

list1=[]
for file in list_of_files:
  print file
  fread1=open(file,"r").read()
  fword=fread1.split()
  list1.append(fword)

Here the list is a list of lists, but I want only one list not 
list of lists.

I was thinking of stripping it as, str(list1).strip('[]')

but in that case it would be converted to string.

Is there a way to do it. I am using Python27 on Windows7 Professional.
Apology for an indentation error. 

If anybody may please suggest. 

Regards,
Subhabrata Banerjee. 




  

  
-- 
https://mail.python.org/mailman/listinfo/python-list