Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 从数字列表中的字符串中删除num_Python 3.x - Fatal编程技术网

Python 3.x 从数字列表中的字符串中删除num

Python 3.x 从数字列表中的字符串中删除num,python-3.x,Python 3.x,例如: print("1.", get_list_nums_without_9([589775, 677017, 34439, 48731548, 782295632, 181967909])) print("2.", get_list_nums_without_9([6162, 29657355, 5485406, 422862350, 74452, 480506, 2881])) print("3.", get_list_nums_without_9([292069010, 7398

例如:

print("1.", get_list_nums_without_9([589775, 677017, 34439, 48731548,     782295632, 181967909]))
print("2.", get_list_nums_without_9([6162, 29657355, 5485406, 422862350, 74452, 480506, 2881]))
print("3.", get_list_nums_without_9([292069010, 73980, 8980155, 921545108, 75841309, 6899644])) 
print("4.", get_list_nums_without_9([]))

nums = [292069010, 73980, 8980155, 21545108, 7584130, 688644, 644908219, 44281, 3259, 8527361, 2816279, 985462264, 904259, 3869, 609436333, 36915, 83705, 405576, 4333000, 79386997]
print("5.", get_list_nums_without_9(nums))
我想得到没有9的数字列表。如果列表为空或列表中的所有数字都包含数字9,则函数应返回空列表。我尝试了下面的功能,它不工作

def get_list_nums_without_9(a_list):
    j=0
    for i in a_list:
    a_list[j]=i.rstrip(9)
    j+=1
    return a_list
预期:

1. [677017, 48731548]
2. [6162, 5485406, 422862350, 74452, 480506, 2881]
3. []
4. []
5. [21545108, 7584130, 688644, 44281, 8527361, 83705, 405576, 4333000]
您的列表包含整数。要删除包含
9
的字符串,最好的方法是测试
9
是否属于作为字符串的数字,并使用带条件的列表理解重新生成输出

(此外,
rstrip
删除字符串的尾随字符。完全不适合您的问题)

使用您的数据进行测试:

>>> numbers = [
...    [589775, 677017, 34439, 48731548, 782295632, 181967909],
...    [6162, 29657355, 5485406, 422862350, 74452, 480506, 2881],
...    [292069010, 73980, 8980155, 921545108, 75841309, 6899644],
...    [],
...    [292069010, 73980, 8980155, 21545108, 7584130, 688644, 644908219, 
...     44281, 3259, 8527361, 2816279, 985462264, 904259, 3869, 609436333, 
...     36915, 83705, 405576, 4333000, 79386997]
... ]

>>> for i, l in enumerate(numbers, 1):
...    print("{}. {}".format(i, get_list_nums_without_9(l)))
1. [677017, 48731548]
2. [6162, 5485406, 422862350, 74452, 480506, 2881]
3. []
4. []
5. [21545108, 7584130, 688644, 44281, 8527361, 83705, 405576, 4333000]
您的列表包含整数。要删除包含
9
的字符串,最好的方法是测试
9
是否属于作为字符串的数字,并使用带条件的列表理解重新生成输出

(此外,
rstrip
删除字符串的尾随字符。完全不适合您的问题)

使用您的数据进行测试:

>>> numbers = [
...    [589775, 677017, 34439, 48731548, 782295632, 181967909],
...    [6162, 29657355, 5485406, 422862350, 74452, 480506, 2881],
...    [292069010, 73980, 8980155, 921545108, 75841309, 6899644],
...    [],
...    [292069010, 73980, 8980155, 21545108, 7584130, 688644, 644908219, 
...     44281, 3259, 8527361, 2816279, 985462264, 904259, 3869, 609436333, 
...     36915, 83705, 405576, 4333000, 79386997]
... ]

>>> for i, l in enumerate(numbers, 1):
...    print("{}. {}".format(i, get_list_nums_without_9(l)))
1. [677017, 48731548]
2. [6162, 5485406, 422862350, 74452, 480506, 2881]
3. []
4. []
5. [21545108, 7584130, 688644, 44281, 8527361, 83705, 405576, 4333000]

您不希望替换列表中的任何内容,而是希望根据在旧列表中检测到的内容生成新列表:

def get_list_nums_without_9(numbers):
    sans_9 = []

    for number in numbers:
        if '9' not in str(number):
            sans_9.append(number)

    return sans_9

如果没有有效的数字,这将得到一个空列表。

如果不想替换列表中的任何内容,则需要根据在旧列表中检测到的内容生成一个新列表:

def get_list_nums_without_9(numbers):
    sans_9 = []

    for number in numbers:
        if '9' not in str(number):
            sans_9.append(number)

    return sans_9

如果没有有效的数字,这将为您提供一个空列表。

但是如果您遵循OP文章中的示例,此输入将为您提供
[215451087584130688644]
。“他不是在修改数字,而是在删除它们。”我读得有点快。谢谢你指出这一点,编辑。但是如果你遵循OP文章中的例子,这个输入应该会给你
[215451087584130688644]
。“他不是在修改数字,而是在删除它们。”我读得有点快。谢谢你指出这一点,编辑。