簡體   English   中英

僅從嵌套列表中刪除 integer 字符串

[英]Removing integer only strings from nested Lists

我有列表,我試圖從中刪除 integer 僅數據。 我想保留任何包含 $、+ 或 - 的字符串。

string = (",A', B', C', +145', $50', Split', D', E', F', 1', 2000', -678', $10', Split', H', I', J', 3', 4', 50', +132', $45', Split'").replace("'", '')

ls_of_lists = [x.split(",") for x in string.split('Split')]

# What I want my out put to be:

desired_ls_of_ls = [
    ["A", "B", "C", "+145", "$50"],
    ["D", "E", "F", "-678", "$10"],
    ["H", "I", "J", "+132","$45"]
]

我已經嘗試了很多東西,我敢打賭我很容易錯過/忽略。 在所有修改過的 int_filters(見下文)上,我嘗試了搜索、.contains 和其他我能想到的方法,並且環顧四周 web 也沒有幫助我。 我也嘗試過交換收益和回報。

下面是我的作品:

def int_filter_0( someList ):
    for v in someList:
        try:
            int(v)
            continue # Skip these
        except ValueError:
            yield v # Keep these


def int_filter_1(some_list):
    for v in some_list:
        if v.find("\\+"):
            yield v  # Keep these
        if v.find("\\-"):
            yield v  # Keep these
        else:
            try:
                int(v)
                continue # Skip these
            except ValueError:
                yield v # Keep these

def int_filter_2(some_list):
    for v in some_list:
        if v.contains("\\+"):
            yield v  # Keep these
        if v.contains("\\-"):
            yield v  # Keep these
        else:
            try:
                int(v)
                continue # Skip these
            except ValueError:
                yield v # Keep these


def int_filter_3( someList ):
    for v in someList:
        try:
            if v.find("\\+"):
                continue
            if v.find("\\-"):
                continue
            int(v)
            continue # Skip these
        except ValueError:
            yield v # Keep these



test_0 = [list(int_filter_0(item)) for item in ls_of_lists]

test_1 = [list(int_filter_1(item)) for item in ls_of_lists]

test_2 = [list(int_filter_2(item)) for item in ls_of_lists]

test_3 = [list(int_filter_3(item)) for item in ls_of_lists]

string = (",A', B', C', +145', $50', Split', D', E', F', 1', 2000', -678', $10', Split', H', I', J', 3', 4', 50', +132', $45', Split'")
string = string.replace("'", '').replace(' ', '') # clean ' and spaces
ls_of_lists = [x.split(",") for x in string.split('Split')]

ls_of_lists = [[c for c in ls if (not c.isdigit() and c)] for ls in ls_of_lists] # Clean the numbers and empty strings
ls_of_lists = [ls for ls in ls_of_lists if ls]  # Clean empty lists
for l in ls_of_lists:
    print(l)

# Output:
# ['A', 'B', 'C', '+145', '$50']
# ['D', 'E', 'F', '-678', '$10']
# ['H', 'I', 'J', '+132', '$45']

這是使用正則表達式和生成器的有效方法:

import re
def split(s):
    l = []
    for x in re.split(',\s*', s):
        if x.isdigit() or not x:
            continue
        elif x == 'Split':
            yield l
            l = []
        else:
            l.append(x)
   # if l:
   #     yield l

out = list(split(string))

Output:

[['A', 'B', 'C', '+145', '$50'],
 ['D', 'E', 'F', '-678', '$10'],
 ['H', 'I', 'J', '+132', '$45']]

注意。 如果即使沒有終端“Split”也想獲取值,請取消注釋最后兩行。

我弄清楚我做錯了什么。 在 If v.find 上,它需要更直接(不包括 \)並包括!=

def int_filter_1(some_list):
    for v in some_list:
        if v.find("+") != -1:
            yield v  # Keep these
        if v.find("-")!= -1:
            yield v  # Keep these
        else:
            try:
                int(v)
                continue # Skip these
            except ValueError:
                yield v # Keep these

這給了我正確的結果

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM