thanks, i hadn&#39;t mapped that concept onto what i saw yet.<br><br><div class="gmail_quote">On Mon, Jun 16, 2008 at 12:22 AM, Isaac &lt;<a href="mailto:hyperneato@gmail.com">hyperneato@gmail.com</a>&gt; wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
it is a list comprehension.<br><br><a href="http://docs.python.org/tut/node7.html#SECTION007140000000000000000" target="_blank">http://docs.python.org/tut/node7.html#SECTION007140000000000000000</a><div><div></div><div class="Wj3C7c">
<br><br><div class="gmail_quote">On Mon, Jun 16, 2008 at 7:18 AM, Seth Friedman &lt;<a href="mailto:sfseth@gmail.com" target="_blank">sfseth@gmail.com</a>&gt; wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">ok i&#39;m a python newb, but can someone answer the following syntax question that&#39;s been bugging me:<br>

<br>the following line<div><br><br>&nbsp; &nbsp; &nbsp; temp = [try_int_convert(x) &nbsp;for x in line.strip().split(&#39; &nbsp; &nbsp; &nbsp; &nbsp;&#39;)]<br>
<br></div>what is temp = [&nbsp; ...whatever ... ]&nbsp; doing?<br><br>i&#39;m used to [ .. ] as array syntax, clearly i&#39;m missing some capability here.&nbsp;&nbsp;&nbsp; what does python code enclosed in square brackets do?&nbsp;&nbsp;&nbsp; i could guess but seems like i might get a stronger answer from the experts.<br>

<font color="#888888">
<br>~seth</font><div><div></div><div><br><br><br><div class="gmail_quote">On Mon, Jun 16, 2008 at 12:08 AM, Al Nevarez &lt;<a href="mailto:anevare2@yahoo.com" target="_blank">anevare2@yahoo.com</a>&gt; wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Thanks for the tips everyone.<br>
I&#39;m running an older version of Python.. but the following combination of your suggestions worked just fine in yielding a list of lists with integers and strings preserved.<br>
<br>
my_data[]<br>
data_file=&#39;datasource.txt&#39;<br>
<div><br>
def try_int_convert(sval):<br>
 &nbsp; &nbsp;try:<br>
 &nbsp; &nbsp; &nbsp; &nbsp;return int(sval)<br>
 &nbsp; &nbsp;except ValueError:<br>
 &nbsp; &nbsp; &nbsp; &nbsp;return sval<br>
<br>
</div>for line in open(data_file):<br>
 &nbsp; &nbsp; &nbsp; temp = [try_int_convert(x) &nbsp;for x in line.strip().split(&#39; &nbsp; &nbsp; &nbsp; &nbsp;&#39;)]<br>
 &nbsp; &nbsp; &nbsp; my_data.append(temp)<br>
<br>
<br>
Works fine when the value is blank in the original tab delimited file too (must be a tab there of course). &nbsp;Seems to work perfectly, but does anybody spot any issue?<br>
<br>
<br>
Al<br>
<br>
<br>
<br>
--- On Sun, 6/15/08, Brent Pedersen &lt;<a href="mailto:bpederse@gmail.com" target="_blank">bpederse@gmail.com</a>&gt; wrote:<br>
<br>
&gt; From: Brent Pedersen &lt;<a href="mailto:bpederse@gmail.com" target="_blank">bpederse@gmail.com</a>&gt;<br>
&gt; Subject: Re: [Baypiggies] Preserving integers when loading a file into a list<br>
&gt; To: &quot;Jason Culverhouse&quot; &lt;<a href="mailto:jason@mischievous.org" target="_blank">jason@mischievous.org</a>&gt;<br>
&gt; Cc: <a href="mailto:baypiggies@python.org" target="_blank">baypiggies@python.org</a><br>
&gt; Date: Sunday, June 15, 2008, 1:46 PM<br>
<div><div></div><div>&gt; On Sun, Jun 15, 2008 at 12:31 PM, Jason Culverhouse<br>
&gt; &lt;<a href="mailto:jason@mischievous.org" target="_blank">jason@mischievous.org</a>&gt; wrote:<br>
&gt; &gt; not x.isdigit() and x or int(x) is going to fail for<br>
&gt; empty string... &#39;&#39;<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp;not False and &#39;&#39; or int(&#39;&#39;)<br>
&gt; &lt;-value error since<br>
&gt; &gt;<br>
&gt; &gt; You could combine Adam&#39;s try_int_convert<br>
&gt; &gt;<br>
&gt; &gt; import csv<br>
&gt; &gt; import functools<br>
&gt; &gt; import sys<br>
&gt; &gt;<br>
&gt; &gt; i = functools.partial(map, try_int_convert) # maybe<br>
&gt; convert a list to int<br>
&gt; &gt; t = functools.partial(tuple) # convert to tuple, not<br>
&gt; sure if you need the in<br>
&gt; &gt; tuples<br>
&gt; &gt;<br>
&gt; &gt; #Read a TSV file from stdin and convert<br>
&gt; &gt; [t(i(line)) for line in csv.reader(sys.stdin,<br>
&gt; dialect=&#39;excel-tab&#39;)]<br>
&gt; &gt;<br>
&gt; &gt; Jason<br>
&gt;<br>
&gt; good point. out of curiosity, how is t =<br>
&gt; functools.partial(tuple),<br>
&gt; then using t() different from using tuple() directly?<br>
&gt; _______________________________________________<br>
&gt; Baypiggies mailing list<br>
&gt; <a href="mailto:Baypiggies@python.org" target="_blank">Baypiggies@python.org</a><br>
&gt; To change your subscription options or unsubscribe:<br>
&gt; <a href="http://mail.python.org/mailman/listinfo/baypiggies" target="_blank">http://mail.python.org/mailman/listinfo/baypiggies</a><br>
<br>
<br>
<br>
_______________________________________________<br>
Baypiggies mailing list<br>
<a href="mailto:Baypiggies@python.org" target="_blank">Baypiggies@python.org</a><br>
To change your subscription options or unsubscribe:<br>
<a href="http://mail.python.org/mailman/listinfo/baypiggies" target="_blank">http://mail.python.org/mailman/listinfo/baypiggies</a><br>
</div></div></blockquote></div><br>
</div></div><br>_______________________________________________<br>
Baypiggies mailing list<br>
<a href="mailto:Baypiggies@python.org" target="_blank">Baypiggies@python.org</a><br>
To change your subscription options or unsubscribe:<br>
<a href="http://mail.python.org/mailman/listinfo/baypiggies" target="_blank">http://mail.python.org/mailman/listinfo/baypiggies</a><br></blockquote></div><br>
</div></div></blockquote></div><br>