[英]How do I iterate through each string in a list and modify it?
I'm trying to iterate through each index of nums and filter any instances of excepted_words in said index.我正在尝试遍历 nums 的每个索引并过滤所述索引中的任何 excepted_words 实例。 The output of this program seems to make little modification, if any at all.
这个程序的 output 似乎几乎没有修改,如果有的话。 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: 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',]
It's easier to rebuild nums
with a list comprehension.使用列表理解来重建
nums
更容易。 Note also the extra level of indirection in excepted_chars
since it's a list of a single string:还要注意
excepted_chars
中的额外间接级别,因为它是单个字符串的列表:
>>> 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 ']
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当然, newnums可以用更复杂的列表理解来构建,但我已经把它分解成,希望它更容易理解
The problem is that excepted_chars
is a list so in excepted_chars
expects it to be exactly 'ABCDEFGHIJKLMNOPQRSTUVWXYZabdefghijklmnopqrstuvwxyz-_??'
问题是
excepted_chars
是一个列表,因此in excepted_chars
期望它完全是'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:此外,如果要删除非数值,请使用带有列表理解的
.isdigit
或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]
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))
You can use translate to remove a list of characters from a string.您可以使用 translate 从字符串中删除字符列表。 To update the list "in-place" you can assign its full subscript range with a comprehension/iterator:
要“就地”更新列表,您可以使用理解/迭代器分配其完整的下标范围:
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-_!?'
cleanUp = str.maketrans('','', excepted_chars) # 3rd parameter deletes
nums[:] = (s.translate(cleanUp) for s in nums) # assign back "in-place"
print(nums)
['6342385 ', '6389255 ', '.7892936 ', '7852141 ', '7857424 ', '6348122 ',
'7832642 ', '7832012 ', '6342060 ']
The "in-place" assignment is only useful if you have other variables referencing the same list. “就地”分配仅在您有其他变量引用同一列表时才有用。 If not, you should simply assign the new content using a list comprehension:
如果没有,您应该使用列表推导简单地分配新内容:
nums = [s.translate(cleanUp) for s in nums]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.