【问题标题】:How do I iterate through each string in a list and modify it?如何遍历列表中的每个字符串并对其进行修改?
【发布时间】:2022-01-12 16:20:38
【问题描述】:

我正在尝试遍历 nums 的每个索引并过滤所述索引中的任何 excepted_words 实例。该程序的输出似乎几乎没有修改,如果有的话。我该如何解决这个问题?

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, '')

输出:

['-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',]

【问题讨论】:

  • e 已经是character 级别,所以在if 行中,for char in e 没有意义

标签: python list loops for-loop


【解决方案1】:

问题在于excepted_chars 是一个列表,所以in excepted_chars 期望它恰好是'ABCDEFGHIJKLMNOPQRSTUVWXYZabdefghijklmnopqrstuvwxyz-_!?'。把它变成一个字符串:

excepted_chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabdefghijklmnopqrstuvwxyz-_!?'

此外,如果您想删除非数值,请使用 .isdigit 与列表解析或 map 函数:

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

或者

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

【讨论】:

    【解决方案2】:

    使用列表解析更容易重建nums。还要注意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 ']
    

    【讨论】:

      【解决方案3】:

      您可以通过将例外字符字符串转换为集合来提高性能(尽管不是问题的一部分)。然后,您可以像这样实现您的目标:

      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)
      

      当然,newnums 可以用更复杂的列表理解来构造,但我已将其分解为,希望更容易理解

      【讨论】:

        【解决方案4】:

        你可以使用正则表达式来提取数字

        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))
        

        【讨论】:

          【解决方案5】:

          您可以使用 translate 从字符串中删除字符列表。要“就地”更新列表,您可以使用理解/迭代器分配其完整的下标范围:

          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 ']
          

          “就地”赋值仅在您有其他变量引用同一列表时才有用。如果没有,您应该使用列表推导简单地分配新内容:

          nums = [s.translate(cleanUp) for s in nums]
          

          【讨论】:

            猜你喜欢
            • 2014-01-24
            • 2015-12-17
            • 1970-01-01
            • 2021-01-08
            • 2022-01-16
            • 1970-01-01
            • 2020-01-12
            • 2021-12-15
            • 2010-09-09
            相关资源
            最近更新 更多