<br>I have the transFn function as follows:<br><br>def transFn(translatefile):<br>&nbsp;&nbsp;&nbsp; transfile = open(translatefile, &#39;r&#39;)<br>&nbsp;&nbsp;&nbsp; records = transfile.read()<br>&nbsp;&nbsp;&nbsp; transfile.close()<br>&nbsp;&nbsp;&nbsp; lines = records.split()<br>
&nbsp;&nbsp;&nbsp; transDict = {}<br>&nbsp;&nbsp;&nbsp; for line in lines:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; key, value = line.split(&#39;,&#39;)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; transDict[key] = value<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; for key, value in data.items():<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; data[key] = [ x for x in (transDict.get
(i, i) for i in value) if x is not None]<br><br>my original data is:<br><br>data = {&#39;1&#39;: [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;], &#39;3&#39;: [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;], &#39;2&#39;: [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;], &#39;4&#39;: [&#39;a&#39;, &#39;c&#39;]}
<br><br>my transDict is:<br><br>transDict = {&#39;a&#39;: &#39;1&#39;, &#39;b&#39;: &#39;2&#39;}<br><br>However when I run transFn my data is:<br><br>data = {&#39;1&#39;: [&#39;1&#39;, &#39;2&#39;, &#39;c&#39;], &#39;3&#39;: [&#39;1&#39;, &#39;2&#39;, &#39;c&#39;], &#39;2&#39;: [&#39;1&#39;, &#39;2&#39;, &#39;c&#39;], &#39;4&#39;: [&#39;1&#39;, &#39;c&#39;]}
<br><br>I was expecting:<br><br>{&#39;1&#39;: [&#39;1&#39;, &#39;2&#39;], &#39;3&#39;: [&#39;1&#39;, &#39;2&#39;], &#39;2&#39;: [&#39;1&#39;, &#39;2&#39;], &#39;4&#39;: [&#39;1&#39;]}<br><br>I will see if I can work with a remove command??
<br><br>M.<br><br><div><span class="gmail_quote">On 10/2/07, <b class="gmail_sendername">Kent Johnson</b> &lt;<a href="mailto:kent37@tds.net">kent37@tds.net</a>&gt; wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
GTXY20 wrote:<br>&gt; Here&#39;s an interesting question:<br>&gt;<br>&gt; Can I use the transFn function to remove items in the value list.<br>&gt;<br>&gt; Can this be done by simple assigning the current value a value of null
<br>&gt; in the translate file?<br><br>No, that will make the translated value be None (I guess that is what<br>you mean by null). You could then filter for these, for example<br><br>&nbsp;&nbsp;&nbsp;&nbsp; for key, value in Data.items():<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Data[key] = [ transDict.get(i, i) for i in value if<br>transDict.get(i, i) is not None]<br><br>If you want to avoid double-fetching from transDict (if Data is huge<br>this might matter) then you could write out the loop or possibly use
<br>something like<br>&nbsp;&nbsp; Data[key] = [ x for x in (transDict.get(i, i) for i in value) if x is<br>not None]<br><br>which makes an intermediate generator and filters that.<br><br>Kent<br><br>&gt;<br>&gt; M.<br>&gt;<br>&gt;
<br>&gt;<br>&gt; On 10/2/07, GTXY20 &lt;<a href="mailto:gtxy20@gmail.com">gtxy20@gmail.com</a>&gt; wrote:<br>&gt;&gt; I adjusted so that I get the following so if I do not need to translate a<br>&gt;&gt; dictionary I do not call the function transFn:
<br>&gt;&gt;<br>&gt;&gt; def transFn(translatefile):<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; transfile = open(translatefile, &#39;r&#39;)<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; records = transfile.read()<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; transfile.close()<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; lines = records.split
()<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; transDict = {}<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; for line in lines:<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; key, value = line.split(&#39;,&#39;)<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; transDict[key] = value<br>&gt;&gt;<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; for key, value in Data.items
():<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Data[key] = [ transDict.get(i, i) for i in value ]<br><br></blockquote></div><br>