Home>
Thing I want to do

When there is a list of addresses with 〒 and spaces, I want to convert it to a list with 〒 and spaces removed.

Example

exampleList = [[1,'〒245-0067 Address Example 1'], [2,'〒245-0067 Address Example 2']]
For example, when there is such a list
exampleList = normalization (exampleList)
I would like to convert it to the list [[1,'Address example 1'], [2,'Address example 2']].

What I tried

I tried to build a function like this, but when I process [[1,'〒245-0067 address example 1'], [2,'〒245-0067 address example 2']],
[[1,'〒245-0067 Address Example 1'], [2,'〒245-0067 Address Example 2']] will be returned as it is.

def normalization (addresses):
    for address in addresses:
        for item in address:
            item = str (item) .replace (",", "")
            item = re.sub ('〒 ...-....','', item)
            item = re.sub ('〒 .......','', item)
            item = item.replace ("", "")
    return addresses
Questions

I'm very sorry for the introductory question, but I would be very grateful if you could teach me how to fix the code.

  • Answer # 1

    If you use the original code,

    def normalization (addresses):
        a = []
        for address in addresses:
            b = []
            for item in address:
                item = str (item) .replace (",", "")
                item = re.sub ('〒 ...-....','', item)
                item = re.sub ('〒 .......','', item)
                item = item.replace ("", "")
                b.append (item)
            a.append (b)
        return a

  • Answer # 2

    The question code just returns the arguments as they are, so I think you should change it as follows.

    def normalization (addresses):
        result = []
        for address in addresses:
            for item in address:
                item = str (item) .replace (",", "")
                item = re.sub ('〒 ...-....','', item)
                item = re.sub ('〒 .......','', item)
                item = item.replace ("", "")
                result.append (item)
        return result