这是我的清单:
lista=[u'REG_S_3_UMTS_0_0 (RNC)', u'REG_S_3_UMTS_0_1 (RNC)', u'REG_S_3_UMTS_0_2 (RNC)', u'REG_S_2_GSM_NORT_CBSP_bsc_0_0 (BSC)', u'REG_S_2_GSM_NORT_CBSP_bsc_0_1 (BSC)', u'REG_S_2_GSM_NORT_CBSP_bsc_0_2 (BSC)', u'REG_S_3_GSM_ERIC_CBSP_bsc_0_0 (BSC)', u'REG_S_3_GSM_ERIC_CBSP_bsc_0_1 (BSC)', u'REG_S_3_GSM_ERIC_CBSP_bsc_0_2 (BSC)', u'REG_S_3_GSM_HUAP_CBSM_bsc_0_0 (BSC)', u'REG_S_3_GSM_HUAP_CBSM_bsc_0_1 (BSC)', u'REG_S_3_GSM_HUAP_CBSM_bsc_0_2 (BSC)', u'REG_S_3_GSM_HUA_CBSM_bsc_0_0 (BSC)', u'REG_S_3_GSM_HUA_CBSM_bsc_0_1 (BSC)', u'REG_S_3_GSM_HUA_CBSM_bsc_0_2 (BSC)', u'REG_S_3_GSM_IPAC_SABP_bsc_0_0 (BSC)', u'REG_S_3_GSM_IPAC_SABP_bsc_0_1 (BSC)', u'REG_S_3_GSM_IPAC_SABP_bsc_0_2 (BSC)', u'REG_S_3_GSM_NOKI_CLNS_bsc_0_0 (BSC)', u'REG_S_3_GSM_NOKI_CLNS_bsc_0_1 (BSC)', u'REG_S_3_GSM_NOKI_CLNS_bsc_0_2 (BSC)', u'REG_S_3_GSM_NOKI_RFC1_bsc_0_0 (BSC)', u'REG_S_3_GSM_NOKI_RFC1_bsc_0_1 (BSC)', u'REG_S_3_GSM_NOKI_RFC1_bsc_0_2 (BSC)', u'REG_S_3_GSM_NORT_CBSP_bsc_0_0 (BSC)', u'REG_S_3_GSM_NORT_CBSP_bsc_0_1 (BSC)', u'REG_S_3_GSM_NORT_CBSP_bsc_0_2 (BSC)', u'REG_S_3_GSM_SIEM_BSCI_bsc_0_0 (BSC)', u'REG_S_3_GSM_SIEM_BSCI_bsc_0_1 (BSC)', u'REG_S_3_GSM_SIEM_BSCI_bsc_0_2 (BSC)', u'REG_S_GSM_ERIC_CBSP_bsc_0_0 (BSC)', u'REG_S_GSM_ERIC_CBSP_bsc_0_1 (BSC)', u'REG_S_GSM_ERIC_CBSP_bsc_0_2 (BSC)', u'REG_S_GSM_HUAP_CBSM_bsc_0_0 (BSC)', u'REG_S_GSM_HUAP_CBSM_bsc_0_1 (BSC)', u'REG_S_GSM_HUAP_CBSM_bsc_0_2 (BSC)', u'REG_S_GSM_HUA_CBSM_bsc_0_0 (BSC)', u'REG_S_GSM_HUA_CBSM_bsc_0_1 (BSC)', u'REG_S_GSM_HUA_CBSM_bsc_0_2 (BSC)', u'REG_S_GSM_NORT_CBSP_bsc_0_0 (BSC)', u'REG_S_GSM_NORT_CBSP_bsc_0_1 (BSC)', u'REG_S_GSM_NORT_CBSP_bsc_0_2 (BSC)', u'Pool ID: 200']
那就是我的功能:
def Filter_List(lista):
string = ''.join(lista)
match = re.findall(r"\(([A-Z]+)\)|Pool ID", string)
return match
结果,我得到:
[u'RNC', u'RNC', u'RNC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'']
但是最后一个元素(应该是:池ID)没有显示,只有:u''。有谁知道我该如何改变自己的表情?提前致谢 !!!
最佳答案
请注意,如果正则表达式模式已定义捕获组,则re.findall
返回元组(的列表)。去掉它:
import re
lista=[u'REG_S_3_UMTS_0_0 (RNC)', u'REG_S_3_UMTS_0_1 (RNC)', u'REG_S_3_UMTS_0_2 (RNC)', u'REG_S_2_GSM_NORT_CBSP_bsc_0_0 (BSC)', u'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S_GSM_ERIC_CBSP_bsc_0_2 (BSC)', u'REG_S_GSM_HUAP_CBSM_bsc_0_0 (BSC)', u'REG_S_GSM_HUAP_CBSM_bsc_0_1 (BSC)', u'REG_S_GSM_HUAP_CBSM_bsc_0_2 (BSC)', u'REG_S_GSM_HUA_CBSM_bsc_0_0 (BSC)', u'REG_S_GSM_HUA_CBSM_bsc_0_1 (BSC)', u'REG_S_GSM_HUA_CBSM_bsc_0_2 (BSC)', u'REG_S_GSM_NORT_CBSP_bsc_0_0 (BSC)', u'REG_S_GSM_NORT_CBSP_bsc_0_1 (BSC)', u'REG_S_GSM_NORT_CBSP_bsc_0_2 (BSC)', u'Pool ID: 200']
string = ''.join(lista)
match = re.findall(r"\([A-Z]+\)|Pool ID", string)
print(match)
请参阅this Python demo返回
[u'(RNC)', u'(RNC)', u'(RNC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'Pool ID']