Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.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 改进了区分不同类型表的算法_Python_Algorithm_Data Structures - Fatal编程技术网

Python 改进了区分不同类型表的算法

Python 改进了区分不同类型表的算法,python,algorithm,data-structures,Python,Algorithm,Data Structures,我有两个具有以下结构的表,其中在表1中,ID位于Name旁边,而在表2中,ID位于Title 1旁边。这两个表之间的一个相似之处是,第一个人的名字旁边总是有ID。对于后来的人来说,他们是不同的 表1: Name&Title | ID # ---------------------- Random_Name 1|2000 Title_1_1 | - Title_1_2 | - Random_Name 2| 2000 Title_2_1 | - Title_2_2

我有两个具有以下结构的表,其中在表1中,ID位于Name旁边,而在表2中,ID位于Title 1旁边。这两个表之间的一个相似之处是,第一个人的名字旁边总是有ID。对于后来的人来说,他们是不同的

表1:

Name&Title   | ID # 
----------------------
Random_Name 1|2000 
Title_1_1    | - 
Title_1_2    | -
Random_Name 2| 2000
Title_2_1    | -
Title_2_2    | -
...          |...
表2:

Name&Title   | ID # 
----------------------
Random_Name 1| 2000 
Title_1_1    | -
Title_1_2    | -
Random_Name 2| -
Title_2_1    | 2000
Title_2_2    | -
...          |...
我有识别表1的代码,但很难合并结构2。该表存储为嵌套的行列表(每行都是一个列表)。通常,一个人只有一行姓名,但有多行标题。伪代码如下所示:

set count = 0
find the ID next to the first name, set it to be a recognizer
for row_i,row in enumerate(table):
   compare the ID of the next row until I found: row[1] == recognizer
   set count = row i
   slice the table to get the first person. 
    header_ind = 0 # something related to the rest of the code
    recognizer = data[header_ind+1][1]
    count = header_ind+1
    result = []
    result.append(data[0]) #this append the headers
    for i, row in enumerate(data[header_ind+2:]):
        if i <= len(data[header_ind+4:]):
            if row[1] and data[i+1+header_ind+2][1] is recognizer:
                print data[i+header_ind+3]
                one_person = data[count:i+header_ind+3]
                result.append(one_person)
                count = i+header_ind+3
        else:
            if i == len(data[header_ind+3:]):
                last_person = data[count:i+header_ind+3]
                result.append(last_person)
                count = i+header_ind+3
实际代码如下:

set count = 0
find the ID next to the first name, set it to be a recognizer
for row_i,row in enumerate(table):
   compare the ID of the next row until I found: row[1] == recognizer
   set count = row i
   slice the table to get the first person. 
    header_ind = 0 # something related to the rest of the code
    recognizer = data[header_ind+1][1]
    count = header_ind+1
    result = []
    result.append(data[0]) #this append the headers
    for i, row in enumerate(data[header_ind+2:]):
        if i <= len(data[header_ind+4:]):
            if row[1] and data[i+1+header_ind+2][1] is recognizer:
                print data[i+header_ind+3]
                one_person = data[count:i+header_ind+3]
                result.append(one_person)
                count = i+header_ind+3
        else:
            if i == len(data[header_ind+3:]):
                last_person = data[count:i+header_ind+3]
                result.append(last_person)
                count = i+header_ind+3
header_ind=0#与代码其余部分相关的内容
识别器=数据[header_ind+1][1]
计数=页眉索引+1
结果=[]
result.append(数据[0])#这将附加标题
对于i,枚举中的行(数据[header\u ind+2:]):

如果我把这个粘在这里

所以这些是你的输入假设是你被限制在这个…:

# Table 1 
data1 = [['Name&Title','ID#'],
    ['Random_Name1','2000'],
    ['Title_1_1','-'],
    ['Title_1_2','-'],
    ['Random_Name2','2000'],
    ['Title_2_1','-'],
    ['Title_2_2','-']]

# TABLE 2
data2 = [['Name&Title','ID#'],
    ['Random_Name1','2000'],
    ['Title_1_1','-'],
    ['Title_1_2','-'],
    ['Random_Name2','-'],
    ['Title_2_1','2000'],
    ['Title_2_2','-']]
这是您想要的输出:

for x in data: 
    print x

['Random_Name2', '2000']
['Name&Title', 'ID#']
[['Random_Name1', '2000'], ['Title_1_1', '-'], ['Title_1_2', '-']]
[['Random_Name2', '2000'], ['Title_2_1', '-'], ['Title_2_2', '-']]

你是在用字典编表1吗?如果是这样,您可以将此循环中的表2创建为将
i
row
连接在一起的元素列表。您的算法没有任何意义。现在还不清楚这个算法到底想做什么。“识别器”是什么?不管怎样,为什么你不能简单地在表中迭代两次(每列一次),扔掉不相关的条目,然后将名称挑选到单独的列表中?@SebasSBM我想我不理解你的方法。但我可以把它写成字典。假设我想找到行Random_Name 1的索引
I
,以及行Random_Name 2的索引
I
。它是如何工作的?@aestrivex由于许多原因,我无法分别识别姓名和头衔。例如,如果我浏览表2并扔掉列ID中的所有空条目,我将丢失Random_Name 2。而且,识别器是原始ID(比如性别或出生年份)。这两个表中的每个人都将具有相同的ID。但是,它为他们保留在不同的行中。为什么会“丢失”空条目?这个问题是否也伴随着一个约束,即您不能在数组中迭代多次?如果是,为什么?嗨,是的。这是我的愿望输出。那你是怎么从假想变成假想的呢?真是糟糕透了。。如果头衔和随机姓名之间没有唯一性,我认为这是不可能的-如果有可能,看看这一点,以确定个人记录的开始。。。直到你有一个叫Mr,Mrs等的古怪的人。。。。或者,如果您确实知道可以作为条件构建的模式,但它不会自动生成。如果您仍然对我的工作感兴趣,我找到了另一种方法来查找名称列表并进行匹配。所以我实际上找不到解决这个问题的办法。可悲的是,这个问题不可能有一个elgant的解决方案,除非你知道随机名称和/或标题的值。。。只有当他们不跨越。