Python在与数据作斗争?

Python在与数据作斗争?,python,arrays,database,python-3.x,Python,Arrays,Database,Python 3.x,该计划的基础是将邮政编码(英国版的邮政编码)转换为坐标。因此,我有一个包含大量邮政编码(以及其他附加数据,如房价)的文件,还有一个包含所有英国邮政编码及其相关坐标的文件 我将这两个文件都转换为列表,然后在for循环中使用for循环来迭代和比较两个文件中的邮政编码。如果文件1中的邮政编码==文件2中的邮政编码,则坐标将被获取并附加到相关文件中 我已经按照我的要求安装并运行了我的代码。我所有的测试输出的都是我想要的,非常好 唯一的问题是,它只能处理小批量的数据(我一直在使用容纳约100行的.csv文

该计划的基础是将邮政编码(英国版的邮政编码)转换为坐标。因此,我有一个包含大量邮政编码(以及其他附加数据,如房价)的文件,还有一个包含所有英国邮政编码及其相关坐标的文件

我将这两个文件都转换为列表,然后在for循环中使用for循环来迭代和比较两个文件中的邮政编码。如果文件1中的邮政编码==文件2中的邮政编码,则坐标将被获取并附加到相关文件中

我已经按照我的要求安装并运行了我的代码。我所有的测试输出的都是我想要的,非常好

唯一的问题是,它只能处理小批量的数据(我一直在使用容纳约100行的.csv文件进行测试-创建包含100个内部列表的列表)

现在,我想将我的程序应用于我的整个数据集。我运行过一次,什么也没发生。我走了,看了些电视,但还是什么事也没发生。IDLE不会让我退出这个项目的。所以我重新启动并再次尝试,这次添加了一个计数器,以查看我的代码是否正在运行。我运行代码,计数器开始运行。直到它达到78902,我的数据集的大小。然后它停下来什么也不做。我什么也做不了,也关不上窗户

令人恼火的是,它甚至无法通过读取CSV文件,因此我无法操作我的数据

下面是它卡住的代码(代码的第一部分):

那么,有人知道如何让我的数据更易于管理吗

编辑:对于那些感兴趣的人,这里是我的完整代码:

from tkinter.filedialog import asksaveasfile
import csv

new_file = asksaveasfile()

lst = []
# List function enables use for all files
def create_list():
    #empty variable to put the list into
    #find the file
    file2 = input('enter filepath:')
    #read the file and iterate over it to append into the list
    with open(file2, 'r') as f:
        reader = csv.reader(f, delimiter=',')
        for row in reader:
            lst.append(row)
    return lst


def remove_space(lst):
    '''(lst)->lst
    Returns the postcode value without any whitespace

    >>> ac45 6nh
    ac456nh
    The above would occur inside a list inside a list
    '''
    filetype = input('Is this a sale or crime?: ')
    num = 0
    #check the filetype to find the position of the postcodes
    if filetype == 'sale':
        num = 3
        #iterate over the postcode to add all characters but the space
    for line in range(len(lst)):        
        pc = ''
        for char in lst[line][num]:
            if char != ' ':
                pc = pc+char
        lst[line][num] = pc

def write_new_file(lst, new_file):
    '''(lst)->.CSV file
    Takes a list and writes it into a .CSV file.
    '''
    writer = csv.writer(new_file, delimiter=',')
    writer.writerows(lst)
    new_file.close()


#conversion function
def find_coord(postcode):

    lst = create_list()
    #create python list for conversion comparison
    print(lst[0])
    #empty variables
    long = 0
    lat = 0
    #iterate over the list of postcodes, when the right postcode is found,
    # return the co-ordinates.
    for row in lst:
        if row[1] == postcode:
            long = row[2]
            lat = row[3]
    return str(long)+' '+str(lat)

def find_all_coord(postcode, file):

    #empty variables
    long = 0
    lat = 0
    #iterate over the list of postcodes, when the right postcode is found,
    # return the co-ordinates.
    for row in file:
        if row[1] == postcode:
            long = row[2]
            lat = row[3]
    return str(long)+' '+str(lat)

def convert_postcodes():
    '''
    take a list of lst = []
    #find the file
    file2 = input('enter filepath:')
    #read the file and iterate over it to append into the list
    with open(file2, 'r') as f:
        reader = csv.reader(f, delimiter=',')
        for row in reader:
            lst.append(row)
    '''
    #save the files into lists so that they can be used
    postcodes = []
    with open(input('enter postcode key filepath:'), 'r') as f:
        reader = csv.reader(f, delimiter=',')
        for row in reader:
            postcodes.append(row)
    print('enter filepath to be converted:')
    file = []
    with open(input('enter filepath to be converted:'), 'r') as f:
        reader = csv.reader(f, delimiter=',')
        for row in reader:
            file.append(row)
    #here is the conversion code
    long = 0
    lat = 0
    matches = 0
    for row in range(len(file)):
        for line in range(len(postcodes)):
            if file[row][3] == postcodes[line][1]:
                long = postcodes[line][2]
                lat = postcodes[line][3]
                file[row].append(str(long)+','+str(lat))
                matches = matches+1
                print(matches)
    final_file = asksaveasfile()
    write_new_file(file, final_file)

我从空闲状态单独调用这些函数,以便在让程序自己运行它们之前对其进行测试。

您的问题是在所有文件中查找所有代码,这会进行大量比较


您可以尝试将其保存在dict中,postral代码是关键。

也许您应该使用sqlite3模块,在其中加载csv文件,并使用SQL进行连接?

在所有这些数据中循环是低效的

一个快速而肮脏的解决方案是使用SQLite或其他关系数据存储,您可以对其应用索引(如果这不能直接解决您的问题)


对于此解决方案和其他解决方案,您可以在每个选项上使用timeit()编写一个快速测试,并增加数据大小以识别响应。

如果您使用
dict()
而不是
list()
,您的代码将更加高效。通用算法:

  • 将数据加载到两个字典中:一个用于坐标,另一个用于信息。两者都以邮政编码为关键
  • 遍历这些字典中最短的一个,并在大字典中为每个邮政编码查找具有相同邮政编码的项目。将匹配的邮政编码和坐标保存在某处

  • 问题是,
    dict()
    通过索引获得O(1),而
    list()
    通过搜索获得O(n)(这几乎与执行另一个循环相同)。对于大数据而言,这会产生巨大的差异,事实上,您不需要双循环。

    您的主要瓶颈在于您的
    转换邮政编码功能:

    for row in range(len(file)):
        for line in range(len(postcodes)):
    
    如果在
    文件中有
    N
    项,在
    邮政编码中有
    M
    项,则此双循环需要
    M*N
    迭代

    相反,将邮政编码中的项目循环一次,并将邮政编码到经度/纬度的映射数据保存在
    目录中。然后在
    文件
    上循环一次,并使用此dict为
    文件
    中的每个项目提供所需的数据。这将完成
    M+N
    迭代:



    你能一次一行地处理
    file2
    中的每一行吗?还是需要将所有行加载到一个Python列表中,并同时保留在内存中?所以你在处理78902*78902比较?因为我是新手,我更容易一次处理每一位。然而,我的大部分代码都是基于for循环的,每次只在代码上运行一个循环,所以这听起来是可能的。但是,如果我在第一步处理方面已经失败了,那么这不会让事情变得更糟吗?@Scironic:而你是在两者之间循环?这是134多万亿次的比较。让我们停止绕圈子(混合隐喻)。如果您发布代码,我们可能会提出具体建议。:)以前从未使用过SQL,所以我对它一无所知!不过,这是要学的语言之一,并不难。你能行!sqlite很快。SQL也是关键值,所以让OP先玩dicts。不知道如何使用SQL,因为我还没有接触过它。虽然我想我很快就会回来。Python不是随机访问字典吗?如果是这样的话,找到正确的钥匙可能需要更长的时间吗?我确实考虑过使用字典,但不确定它会好得多。不,字典以看似随机的顺序显示内容,但是访问是非常高效的。这就是他们所做的,有效地将一个关键映射到未来参考的价值。我以前没有听说过时间复杂性,但它有一定的意义。。。这就是说,我的关键列表(包含邮政编码和坐标的列表)已经排序,只有我的其他数据集没有排序。如果您使用列表,您需要使用O(n)进行搜索还是使用O(n)进行迭代。如果正确使用排序(例如,使用对分模块),排序会带来一些好处,但在这种情况下,字典的效率要高得多。您不需要使用字典进行排序。谢谢,我将尝试使用上述建议之一(逐行阅读和书写,而不是一次全部阅读和书写)@Scironic O(1)意味着无论您拥有多少数据,都需要相同的时间。O(n)意味着它的长度将是数据的两倍。谢谢,这真的很有帮助!我还没有做过任何关于词典的工作,所以和它们一起玩会很有趣。我会在早上试一试,希望一切都能顺利进行。
    for row in range(len(file)):
        for line in range(len(postcodes)):
    
    def convert_postcodes(postcode_path, file_path, output_path):
        postcodes = dict()
        with open(postcode_path, 'rb') as f:
            reader = csv.reader(f, delimiter=',')
            for row in reader:
                code, lng, lat = row[1:4]
                postcodes[code] = [lng, lat]
        with open(file_path, 'rb') as fin, open(output_path, 'wb') as fout:
            reader = csv.reader(fin, delimiter=',')
            writer = csv.writer(fout, delimiter=',')
            for row in reader:
                code = row[3]
                row.extend(postcodes[code])
                writer.writerow(row)