Using Containers.Map to find unique rows in a text file
3 次查看(过去 30 天)
显示 更早的评论
I am creating a script or function that will be able to take a text file with 2 collums (Firstname, Lastname) and about 900 rows. Below is my script so far, I made sure to add plenty of notes! The purpose of this script or function is to be able to take two different lists of peoples names from an excel file where their last names and first names are in separate columns and paste that into a text file. I am having difficulty of knowing where to go from here to make this work. I am also wondering if I should make this a script or a function in order to properly read the text files with name lists. The output of this code should be names that are only displayed once. So if the name John Doe is only in the text file once then John Doe will be included in the output while if the name Sam Kemper had his name listed twice then he would not be in the output.
My code:
MATLAB code
clear;
clc;
%
m = memmapfile('lcnames.txt.')
%places the text file into the memory of faster reading of the names, just
%a fun part to add to the script, not entirely nessisary
%
nameMap = containers.Map;
%not entirely sure what this does
%
%Suppose you're going though a file
nextName = 'John Doe';
considerName( nextName, nameMap );
nextName = 'George Doe';
considerName( nextName, nameMap );
nextName = 'John Doe';
considerName( nextName, nameMap );
nextName = 'John Smith';
considerName( nextName, nameMap );
nextName = 'John Smith';
considerName( nextName, nameMap );
%This stuff here is what it'll be like
%
keys = nameMap.keys;
values = nameMap.values;
size = nameMap.size;
%think of these three as like a three dimensional map they each relate to
%eachother
%
for i = nameMap.keys
if( nameMap(char(i)) == 1 )
%This person was unique
disp( i );
%This part of the script is to show people whose name is not on the
%list twice
end
end
0 个评论
回答(1 个)
Walter Roberson
2018-5-5
t = readtable('YourFile.txt', 'VariableNames', {'Firstname', 'Lastname'}, 'ReadVariableNames', false);
[G, TID] = findgroups(t);
counts = accumarray(G);
only_occurs_once = TID(counts==1);
2 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Cell Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!