我有大约2000行数据采用以下格式:
.
.
[(0, 1, 'Blank', ''), (0, 3, 'NAME', 'Ayiesha Woods'), (5, 10, 'DOB', 'July 2 , 1979'), (10, 13, 'LOC', 'Long Island'), (13, 16, 'LOC', 'New York')]
[(0, 1, 'Blank', ''), (0, 3, 'NAME', 'Craig Rivera'), (7, 12, 'DOB', 'October 10 , 1954'), (5, 7, 'LOC', 'Manhattan')]
[(0, 1, 'Blank', ''), (0, 4, 'NAME', 'Margery Pitt Durant'), (14, 16, 'LOC', 'Flint'), (6, 11, 'DOB', 'May 24 , 1887'), (16, 18, 'LOC', 'Michigan')]
[(0, 1, 'Blank', ''), (0, 3, 'NAME', 'Austin Watson'), (10, 13, 'LOC', 'Ann Arbor'), (13, 15, 'LOC', 'Michigan'), (4, 9, 'DOB', 'January 13 , 1992')]
[(0, 1, 'Blank', ''), (0, 3, 'NAME', 'Gary Spatz'), (5, 8, 'LOC', 'New York'), (16, 19, 'LOC', 'New York'), (19, 21, 'LOC', 'Miami'), (21, 23, 'LOC', 'Florida'), (8, 13, 'DOB', 'April 1 , 1951')]
.
.
.
它们基本上是很多列表,每个列表都包含各个元组内的名称,dob,loc等人的详细信息。
我想以下列格式提取所有人及其对应的dob的名称:
('Ayiesha Woods', 'DateOfBirth', 'July 2 , 1979')
('Craig Rivera', 'DateOfBirth', 'October 10 , 1954')
依旧......
这是我的尝试:
temp = "DateOFBirth"
results = []
for n1 in text:
for n2 in text:
if n1 is not n2:
if text[1][2] == 'NAME' and text[2][2] == 'DOB':
rel = text[1][3], temp, text[2][3]
print(rel)
results.append(rel)
仅当名称元组位于位置1且日期元组位于列表中的位置2时才会输出,但并非总是如此。
如果我想输出结果,无论列表中的名称元组或日期元组的位置如何,我该怎么办。
编辑:
我有一个包含元组的列表:
text = [(0, 1, 'Blank', ''), (0, 3, 'NAME', 'Vance Trimble'), (5, 7, 'LOC', 'Harrison'), (7, 9, 'LOC', 'Arkansas'), (9, 14, 'DOB', 'July 6 , 1913')]
我希望以下列格式提取数据:
('Vance Trimble', 'DateOFBirth', 'July 6 , 1913')
我的代码:
temp = "DateOFBirth"
if text[1][2] == 'NAME' and text[4][2] == 'DOB':
rel = text[1][3], temp, text[4][3]
print(rel)
何我这样做而不必硬编码:
text[1][2] == 'NAME' and text[4][2] == 'DOB'
以便它自己搜索列表中的“#NAME”字样。和' DOB'并得到输出。
答案 0 :(得分:2)
将问题分解为简单的步骤:
中提琴!
results = []
for rec in records:
result = ["", "DateOfBirth", ""]
for item in rec:
if "NAME" in item:
result[0] = item[3]
elif "DOB" in item:
result[2] = item[3]
results.append(tuple(result))
print(results)
答案 1 :(得分:1)
我建议编写一个帮助函数,从函数中检索信息。我还假设您正在使用元组列表列表。
test_list = [[(0, 1, 'Blank', ''),
(0, 3, 'NAME', 'Ayiesha Woods'),
(5, 10, 'DOB', 'July 2 , 1979'),
(10, 13, 'LOC', 'Long Island'),
(13, 16, 'LOC', 'New York')],
[(0, 1, 'Blank', ''),
(0, 3, 'NAME', 'Craig Rivera'),
(7, 12, 'DOB', 'October 10 , 1954'),
(5, 7, 'LOC', 'Manhattan')],
[(0, 1, 'Blank', ''),
(0, 4, 'NAME', 'Margery Pitt Durant'),
(14, 16, 'LOC', 'Flint'),
(6, 11, 'DOB', 'May 24 , 1887'),
(16, 18, 'LOC', 'Michigan')],
[(0, 1, 'Blank', ''),
(0, 3, 'NAME', 'Austin Watson'),
(10, 13, 'LOC', 'Ann Arbor'),
(13, 15, 'LOC', 'Michigan'),
(4, 9, 'DOB', 'January 13 , 1992')],
[(0, 1, 'Blank', ''),
(0, 3, 'NAME', 'Gary Spatz'),
(5, 8, 'LOC', 'New York'),
(16, 19, 'LOC', 'New York'),
(19, 21, 'LOC', 'Miami'),
(21, 23, 'LOC', 'Florida'),
(8, 13, 'DOB', 'April 1 , 1951')]]
#Helper function
def get_person_info(lst):
person_name = list(filter(lambda x: 'NAME' in x, lst))[0][3:]
person_dob = list(filter(lambda x: 'DOB' in x, lst))[0][2:4]
return person_name + person_dob
#Use it with map
list(map(get_person_info, test_list))
输出:
[('Ayiesha Woods', 'DOB', 'July 2 , 1979'),
('Craig Rivera', 'DOB', 'October 10 , 1954'),
('Margery Pitt Durant', 'DOB', 'May 24 , 1887'),
('Austin Watson', 'DOB', 'January 13 , 1992'),
('Gary Spatz', 'DOB', 'April 1 , 1951')]
使用text
测试辅助函数:
text = [(0, 1, 'Blank', ''), (0, 3, 'NAME', 'Vance Trimble'), (5, 7, 'LOC', 'Harrison'), (7, 9, 'LOC', 'Arkansas'), (9, 14, 'DOB', 'July 6 , 1913')]
get_person_info(text)
## ('Vance Trimble', 'DOB', 'July 6 , 1913')
您可以轻松更换“DOB'与' DateOFBirth'。
答案 2 :(得分:1)
你可以这样做:
temp = "DateOFBirth"
text = [(0, 1, 'Blank', ''), (0, 3, 'NAME', 'Vance Trimble'), (5, 7, 'LOC', 'Harrison'), (7, 9, 'LOC', 'Arkansas'), (9, 14, 'DOB', 'July 6 , 1913')]
rel = []
for i in text:
if 'NAME' in i:
rel.append(i[i.index('NAME')+1])
rel.append(temp)
elif 'DOB' in i:
rel.append(i[i.index('DOB')+1])
print rel
# result:
# ['Vance Trimble', 'DateOFBirth', 'July 6 , 1913']
通过这种方式,结果与元组中项目“NAME”和“DOB”的位置无关,但仅限于实际名称始终位于“标记”'NAME'
之后,因为它位于(0, 3, 'NAME', 'Vance Trimble')
,其中实际名称位于NAME
之后。