473,289 Members | 1,963 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

int to str in list elements..

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

Oct 14 '07 #1
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))
Oct 14 '07 #2
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.

Oct 14 '07 #3
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
Oct 15 '07 #4
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.
Oct 16 '07 #5
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
Oct 16 '07 #6

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

Oct 16 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
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,...
8
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...
3
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...
33
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"...
12
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...
5
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...
2
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...
1
Jezternz
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...
1
by: antar2 | last post by:
Hello Suppose I have a textfile (text1.txt) with following four words: Apple balcony cartridge damned paper
1
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. ...
2
isladogs
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...
0
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...
0
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...
0
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 =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
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...
1
isladogs
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...
0
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...
0
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.