-1

I have file with strings "one", "two", etc for all number representations. I want them to be replaced with actual numbers 1, 2, 3 etc. That is, i want a mapping of {"zero", "one", "two", ..., "nine"} to {"0", "1", ... "9"} How can I do that in pythonic way?

Ryne Everett
  • 6,427
  • 3
  • 37
  • 49
Kamalakshi
  • 6,918
  • 3
  • 17
  • 21
  • Its kind of unclear what it is you're looking for, – Games Brainiac Oct 30 '13 at 16:51
  • Yes, i think i should have been more clearer. I have a text file with general text content. There are numbers in word form in between the text, at arbitrary places. These numbers in word form such as "one", "two" etc have to changed to "1", "2" etc. – Kamalakshi Oct 30 '13 at 17:14

2 Answers2

1

Use an associative array, referred to in Python as a "dictionary":

themap={"one":1, "two":2}   # make a dictionary
themap["one"]    # evaluates to the number 1

This will work for any type of data, so, per your question,

themap={"one":"1", "two":"2"}
themap["one"]    # evaluates to the string "1"

To map lots of values at once:

inputs=["one","two"]   # square brackets, so it's an array
themap={"one":1, "two":2}   # braces, so it's a dictionary
map(lambda x: themap[x], inputs)  # evaluates to [1, 2]

The lambda x: themap[x] is a function that looks up items in themap. map() calls that function for each element of inputs and puts the results together as an array. (tested on Python 2.7.3)

cxw
  • 16,685
  • 2
  • 45
  • 81
  • If you're going to use `map`, then that `lambda`'s not necessary - consider instead the methods already available on the `dict`... Consider `map(themap.get, inputs)` for a default of `None` or `map(themap.__getitem__, inputs)` if a `KeyError` should occur. (But using a magic method isn't particulary friendly...) – Jon Clements Oct 30 '13 at 16:54
  • Thanks for the answers. I've accepted this answer as I can modify this to my requirement. But I think I was not clear in stating my question earlier. I have updated it. – Kamalakshi Oct 30 '13 at 17:17
  • @Kama, [this](http://stackoverflow.com/questions/1919096) question relates to replacing different strings (like "one", "two", ...) in a given block of text. Looking at your updated question, I think some of the techniques described there might be helpful to you. – cxw Oct 30 '13 at 17:46
0

A dict will do this job both ways:

st='zero one two three four five six seven eight nine ten'
name2num={s:i for i,s in enumerate(st.split())}
num2name={i:s for i,s in enumerate(st.split())}

print name2num
print num2name
for i, s in enumerate(st.split()):
    print num2name[i], '=>', name2num[s]

Prints:

{'seven': 7, 'ten': 10, 'nine': 9, 'six': 6, 'three': 3, 'two': 2, 'four': 4, 'zero': 0, 'five': 5, 'eight': 8, 'one': 1}
{0: 'zero', 1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five', 6: 'six', 7: 'seven', 8: 'eight', 9: 'nine', 10: 'ten'}
zero => 0
one => 1
two => 2
three => 3
four => 4
five => 5
six => 6
seven => 7
eight => 8
nine => 9
ten => 10

You can also use a class:

class Nums:
    zero=0
    one=1
    two=2
    three=3
    # etc

print Nums.zero
# 0
print Nums.one     
# 1
print getattr(Nums, 'two')
# 2

Or, another way to use a class enumeration:

class Nums2:
    pass

for i, s in enumerate(st.split()):
    setattr(Nums2, s, i)        

for s in st.split():
    print getattr(Nums2,s)  
# prints 0-10...

Or wait for Python 3.4 and the implementation of the Enum type described in PEP 435

dawg
  • 98,345
  • 23
  • 131
  • 206