简体   繁体   English

如何对列表信息的某些部分进行分组?

[英]how to group some part of a list's information?

I have a list each element of list is like this:我有一个列表,列表的每个元素都是这样的:

 list[0]={'Keywords': 'program manager',
 'result': {'categoryId': '2712',
  'categoryName': 'program manager',
  'score': '0.9506622791290285'},
 'categoryId': '2712'}
 {'Keywords': 'technicalfunctional consultant', 'result': []}

I need to collect all keywords whose have the same categoryName .我需要收集所有具有相同categoryName的关键字。 I did the following:我做了以下事情:

output1 = defaultdict(set)
for entry in list:
    kwds = entry['Keywords'].strip().split(' ')
    for word in kwds:
        output1[entry.get('categoryId', None)].add(word)

But it split all words and I don't want it.但它分裂了所有的词,我不想要它。 is there any way to collect all keywords with the same categoryName ?有没有办法收集所有具有相同categoryName的关键字?

You can find all the existent categoryIds in the dataset, then build a dict that for each one of them contains all the keywords associated.您可以在数据集中找到所有现有的 categoryId,然后构建一个 dict,其中每个包含相关的所有关键字。

EDIT: Changed the code to have a MRE and changed the name of the list to list1编辑:将代码更改为具有 MRE 并将列表名称更改为list1

Code:代码:

list1=[]
list1.append({'Keywords': 'program manager',
'result': {'categoryId': '2712',
'categoryName': 'program manager',
'score': '0.9506622791290285'},
'categoryId': '2712'})

cat_set = set([elem["result"]["categoryName"] for elem in list1])

cat_dict = {}

for cat_name in cat_set:
    cat_dict[cat_name] = [elem["Keywords"] for elem in list1 if elem["result"]["categoryName"] == cat_name]
mylist=[{'Keywords': 'scrum master',
  'result': {'categoryId': '3193',
   'categoryName': 'agile coach',
   'score': '1.0'},
  'categoryId': '3193'},
 {'Keywords': 'principal consultant',
  'result': {'categoryId': '2655',
   'categoryName': 'principal consultant',
   'score': '1.045369052886963'},
  'categoryId': '2655'},{'Keywords': 'technicalfunctional consultant', 'result': []}]
def keywordcollector(yourlist):
    yourlist=list(filter(lambda x:type(x['result'])==type({}),yourlist))
    categories=set(x['result']['categoryName'] for x in yourlist)
    keywordslist=[]
    for category in categories:
        temp_list=list(filter(lambda x:x['result']['categoryName']==category,yourlist))
        temp_keywords=list(map(lambda x:x['Keywords'],temp_list))
        keywordslist.append({category:temp_keywords})
    return keywordslist
print(keywordcollector(mylist))

The code above gives you dictionary object with keywords for every categoryname.上面的代码为您提供字典 object,其中每个类别名称都有关键字。 The code outputs代码输出

[{'principal consultant': ['principal consultant']}, {'agile coach': ['scrum master']}]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据每个元素中的某些信息对列表的元素进行分组? - how to group element's of a list with respect of some information in each elements? 如何针对 python 中的某些值按键分组? - how to group by keys with respect of some part of values in python? 如何从列表中的一个元素中检测字符串的某些部分,从列表中的另一个元素中检测字符串的另一部分? - How to detect some part of string from one element in list and other part from another element in list? Python将列表的一部分分配给另一个列表 - Python assign some part of the list to another list 为什么列表的值在代码的某些部分没有改变? - Why my list's values does not change in some part of my code? 如何使用python中的某个范围对数字列表进行分组? - How do I group list of numbers using some range in python? 通过Python中的某些值对列表进行分组 - Group list by some value in Python 如何检查,然后取出列表项的一部分? - How to check, and then, pull out part of list's item? 如何根据元素名称的一部分对一维列表进行排序? - How to sort a 1-d list based on part of element's name? 对存储在列表中的字符进行分组,以便可以使用正则表达式来解析列表信息,该信息用于存储由GSM调制解调器在Python中接收的数据 - Group characters stored in a list so regular expressions can be used to parse the list's information storing data received by a GSM modem in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM