颤音/飞镖:如何计算列表阵列的标签

发布于 2025-02-13 05:56:34 字数 484 浏览 0 评论 0原文

如何计算数组的标签? 我有此代码:

class Product{
  Product({required this.name, required this.tags});
  final String name;
  final List<String> tags;

}

void TagList(){

  final product =[
    Product(name: 'bmw', tags: ['car', 'm3']),
    Product(name: 'kia', tags: ['car', 'morning', 'suv']),
    Product(name: 'hyundai', tags: ['car', 'ev6', 'suv']),
  ];
}

如何获得每个标签使用多少次?

预期输出:

汽车(3) M3(1) EV6(1) SUV(2) 早上(1)

How can I count tags of an array?
I have this code:

class Product{
  Product({required this.name, required this.tags});
  final String name;
  final List<String> tags;

}

void TagList(){

  final product =[
    Product(name: 'bmw', tags: ['car', 'm3']),
    Product(name: 'kia', tags: ['car', 'morning', 'suv']),
    Product(name: 'hyundai', tags: ['car', 'ev6', 'suv']),
  ];
}

How can I get how many times each tag was used?

Expected output:

car(3)
m3(1)
ev6(1)
suv(2)
morning(1)

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

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

发布评论

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

评论(2

德意的啸 2025-02-20 05:56:34

您可以使用此功能在products列表上循环循环,并使用每次使用标签的次数:

void printTags(List<Product> products) {
  final tagCount = <String, int>{};
  for (final product in products) {
    for (final tag in product.tags) {
      tagCount[tag] = tagCount.putIfAbsent(tag, () => 0) + 1;
    }
  }
  print(tagCount);
}

输出:

{car: 3, m3: 1, morning: 1, suv: 2, ev6: 1}

这是一个完整的可运行示例:

void main() {
  final products = [
    Product(name: 'bmw', tags: ['car', 'm3']),
    Product(name: 'kia', tags: ['car', 'morning', 'suv']),
    Product(name: 'hyundai', tags: ['car', 'ev6', 'suv']),
  ];

  printTags(products);
}

class Product {
  Product({required this.name, required this.tags});
  final String name;
  final List<String> tags;
}

void printTags(List<Product> products) {
  final tagCount = <String, int>{};
  for (final product in products) {
    for (final tag in product.tags) {
      tagCount[tag] = tagCount.putIfAbsent(tag, () => 0) + 1;
    }
  }
  print(tagCount);
}

You can use this function to loop over the products list and get how many times each tag is used:

void printTags(List<Product> products) {
  final tagCount = <String, int>{};
  for (final product in products) {
    for (final tag in product.tags) {
      tagCount[tag] = tagCount.putIfAbsent(tag, () => 0) + 1;
    }
  }
  print(tagCount);
}

Output:

{car: 3, m3: 1, morning: 1, suv: 2, ev6: 1}

Here's a complete runnable example:

void main() {
  final products = [
    Product(name: 'bmw', tags: ['car', 'm3']),
    Product(name: 'kia', tags: ['car', 'morning', 'suv']),
    Product(name: 'hyundai', tags: ['car', 'ev6', 'suv']),
  ];

  printTags(products);
}

class Product {
  Product({required this.name, required this.tags});
  final String name;
  final List<String> tags;
}

void printTags(List<Product> products) {
  final tagCount = <String, int>{};
  for (final product in products) {
    for (final tag in product.tags) {
      tagCount[tag] = tagCount.putIfAbsent(tag, () => 0) + 1;
    }
  }
  print(tagCount);
}
牵强ㄟ 2025-02-20 05:56:34
  final result = product.map((i) => i.tags)
    .expand((i) => i)
    .map((i) => {i: 1})
    .reduce((sum, i) => {
      ...sum, 
      ...{i.keys.first: (sum[i.keys.first] ?? 0) + 1 }
    });
  final result = product.map((i) => i.tags)
    .expand((i) => i)
    .map((i) => {i: 1})
    .reduce((sum, i) => {
      ...sum, 
      ...{i.keys.first: (sum[i.keys.first] ?? 0) + 1 }
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文