Hi..
I have a list as a=[1, 2, 3 .... ] (4 million elements)
and
b=",".join(a)
than
TypeError: sequence item 0: expected string, int found
I want to change list to a=['1','2','3'] but i don't want to use FOR
because my list very very big.
I'm sorry my bad english.
King regards 6 57054
1. Use a generator expression:
b = ",".join(str(i) for i in a)
or
2. Use imap
from itertools import imap
b = ",".join(imap(str, a))
On Oct 15, 4:02 am, Abandoned <best...@gmail.comwrote:
Hi..
I have a list as a=[1, 2, 3 .... ] (4 million elements)
and
b=",".join(a)
than
TypeError: sequence item 0: expected string, int found
I want to change list to a=['1','2','3'] but i don't want to use FOR
because my list very very big.
What is your worry: memory or time? The result string will be very
very very big. What will you do with the result string -- write it to
a file? If so, look at the cPickle module.
Abandoned <be*****@gmail.comwrote:
Hi..
I have a list as a=[1, 2, 3 .... ] (4 million elements)
and
b=",".join(a)
than
TypeError: sequence item 0: expected string, int found
I want to change list to a=['1','2','3'] but i don't want to use FOR
because my list very very big.
I'm sorry my bad english.
King regards
Try b=','.join(map(str, a)) -- it WILL take up some memory (temporarily)
to build the huge resulting string, but there's no real way to avoid
that.
It does run a bit faster than a genexp with for...:
brain:~ alex$ python -mtimeit -s'a=range(4000*1000)'
'b=",".join(map(str,a))'
10 loops, best of 3: 3.37 sec per loop
brain:~ alex$ python -mtimeit -s'a=range(4000*1000)' 'b=",".join(str(x)
for x i
n a)'
10 loops, best of 3: 4.36 sec per loop
Alex
John Machin <sj******@lexicon.netwrote:
>On Oct 15, 4:02 am, Abandoned <best...@gmail.comwrote:
>Hi.. I have a list as a=[1, 2, 3 .... ] (4 million elements) and b=",".join(a) than TypeError: sequence item 0: expected string, int found I want to change list to a=['1','2','3'] but i don't want to use FOR because my list very very big.
What is your worry: memory or time? The result string will be very very very big.
It's an interesting mental exercise to try to figure out just how large
that string will be, without using Python.
I get 30,888,889 bytes...
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
On Tue, 16 Oct 2007 06:18:51 +0000, Tim Roberts wrote:
John Machin <sj******@lexicon.netwrote:
>>On Oct 15, 4:02 am, Abandoned <best...@gmail.comwrote:
>>Hi.. I have a list as a=[1, 2, 3 .... ] (4 million elements) and b=",".join(a) than TypeError: sequence item 0: expected string, int found I want to change list to a=['1','2','3'] but i don't want to use FOR because my list very very big.
What is your worry: memory or time? The result string will be very very very big.
It's an interesting mental exercise to try to figure out just how large
that string will be, without using Python.
I get 30,888,889 bytes...
I think you have an off by one error here. (One number, not one byte) :-)
Ciao,
Marc 'BlackJack' Rintsch
Marc 'BlackJack' Rintsch wrote:
On Tue, 16 Oct 2007 06:18:51 +0000, Tim Roberts wrote:
John Machin <sj******@lexicon.netwrote:
>On Oct 15, 4:02 am, Abandoned <best...@gmail.comwrote: Hi.. I have a list as a=[1, 2, 3 .... ] (4 million elements) and b=",".join(a) than TypeError: sequence item 0: expected string, int found I want to change list to a=['1','2','3'] but i don't want to use FOR because my list very very big.
What is your worry: memory or time? The result string will be very very very big.
It's an interesting mental exercise to try to figure out just how large
that string will be, without using Python.
I get 30,888,889 bytes...
I think you have an off by one error here. (One number, not one byte) :-)
It's certainly off :
[Best viewed in a fixed-width font ... umm, do they still sell squared
paper for doing arithmetic on? I had to rip a page out of a notebook
and rotate it through 90 degrees]
3000001 x 8 = 24000008
0900000 x 7 = 06300000
0090000 x 6 = 00540000
0009000 x 5 = 00045000
0000900 x 4 = 00003600
0000090 x 3 = 00000270
0000009 x 2 = 00000018
------- --------
4000000 30888896
less one for a comma counted above but not used -30888895
difference is 6 bytes which is one number (8) LESS 2 bytes
Cheers,
John This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Peter Ballard |
last post by:
Whew. I hope that title is descriptive!
Hi all,
The python tutorial tells me "It is not safe to modify the sequence
being iterated over in the loop". But what if my list elements are
mutable,...
|
by: Nickolay Kolev |
last post by:
Hi all,
I have a list whose length is a multiple of 3. I want to get a list of
tuples each of which has 3 consecutive elements from the original list,
thus packing the list into smaller...
|
by: yawnmoth |
last post by:
I'm trying to center list elements in a webpage I'm working on, and
setting margin-left to auto for ol (or ul) seems to prevent the number
(or bullet) from displaying in IE6 (strict mode) and...
|
by: abs |
last post by:
Hi all.
My list:
<ul>
<li id="a" onclick="show(this)">Aaaaaaaa</li>
<li id="b" onclick="show(this)">Bbbbbbbb</li>
<li id="c" onclick="show(this)">Cccccccc
<ul>
<li id="d"...
|
by: Eugen J. Sobchenko |
last post by:
Hi!
I'm writing function which swaps two arbitrary elements
of double-linked list. References to the next element of list
must be unique or NULL (even during swap procedure), the same condition...
|
by: nuffnough |
last post by:
This is python 2.4.3 on WinXP under PythonWin.
I have a config file with many blank lines and many other lines that I
don't need.
read the file in, splitlines to make a list, then run a loop...
|
by: psbasha |
last post by:
Hi,
I am trying to assign the list elements(l1) to another list (l2).When I am trying to remove the element from l2,it is removing the element from l1.
Is there any method available to copy the...
|
by: Jezternz |
last post by:
Scriptaculous needs an official forum but does not have one so I am asking here :).
note you will need to be familiar with scriptaculous to help me here.
Basicly I have a script that has a...
|
by: antar2 |
last post by:
Hello
Suppose I have a textfile (text1.txt) with following four words:
Apple
balcony
cartridge
damned
paper
|
by: donpro |
last post by:
Hi,
I have an unordered list that displays for elements horizontally. I'd
like each to take up 25% of the browser window but my code does not
work. Can someone help? The code is shown below.
...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM).
In this month's session, the creator of the excellent VBE...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: Aftab Ahmad |
last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below.
Dim IE As Object
Set IE =...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
| |