Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.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 从数据列表中仅收集7个所需项目,并创建一个新列表以删除不需要的数据?_Python_Database_List_For Loop_Iteration - Fatal编程技术网

Python 从数据列表中仅收集7个所需项目,并创建一个新列表以删除不需要的数据?

Python 从数据列表中仅收集7个所需项目,并创建一个新列表以删除不需要的数据?,python,database,list,for-loop,iteration,Python,Database,List,For Loop,Iteration,我需要遍历一个列表,检查该项是否为整数和 这是毫无意义的,因为您不能从int类型中分一杯羹 坦率地说,这整条线看起来有点像是由某种随机过程生成的: 如果供应商中的x==len(int[0:])

我需要遍历一个列表,检查该项是否为
整数
<3位数,如果不是,跳过该项并转到下一项。执行此操作,直到找到正确的项目。然后抓取该项和下面的6项,总共7项,在一个新列表中产生7个组。这将允许我跳过
“一些我不想要的struff”
,只从数据中获取我确实想要的正确分组的7项,即使中间有一组随机字符串

我尝试使用for循环:

for x in vendor:
    if x in vendor == len(int[0:]) < 3:
        vendor = [vendor[x:x+7] for x in range(0, len(vendor), 7)]
    else:
        pass
输出:

[['1','1','2', '11" Some Words symbols and numbers mixed 3-4-2#', '4', '3.00', '43.00 NC'], ['1','1','2', '11" Some Words symbols and numbers mixed 3-4-2#', '3.00', '3', '43.00 NC'], ['1','1','2', '11" Some Words symbols and numbers mixed 3-4-2#', '3.00', '3', '43.00 NC']]

数据集是结构化的,但每隔一段时间就会有一个不需要的额外项,并且无法判断该随机项是什么,或者何时出现。

错误
'type'对象不可下标
指的是以下表达式:

int[0:][/code>

这是毫无意义的,因为您不能从
int
类型中分一杯羹

坦率地说,这整条线看起来有点像是由某种随机过程生成的:

如果供应商中的x==len(int[0:])<3:

供应商
中的
x将始终是
True
,因为您在迭代中执行此操作,
x
的每个值都取自供应商
,我甚至猜不出
len(int[0:])<3
应该测试什么。除了尝试切片
int
这一事实之外,切片将获取所有切片内容,您所做的只是检查其
len
,这样您就可以轻松地执行
len(无论什么)<3
。我无法理解这段代码应该做什么

因此,让我们试着逐行查看问题描述,看看它是否可以转化为代码:

我需要迭代列表中的前1项

列表中的第一项是供应商[0]
。这不是一个迭代,因为你只做了一次。你没有说你在用那一项做什么,所以我们可以忽略这一行

然后检查下一个7

所以这将是
供应商[1:8]

以3位数或少于6位数开始

3本身小于6,所以我们可以说“小于6”,对吗?那就是len(x)<6或者不是x[0:6].isdigit()

如果没有,则跳过每个项目,直到找到正确的项目为止,抓取该项目和以下6个项目,总共7个项目,将每组7个项目放入新的子列表中

我认为这使得在
迭代中做一个简单的
变得困难,因为我们想根据我们所处的位置获取列表的各个部分,所以迭代索引似乎是一种方法

根据你的描述,我认为这是:

sublist = []
i = 1  # skip the first element
while i < len(vendor):
    if len(vendor[i]) >= 6 and vendor[i][0:6].isdigit():
        # starts with 6 or more digits, skip this one
        i += 1
        continue
    if len(vendor) - i < 7:
        # don't have 7 items left to grab, so we're done
        break
    # grab the next 7 items and then skip ahead 7
    sublist.append(vendor[i:i+7])
    i += 7
这与您的示例输出不匹配,但是您的描述说您希望子列表有7个组,而您的示例输出是6个组,所以我不知道该告诉您什么


现在是完全不同的事情 在尝试在注释中确定此代码应该做什么,但完全失败后,我将这样处理问题:

from typing import List

def some_func(data: List[str]) -> List[List[str]]:
    pass

sample_input = ['1','1','2', '11" Some Words symbols and numbers mixed 3-4-2#', '4', '3.00', '43.00 NC', '1','1','2', '11" Some Words symbols and numbers mixed 3-4-2#', '3.00', '3','43.00 NC', 'some stuff I dont want', '1','1','2', '11" Some Words symbols and numbers mixed 3-4-2#', '3.00', '3', '43.00 NC']
sample_output = [['1','1','2', '11" Some Words symbols and numbers mixed 3-4-2#', '4', '3.00', '43.00 NC'], ['1','1','2', '11" Some Words symbols and numbers mixed 3-4-2#', '3.00', '3', '43.00 NC'], ['1','1','2', '11" Some Words symbols and numbers mixed 3-4-2#', '3.00', '3', '43.00 NC']]

assert some_func(sample_input) == sample_output
经典的测试驱动开发——从测试开始,然后编写满足它的函数

在做了一点面条之后,我想到了算法“删除所有没有数字的元素,将剩余元素分组为七”:


这样做的目的是只将我想要的7个元素放入子列表中,并跳过任何使其进入初始列表的随机列表项。它对我需要做的事情非常有效

items = []
i = 0
while i < len(data):
    try:
        if int(data[i]) < 100:
            items.append(data[i:i+7])
            i += 7
    except ValueError:
        i += 1
items=[]
i=0
而i
您能为输入到输出的转换提供更多的解释或更好的示例吗?从外观上看,您正在创建仅包含6个元素的子列表。int是python中的类型,因此请避免使用该变量名。为了清楚起见,您希望在输入列表中搜索7个项目的子序列,其中前三个符合条件?这是什么“3或小于6”业务?它可能有助于解释数据来自何处,以及为什么它包含您要跳过的额外项目。这些项目来自PDF中的文本数据。我编辑了我的问题以使其更有意义。我对这个很陌生,所以我为我糟糕的英语道歉。它的意思是说“3个或更少的数字”,意思是小于“123”。也许有更好的方法来完成我想做的事情,但我是新来的,所以我还不知道做事情的所有方法。这就是问题所在。如果我已经知道如何用正确的语法表达每件事,我就不需要问这个问题了,是吗?这比我现在的方式更有意义。但是,我得到了一个错误:-------------------------------------------------------------AttributeError Traceback(最近一次调用last)in 2 I=1#跳过第一个元素3,如果len(vendor[I])>=6且vendor[I][0:6,则跳过第一个元素3:5#以6个或更多数字开头,跳过这一个6 i+=1 AttributeError:“list”对象没有属性“isdigit”您得到了它,因为您的
供应商
对象不像您的示例输入那样是字符串列表。我已经解决了所有问题,但这里的想法是不在输出中包含“一些我不想要的东西”。因此,我们的想法是获取输入,只获取以数字开头的项目,以及紧跟其后的6个项目,生成7个项目子列表,在数据列表中不包含不需要的项目。您在其中描述了两个不同的项目,但它们都与您的原始问题不匹配。查看您的示例输入/输出,它看起来不像li
from typing import List

def some_func(data: List[str]) -> List[List[str]]:
    pass

sample_input = ['1','1','2', '11" Some Words symbols and numbers mixed 3-4-2#', '4', '3.00', '43.00 NC', '1','1','2', '11" Some Words symbols and numbers mixed 3-4-2#', '3.00', '3','43.00 NC', 'some stuff I dont want', '1','1','2', '11" Some Words symbols and numbers mixed 3-4-2#', '3.00', '3', '43.00 NC']
sample_output = [['1','1','2', '11" Some Words symbols and numbers mixed 3-4-2#', '4', '3.00', '43.00 NC'], ['1','1','2', '11" Some Words symbols and numbers mixed 3-4-2#', '3.00', '3', '43.00 NC'], ['1','1','2', '11" Some Words symbols and numbers mixed 3-4-2#', '3.00', '3', '43.00 NC']]

assert some_func(sample_input) == sample_output
from typing import List

def some_func(data: List[str]) -> List[List[str]]:
    filtered = [i for i in data if any(c.isdigit() for c in i)]
    return [filtered[i*7:(i+1)*7] for i in range(len(filtered) // 7)]

sample_input = ['1','1','2', '11" Some Words symbols and numbers mixed 3-4-2#', '4', '3.00', '43.00 NC', '1','1','2', '11" Some Words symbols and numbers mixed 3-4-2#', '3.00', '3','43.00 NC', 'some stuff I dont want', '1','1','2', '11" Some Words symbols and numbers mixed 3-4-2#', '3.00', '3', '43.00 NC']
sample_output = [['1','1','2', '11" Some Words symbols and numbers mixed 3-4-2#', '4', '3.00', '43.00 NC'], ['1','1','2', '11" Some Words symbols and numbers mixed 3-4-2#', '3.00', '3', '43.00 NC'], ['1','1','2', '11" Some Words symbols and numbers mixed 3-4-2#', '3.00', '3', '43.00 NC']]

assert some_func(sample_input) == sample_output  # passes!
items = []
i = 0
while i < len(data):
    try:
        if int(data[i]) < 100:
            items.append(data[i:i+7])
            i += 7
    except ValueError:
        i += 1