【问题标题】:Search for a partial string in an array of strings在字符串数组中搜索部分字符串
【发布时间】:2016-08-25 17:07:52
【问题描述】:

在数组中搜索部分字符串然后从数组中删除该字符串的 Pythonic/快速方法是什么?
(我可以用一个简单的循环和 IF IN 来完成,并在循环中重建两个数组,询问是否有 Pythonic 方式/功能来做到这一点)

例子:

array = ['rule1','rule2','exception[type_a]','rule3','exception[type_b]']
res(,)=remove_exceptions(array,'exception')
print(res[0]) >>> ['rule1','rule2','rule3']
print(res[1]) >>> ['exception[type_a]','exception[type_b]']

【问题讨论】:

  • 您只想删除它们还是从您的列表中提取它们?
  • 遍历您的列表并在循环套件中创建两个新列表是 Pythonic。如果列表元素与您表示的一样,则可以使用str.startswith 方法。
  • @Kasramvd 要么对我有用
  • @ItayMoav-Malimovka 看看我的回答。
  • @Kasramvd 虽然这是我目前(几乎)使用的,但我的问题是是否有内置的函数/语言构造(Pythonic 方式)可以更有效地执行此操作(文本更少/更快) .

标签: python arrays python-3.x


【解决方案1】:
>>> [x for x in array if 'exception' not in x]
['rule1', 'rule2', 'rule3']
>>> [x for x in array if 'exception' in x]
['exception[type_a]', 'exception[type_b]']

另请参阅:Python: split a list based on a condition?

【讨论】:

    【解决方案2】:

    如果你想分离你的项目,你可以通过一个循环来做到这一点,并将项目保存在字典中:

    >>> d = {}
    >>> for i in array:
    ...     if 'exception' in i:
    ...         d.setdefault('exception', []).append(i)
    ...     else:
    ...         d.setdefault('other', []).append(i)
    ... 
    >>> 
    >>> d
    {'exception': ['exception[type_a]', 'exception[type_b]'], 'other': ['rule1', 'rule2', 'rule3']}
    

    您可以通过调用字典的值来访问分隔的项目:

    >>> d.values()
    [['exception[type_a]', 'exception[type_b]'], ['rule1', 'rule2', 'rule3']]
    

    【讨论】:

      【解决方案3】:

      说真的,只需使用 for 循环,您正在尝试创建 两个 列表,因此单个理解将无法执行(即迄今为止的最佳解决方案迭代 两次 em> 在同一个列表中)。

      创建两个列表并有条件地附加到它们:

      l1 = list()
      l2 = list()
      for i in array:
          l1.append(i) if 'exception' in i else l2.append(i)
      
      print(l1)
      ['exception[type_a]', 'exception[type_b]']
      print(l2)
      ['rule1', 'rule2', 'rule3']
      

      【讨论】:

      • "一个单一的理解不会做"我觉得必须有一种方法可以通过协同程序和几个deques 来做到这一点,这样你就只会跳过列表一次,但使用尽可能少的额外内存。与此相反,就像您说的那样,认真地使用 for 循环。特别是当期望的结果是两个列表时,只需创建两个列表:-)
      【解决方案4】:

      对于字符串列表array 和要排除的事物target:

      列表推导起作用:

      result = [s for s in array if target not in s]
      

      或者生成器对它的理解:

      result = (s for s in array if target not in s)
      

      (in 实际上是一个包含运算符,not in 是相反的。)

      或者,将 filter() built-in 与 lambda 一起使用:

      result = filter(lambda x: target not in x,
                      array)
      

      任何一个都返回一个新对象,而不是修改您的原始列表。列表推导返回一个列表,filter() 返回一个生成器,但如果您需要随机访问,可以将调用包装在 list() 中。

      【讨论】:

        【解决方案5】:

        您可以使用内置的filter 和lambda 函数来实现:

        >>> my_array = array = ['rule1','rule2','exception[type_a]','rule3','exception[type_b]']
        >>> my_string = 'exception'
        >>> filter(lambda x: my_string not in x, my_array)
        ['rule1', 'rule2', 'rule3']
        

        【讨论】:

        • 是什么让这个 Pythonic ?
        • 它是 Pythonic 的,因为它使用了 Python 提供的内置函数,完全符合要求。也就是说,没有必要进行列表理解。现在我要问你一个问题:是什么让它非 Pythonic?
        • 理解(可以说)更具可读性。见http://stackoverflow.com/a/3013722/1936320
        • 我很好奇为什么这是 Python 风格,或者比仅仅使用 for 循环遍历列表并测试元素以分离它们更符合 Python 风格。
        • 在一个理想的世界里,你会写filter(not x.contains, my_array),但他们并没有提供给我们 ;-)
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-03
        • 2011-07-04
        • 2012-08-16
        • 2011-08-10
        • 2011-07-22
        • 1970-01-01
        相关资源
        最近更新 更多