473,382 Members | 1,375 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,382 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 57077
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. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.