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 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))
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: 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...
|
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...
|
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...
|
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,...
|
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$) {
}
...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
| |