【问题标题】:counting strings within strings计算字符串中的字符串
【发布时间】:2019-09-14 16:29:45
【问题描述】:

我有一个如下所示的数组:

a = ['UCI_99648;102568',  'UCI_99648;102568',  'UCI_99648;102568;99651', 'UCI_99651', 'UCI_99652', 'SIB_99658;102568;506010;706080', NaN]

我想知道有多少个stings 有一个像UCI_99651UCI_99652 这样的数字

所以,预期的结果是 2。

我如何在 python 中做到这一点。

注意:我的实际数据非常大,数字可以是任何数字,如示例中所述,可能涉及缺失值。

【问题讨论】:

  • 是什么让 UCI_99651、UCI_99652 成为唯一编号?
  • 这里的唯一是指与其他情况相比,字符串中只有一个数字。例如,代码UCI_99648;102568 有两个数字。

标签: python arrays string pandas


【解决方案1】:

您可以尝试如下。希望这能解决您的问题。

p = [word.split(";")[0] for word in uci if word != 'NaN']
print(Counter(p))
#Counter({'UCI_99648': 3, 'UCI_99651': 1, 'UCI_99652': 1, 'SIB_99658': 1})
#To filter only one occurance you can try below.
b = [word for word in p if p.count(word)==1]
print(b)

有关更多信息,您可以在此处参考列表理解文档。

http://dataunbox.com/course/24/119/Python%20for%20dummies

【讨论】:

    【解决方案2】:

    您可以使用正则表达式提取数字。 例如,像这样:

    import re
    import numpy as np
    from collections import Counter
    
    
    def count_strings_with_unq_nums(list_of_strings):
    
        # Initialize two lists - one to keep track of where the numbers occur and another to isolate unique occurences of numbers
        all_nums_nested = []
        all_nums_flat = []
    
        # Loop through all strings and extract integers within
        for s in list_of_strings:
            try:
                nums = re.findall(r'\d+', s)
                all_nums_nested.append(nums)
                all_nums_flat.extend(nums)
            except:
                continue
    
        # Count occurences of all extracted numbers
        num_counter = Counter(all_nums_flat)
    
        # Loop through nested list to find strings where unique numbers occur
        unq_items = []
        for key, val in num_counter.items():
            if val == 1:
                for n, num_list in enumerate(all_nums_nested):
                    if key in num_list:
                        unq_items.append(list_of_strings[n])
    
        # Return the number of strings containing unique numbers.        
        return len(set(unq_items))
    
    if __name__ == '__main__':
        a = ['UCI_99648;102568',  'UCI_99648;102568',  'UCI_99648;102568;99651', 'UCI_99651', 'UCI_99652', 'SIB_99658;102568;506010;706080', np.NaN]
    
        print(count_strings_with_unq_nums(a))
    
    >>> 2
    

    【讨论】:

      【解决方案3】:

      既然你已经标记了 pandas,另一种方式:

      s=pd.Series(a).dropna()
      s[s.str.split(';').str.len().eq(1)]
      

      3    UCI_99651
      4    UCI_99652
      

      【讨论】:

        【解决方案4】:

        假设所有字符串的结构都遵循上面的例子,下面的列表推导就可以了:

        l = ['UCI_99648;102568',  'UCI_99648;102568',  'UCI_99648;102568;99651', 
             'UCI_99651', 'UCI_99652', 'SIB_99658;102568;506010;706080', 'NaN']
        
        [i for i in l if ';' not in i and i != 'NaN']
        

        输出

        ['UCI_99651', 'UCI_99652']
        

        【讨论】:

          猜你喜欢
          • 2023-04-06
          • 2023-03-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多