gpt4 book ai didi

python - 更新字典的值而不需要太多的项目副本

转载 作者:行者123 更新时间:2023-11-30 22:09:09 25 4
gpt4 key购买 nike

我有以下工作详细数据,
其中之一是

   job_detail = {
"company": "abc company recruit",
"job_title": "python developer",
"job_request": "20k-40k /Delhi / 3-5years / Bachelor / Full-time",
"job_tags": "Big Data\nSoftware Development",
"pub_date": "10:19 published from network",
}

我尝试使用以下代码提取并格式化它:

job_detail["company"] = job_detail["company"].replace("recruit", "").strip()
job_detail["job_title"] = job_detail["job_title"].strip()
job_request = [jr.strip() for jr in job_detail["job_request"].split("/")]
salary, location, experience, education, time = job_request
job_detail['salary'] = salary
job_detail['location'] = location
job_detail['experience'] = experience
job_detail["education"] = education
job_detail['time'] = time
job_detail["job_tags"] = job_detail["job_tags"].replace("\n", ",")
job_detail['pub_date'] = re.search(r"\w+", job_detail["pub_date"]).group()

运行一下

In [81]: job_detail
Out[81]:
{'company': 'abc company',
'job_title': 'python developer',
'job_request': '20k-40k /Delhi / 3-5years / Bachelor / Full-time',
'job_tags': 'Big Data,Software Development',
'pub_date': '10',
'salary': '20k-40k',
'location': 'Delhi',
'experience': '3-5years',
'education': 'Bachelor',
'time': 'Full-time'}

我通过输入太多“job_details”来获取所需的数据,并且每个项目都应该复制到每一行。

如何以优雅的方式解决问题?

最佳答案

使用zip创建键值对并使用dict.update更新字典

>>> keys=["salary", "location", "experience", "education", "time"]
>>> pairs = zip(keys, map(str.strip, job_detail["job_request"].split("/")))
>>> job_detail.update(pairs)
>>> pprint(job_detail)
{'company': 'abc company recruit',
'education': 'Bachelor',
'experience': '3-5years',
'job_request': '20k-40k /Delhi / 3-5years / Bachelor / Full-time',
'job_tags': 'Big Data\nSoftware Development',
'job_title': 'python developer',
'location': 'Delhi',
'pub_date': '10:19 published from network',
'salary': '20k-40k',
'time': 'Full-time'}

关于python - 更新字典的值而不需要太多的项目副本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51999725/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com