这是代码:
d = []
d1 = {}
print(type(d1))
desss = df['Description'].head()
for des in desss:
print(des)
d1['Description'] = des
d.append(d1)
d这是输出:
<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.'}]我如何纠正上述错误?
有人能帮忙吗?
发布于 2019-08-07 05:09:41
运行d.append(d1)时,实际上只对指向d1的指针进行排队。同时,在运行d1['Description'] = des时,如果更改现有对象的description字段,则不会创建新对象。
因此,在对对象的引用排队之后,您正在更改对象,而且由于列表没有存储对象的副本,所以列表中的值也会发生更改。下列备选办法应能奏效:
d = []
desss = df['Description'].head()
for des in desss:
print(des)
d1 = {}
d1['Description'] = des
d.append(d1)
d发布于 2019-08-07 05:13:46
据我所知,您希望创建一个名为df['Description'].head()的字典,并期望它包含在df['Description'].head()下找到的所有元素。
但是下面的代码表明,您正在试图覆盖该dict的现有内容。
d1['Description'] = des因此,要将新数据附加到,您的dict应该如下所示
d1 = {
'Description': [des1, des2, des2]
}尝试将其附加到d1 dict中现有的列表中,如
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这应该能解决你的问题。
https://stackoverflow.com/questions/57387234
复制相似问题