标签云中的算法(计算)

发布于 2024-08-17 04:10:11 字数 415 浏览 5 评论 0原文

我有一个标签列表,其中包含重复标签的列表,例如“tag1”、“tag1”、“tag2”、“tag3”、“tag3”和“tag2”。

我想计算列表中有多少个tag1,tag2和tag3,然后将它们添加到一个新的标签列表中,例如var tagList = new List(),它将有数千个列表中的标签。谁能帮助我以有效的方式实现这一目标?该列表应包含标签名称和标签数量(计数)。

这是我的 Tag 类属性:

    public int TagID { get; set; }
    public string TagName { get; set; }
    public int TagCount { get; set; }

非常感谢。

I have a tag list which contains a list of duplicated tags, such as "tag1", "tag1", "tag2", "tag3", "tag3" and "tag2".

I want to calculate and how many tag1, tag2 and tag3 are in the list, and then add them into a new tag list such as var tagList = new List<Tag>(), it will have thousands of the tags in the list. Can anyone one help me to achieve this in an effective way? The list should have the tag name and the number of tags (count).

Here is the my Tag class properties:

    public int TagID { get; set; }
    public string TagName { get; set; }
    public int TagCount { get; set; }

Many thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我不吻晚风 2024-08-24 04:10:11

我无法从您的描述中判断您是否有代表标签的 IEnumerable 或代表标签的 IEnumerable,因此这里是两种方法:

对于 Tag 情况:

List<Tag> tags = new List<Tag>() {
    new Tag() { TagName = "tag1" },
    new Tag() { TagName = "tag1" },
    new Tag() { TagName = "tag2" },
    new Tag() { TagName = "tag3" },
    new Tag() { TagName = "tag3" },
    new Tag() { TagName = "tag2" }
};
var tagList = tags.GroupBy(t => t.TagName)
                  .Select(g => new Tag() {
                      TagName = g.Key,
                      TagCount = g.Count()
                  })
                  .ToList();

对于 string 情况:

List<string> list = new List<string>() {
    "tag1",
    "tag1",
    "tag2",
    "tag3",
    "tag3",
    "tag2"
};
var tagList = list.GroupBy(s => s)
                  .Select(g => new Tag() {
                      TagName = g.Key,
                      TagCount = g.Count() 
                  })
                  .ToList(); 

在这两种情况下我都使用了定义

class Tag {
    public int TagID { get; set; }
    public string TagName { get; set; }
    public int TagCount { get; set; }
}

I can't tell from your description if you have an IEnumerable<string> representing the tags or an IEnumerable<Tag> representing the tags so here is an approach for both:

For the Tag case:

List<Tag> tags = new List<Tag>() {
    new Tag() { TagName = "tag1" },
    new Tag() { TagName = "tag1" },
    new Tag() { TagName = "tag2" },
    new Tag() { TagName = "tag3" },
    new Tag() { TagName = "tag3" },
    new Tag() { TagName = "tag2" }
};
var tagList = tags.GroupBy(t => t.TagName)
                  .Select(g => new Tag() {
                      TagName = g.Key,
                      TagCount = g.Count()
                  })
                  .ToList();

For the string case:

List<string> list = new List<string>() {
    "tag1",
    "tag1",
    "tag2",
    "tag3",
    "tag3",
    "tag2"
};
var tagList = list.GroupBy(s => s)
                  .Select(g => new Tag() {
                      TagName = g.Key,
                      TagCount = g.Count() 
                  })
                  .ToList(); 

In both cases I used the definition

class Tag {
    public int TagID { get; set; }
    public string TagName { get; set; }
    public int TagCount { get; set; }
}
苏辞 2024-08-24 04:10:11

如果我正确理解你的问题,我认为这会起作用。您想要按标签 ID 和名称进行分组,然后对现有列表中每个标签的计数求和。

var tagList = existingList.GroupBy( t => new { t.TagID, t.TagName } )
                          .Select( g => new Tag {
                                         TagID = g.Key.TagID,
                                         TagName = g.Key.TagName,
                                         TagCount = g.Sum( t => t.TagCount )
                          })
                          .ToList();

If I understand your question properly, I think this will work. You want to group by the tag id and name, then sum the count for each of the tags in the existing list.

var tagList = existingList.GroupBy( t => new { t.TagID, t.TagName } )
                          .Select( g => new Tag {
                                         TagID = g.Key.TagID,
                                         TagName = g.Key.TagName,
                                         TagCount = g.Sum( t => t.TagCount )
                          })
                          .ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文