<br>I have the transFn function as follows:<br><br>def transFn(translatefile):<br> transfile = open(translatefile, 'r')<br> records = transfile.read()<br> transfile.close()<br> lines = records.split()<br>
transDict = {}<br> for line in lines:<br> key, value = line.split(',')<br> transDict[key] = value<br> <br> for key, value in data.items():<br> 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 = {'1': ['a', 'b', 'c'], '3': ['a', 'b', 'c'], '2': ['a', 'b', 'c'], '4': ['a', 'c']}
<br><br>my transDict is:<br><br>transDict = {'a': '1', 'b': '2'}<br><br>However when I run transFn my data is:<br><br>data = {'1': ['1', '2', 'c'], '3': ['1', '2', 'c'], '2': ['1', '2', 'c'], '4': ['1', 'c']}
<br><br>I was expecting:<br><br>{'1': ['1', '2'], '3': ['1', '2'], '2': ['1', '2'], '4': ['1']}<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> <<a href="mailto:kent37@tds.net">kent37@tds.net</a>> 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>> Here's an interesting question:<br>><br>> Can I use the transFn function to remove items in the value list.<br>><br>> Can this be done by simple assigning the current value a value of null
<br>> 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> for key, value in Data.items():<br>
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> 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>><br>> M.<br>><br>>
<br>><br>> On 10/2/07, GTXY20 <<a href="mailto:gtxy20@gmail.com">gtxy20@gmail.com</a>> wrote:<br>>> I adjusted so that I get the following so if I do not need to translate a<br>>> dictionary I do not call the function transFn:
<br>>><br>>> def transFn(translatefile):<br>>> transfile = open(translatefile, 'r')<br>>> records = transfile.read()<br>>> transfile.close()<br>>> lines = records.split
()<br>>> transDict = {}<br>>> for line in lines:<br>>> key, value = line.split(',')<br>>> transDict[key] = value<br>>><br>>> for key, value in Data.items
():<br>>> Data[key] = [ transDict.get(i, i) for i in value ]<br><br></blockquote></div><br>