两个数组中寻找共同的元素 [英] Finding common elements in two arrays

查看:165
本文介绍了两个数组中寻找共同的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经声明类似以下内容类型。

I’ve declared a type similar to the following.

type
  TLikes = record
    Name            : string[20];
    favColours  : array of string[20];

  faves     = array of TLikes;

一旦记录被填充我将它们保存到一个二进制文件所以结构就是这样如下图所示。

Once the records are populated I save them to a binary file so the structure is like that shown below.

[John],     [Green]     [White]     [Blue]
[Paul],     [Blue]      [Red]       [White]     [Green]
[David],    [Red]       [Blue]      [Green]
[Bob],      [White]     [Blue]
[Peter],    [Blue]      [Green]     [Red]

这很容易找出什么颜色的大卫,例如,喜欢。当我想知道谁喜欢蓝色出现一个小问题。所以我所做的就是建立第二个文件,像这样......

It’s easy to find out what colours David, for example, likes. A small problem occurs when I want the to know who likes blue. So what I’ve done is build a second file, like so …

[Blue],     [John]      [Paul]      [David]     [Peter]         [Bob]
[Red],      [David]     [Paul]      [Peter]
[White],    [Bob]       [David]     [John]      [Paul]
[Green],    [John]      [David]     [Paul]      [Peter]

但有些事情告诉我,我应该不是真的需要创建第二个文件/数据结构,它只是似乎效率不高。

But something is telling me, I shouldn’t really need to create a second file / data structure, it just seems inefficient.

下面是一个更大的问题......

Here’s a bigger issue ….

如果我需要找到谁喜欢什么大卫喜欢的任何组合是什么?我的结果是...

What if I need to find who likes any combination of what David likes? My results would be …

Blue and red and green  =   Paul, David, Peter
Blue and red            =   Paul, David, Peter
Blue and green          =   John, Paul, David, Peter
Red and Green           =   Paul, David, Peter

我的问题是。

有没有更好的方式来组织数据/记录,所以我可以找出鲍勃和保罗的共同点(蓝色和白色),或者什么红色和白色的共同点(大卫和保罗)?

Is there a better way to structure the data / records so I can figure out what Bob and Paul have in common (Blue and White) or what red and white have in common (David and Paul) ?

我想我需要指出的是,我试图简化上面的例子。在现实Tlikes.Name数据会像串...

I guess I need to point out that I have tried to simplify the example above. In reality the data for Tlikes.Name will be strings like …

‘decabbadc’
‘bacddbcad’
‘eebadeaac’

有东西在这些字符串的200K +的顺序。并且Tlikes.FavColours数据是文件名(有这些文件的周围2K)。文件名表示包含Tlikes.Name字符串的文件

There are something in the order of 200k+ of these strings. And the Tlikes.FavColours data is a filename (there are around 2k of these files). The file name indicates a file that contains the Tlikes.Name string.

欲能够检索给定Tlikes.Name串或给定文件名的字符串列表的文件名的列表。

I want to be able to retrieve a list of file names given a Tlikes.Name string or a list of strings given a file name.

NB - 这是把我往'套',但是从小事我明白了,我在集合的元素数量有限的,我是在正确的轨道上。

NB – Something is drawing me to ‘sets’ but from the little I understand, I’m limited in the number of elements in sets, am I on the right track ?

感谢您抽出时间阅读文章。

Thank you for taking the time to read the post.

推荐答案

您在处理一个多到多的关系在这里。结果
如果它是一个数据库,这意味着你会放在3个表:

You're dealing with a many-to-many relationship here.
If it were a database that means you'd put in 3 tables:

1. People
2. Colors
3. Link table between 1 and 2

我建议你要么通过利用数据库​​解决问题或者数据库在Delphi模拟的东西,就像它。

I suggest you either fix the problem by utilizing a database or model the thing in Delphi just like it where a database.

用Delphi结构结果
此外 ShortString短他们已经过时,有超过longstrings零收益停止使用。结果
使用3个表意味着您可以迅速得到人们每种颜色和颜色每人列表。

Using Delphi structures
Furthermore stop using shortstring They are outdated and have zero benefits over longstrings.
Using 3 tables means you can quickly get a list of people per color and colors per person.

下面是它如何工作:

TPerson = record
  name: string;
  other_data....
end;

TPeople = array of TPerson;

TFavColor = record
  name: string;
  other_data....
end;

TFavColors = array of TFavColor;

TPersonColor = record
  PersonIndex: Cardinal;  <<-- index into the TPeople array
  ColorIndex: Cardinal;   <<-- index into the TFavColors array
end;

TPersonColors = array of TPersonColor;

现在你可以遍历所有的TPersonColors阵列来提取数据。

Now you can just loop over the TPersonColors array to extract your data.

使用数据库结果
在SQL因为你的数据编制索引,将会变​​得更快(外键都(应该是)永远索引)。

Using a database
In SQL it would be even faster because your data is indexed (foreign key are (should be) always indexed).

SQL语句中看到所有的人,像蓝色和红色会是什么样子(这里使用MySQL的语法):

The SQL statement the see all people that like blue and red would look like (using MySQL syntax here):

SELECT p.name  
FROM person p
INNER JOIN personcolor pc ON (pc.person_id = p.id)
INNER JOIN color c1 ON (pc.color_id = c1.id)
INNER JOIN color c2 ON (pc.color_id = c2.id)
WHERE c1.name = 'red' AND c2.name = 'blue'
GROUP BY p.id <<-- eliminate duplicates (not sure it's needed)

用Delphi其琐碎的数据库链接到您的应用程序。结果
所以这是我推荐的路线。

Using Delphi its trivial to link a database to your app.
So that's the route I'd recommend.

这篇关于两个数组中寻找共同的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆