首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >词典没有把所有的项目都收进去。只有最后一个项目被接受。如何将所有的值存储在dict中?

词典没有把所有的项目都收进去。只有最后一个项目被接受。如何将所有的值存储在dict中?
EN

Stack Overflow用户
提问于 2019-08-07 05:01:56
回答 2查看 29关注 0票数 0

这是代码:

代码语言:javascript
复制
d = []
d1 = {}
print(type(d1))
desss = df['Description'].head()
for des in desss:
    print(des)
    d1['Description'] = des
    d.append(d1)
d

这是输出:

代码语言:javascript
复制
<class 'dict'>
Sr Project Manager \xe2\x80\x93 Cluster / Infotainment/ Automotive. Minimum 10 years experience in embedded domain \xe2\x80\x93 Automotive Cluster is preferred.
Knowledge of Automotive Domain - cluster and infotainment. In the area of Instrument Clusters and Infotainment Systems.
Strong Debugging Skills, Automotive infotainment Background Android framework and linux kernel. Porting of Applications across platforms,.
Ensure quality of delivery for customer releasesExcellent debugging and analytical skillsIndependent Cluster Infotainment Domain knowledge and handling skills\xe2\x80\xa6
Prior automotive experience in integrated instrument cluster applications, ADAS and self-driving systems preferred. 5 to 15 years of relevant experience.
[{'Description': 'Prior automotive experience in integrated instrument cluster applications, ADAS and self-driving systems preferred. 5 to 15 years of relevant experience.'},
 {'Description': 'Prior automotive experience in integrated instrument cluster applications, ADAS and self-driving systems preferred. 5 to 15 years of relevant experience.'},
 {'Description': 'Prior automotive experience in integrated instrument cluster applications, ADAS and self-driving systems preferred. 5 to 15 years of relevant experience.'},
 {'Description': 'Prior automotive experience in integrated instrument cluster applications, ADAS and self-driving systems preferred. 5 to 15 years of relevant experience.'},
 {'Description': 'Prior automotive experience in integrated instrument cluster applications, ADAS and self-driving systems preferred. 5 to 15 years of relevant experience.'}]

我如何纠正上述错误?

有人能帮忙吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-08-07 05:09:41

运行d.append(d1)时,实际上只对指向d1的指针进行排队。同时,在运行d1['Description'] = des时,如果更改现有对象的description字段,则不会创建新对象。

因此,在对对象的引用排队之后,您正在更改对象,而且由于列表没有存储对象的副本,所以列表中的值也会发生更改。下列备选办法应能奏效:

代码语言:javascript
复制
d = []
desss = df['Description'].head()
for des in desss:
    print(des)
    d1 = {}
    d1['Description'] = des
    d.append(d1)
d
票数 2
EN

Stack Overflow用户

发布于 2019-08-07 05:13:46

据我所知,您希望创建一个名为df['Description'].head()的字典,并期望它包含在df['Description'].head()下找到的所有元素。

但是下面的代码表明,您正在试图覆盖该dict的现有内容。

代码语言:javascript
复制
d1['Description'] = des

因此,要将新数据附加到,您的dict应该如下所示

代码语言:javascript
复制
d1 = {
'Description': [des1, des2, des2]
}

尝试将其附加到d1 dict中现有的列表中,如

代码语言:javascript
复制
d = []
d1 = {'Description': []}
print(type(d1))
desss = df['Description'].head()
for des in desss:
    print(des)
    d1['Description'].append(des)
    #d.append(d1)
#d
print(d1) # Should print the whole d1 dict

这应该能解决你的问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57387234

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档