Home > Software design >  How do I iterate through each string in a list and modify it?
How do I iterate through each string in a list and modify it?

Time:12-08

I'm trying to iterate through each index of nums and filter any instances of excepted_words in said index. The output of this program seems to make little modification, if any at all. How do I fix this?

nums = ['-634-2385 BI', '-638-9255 Br', '.789-2936 Br', '785-2141 Br', '785-7424 Br', '634-8122 Bri', '783-2642 Br', '783-2012 !', '634-2060 Br']
excepted_chars = ['ABCDEFGHIJKLMNOPQRSTUVWXYZabdefghijklmnopqrstuvwxyz-_!?']

for i in nums.copy():
    for e in i:
        if any(char in excepted_chars for char in e):
            nums[nums.index(i)] = nums[nums.index(i)].replace(e, '')

Output:

['-634-2385 BI', '-638-9255 Br', '.789-2936 Br', '785-2141 Br', '785-7424 Br', '634-8122 Bri', '783-2642 Br', '783-2012 !', '634-2060 Br',]

CodePudding user response:

It's easier to rebuild nums with a list comprehension. Note also the extra level of indirection in excepted_chars since it's a list of a single string:

>>> nums = ['-634-2385 BI', '-638-9255 Br', '.789-2936 Br', '785-2141 Br', '785-7424 Br', '634-8122 Bri', '783-2642 Br', '783-2012 !', '634-2060 Br']
>>> excepted_chars = ['ABCDEFGHIJKLMNOPQRSTUVWXYZabdefghijklmnopqrstuvwxyz-_!?']
>>> nums = [''.join(char for char in i if char not in excepted_chars[0]) for i in nums]
>>> nums
['6342385 ', '6389255 ', '.7892936 ', '7852141 ', '7857424 ', '6348122 ', '7832642 ', '7832012 ', '6342060 ']

CodePudding user response:

You can improve performance (albeit not part of the question) by converting your string of excepted characters to a set. Then, you can achieve your objective like this:

nums = ['-634-2385 BI', '-638-9255 Br', '.789-2936 Br', '785-2141 Br',
        '785-7424 Br', '634-8122 Bri', '783-2642 Br', '783-2012 !', '634-2060 Br']
excepted_chars = set('ABCDEFGHIJKLMNOPQRSTUVWXYZabdefghijklmnopqrstuvwxyz-_!?')
newnums = []
for n in nums:
    w = [c for c in n if c not in excepted_chars]
    newnums.append(''.join(w))
print(newnums)

Of course, newnums could be constructed with a more complex list comprehension but I've broken it down to, hopefully, make it easier to understand

CodePudding user response:

The problem is that excepted_chars is a list so in excepted_chars expects it to be exactly 'ABCDEFGHIJKLMNOPQRSTUVWXYZabdefghijklmnopqrstuvwxyz-_!?'. make it a string:

excepted_chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabdefghijklmnopqrstuvwxyz-_!?'

Also if you want to remove non numerical values use .isdigit with a list comprehension or the map function:

nums = list(map(lambda x: ''.join(c for c in x if c.isdigit()), nums))

Or

nums = [''.join(c for c in i if c.isdigit()) for i in nums]

CodePudding user response:

you can use regular expressions to extract numbers

nums = ['-634-2385 BI', '-638-9255 Br', '.789-2936 Br', '785-2141 Br', '785-7424 Br', '634-8122 Bri', '783-2642 Br', '783-2012 !', '634-2060 Br']
for i,num in enumerate(nums):
    print(num)
    nums[i] = "".join(re.findall(r'[0-9.] ',num))
  • Related