[英]Python: How do I remove elements from a dictionary and return it as a list?
我一直在嘗試一項練習,要求我刪除字典中包含7個或更少字符長的單詞的單詞。 到目前為止,我已嘗試使用.values()
方法僅獲取方括號內的單詞,從而創建一個列表來存儲這些單詞。 但是,我遇到了使用pop方法的問題
“TypeError:'str'對象不能解釋為整數”。
這是我的代碼:
word_dict = {'show': ['display', 'exhibit', 'convey', 'communicate', 'manifest', 'disclose'],
'slow': ['unhurried', 'gradual', 'leisurely', 'late', 'behind', 'tedious', 'slack']}
def main():
edited_synonyms = remove_word(word_dict)
print(edited_synonyms)
def remove_word(word_dict):
synonyms_list = word_dict.values()
new_list = []
for i in synonyms_list:
new_list.extend(i)
for word in new_list:
letter_length = len(word)
if letter_length <= 7:
new_list.pop(word)
return new_list
main()
list.pop
將要從列表中彈出的項目的索引作為參數,但是當您執行new_list.pop(word)
,實際上是在提供項目本身,因此錯誤。
來自文檔: https : //docs.python.org/3/tutorial/datastructures.html
list.pop([I])
刪除列表中給定位置的項目,然后將其返回。 您可以使用字典理解輕松解決此問題,您只需在列表中包含長度> 7的值
要解決此問題,只需找到要通過list.remove()
彈出的單詞的索引。
所以行
new_list.pop(word)
將改為
new_list.remove(word)
但更好的方法可能是使用像這樣的列表理解
word_dict = {'show': ['display', 'exhibit', 'convey', 'communicate', 'manifest', 'disclose'],
'slow': ['unhurried', 'gradual', 'leisurely', 'late', 'behind', 'tedious', 'slack']}
#Iterate through the values of the list and pick one with length > 7
res = [v for value in word_dict.values() for v in value if len(v) > 7 ]
print(res)
輸出將是
['communicate', 'manifest', 'disclose', 'unhurried', 'leisurely']
你的計划是對的。 您需要在pop
添加index
值的任何方法。
修改后的代碼
>>> def remove_word(word_dict):
synonyms_list = word_dict.values()
new_list = []
for i in synonyms_list:
new_list.extend(i)
for word in new_list:
letter_length = len(word)
if letter_length <= 7:
new_list.pop(new_list.index(word)) # Modified line
return new_list
輸出:
>>> main()
['exhibit', 'communicate', 'manifest', 'disclose', 'unhurried', 'leisurely', 'behind', 'slack']
嘗試這個 :
outp = {k: [v for v in word_dict[k] if len(v)<=7 ]for k in word_dict}
輸出 :
{'show': ['communicate', 'manifest', 'disclose'], 'slow': ['unhurried', 'leisurely']}
在list
您可以根據索引彈出元素。
word_dict = {'show': ['display', 'exhibit', 'convey', 'communicate', 'manifest', 'disclose'],
'slow': ['unhurried', 'gradual', 'leisurely', 'late', 'behind', 'tedious', 'slack']}
def main():
edited_synonyms = remove_word(word_dict)
print(edited_synonyms)
def remove_word(word_dict):
synonyms_list = word_dict.values()
new_list = list()
for i in synonyms_list:
new_list.extend([word for word in i if len(word) > 7])
return new_list
main()
就個人而言,我會這樣做:
for key in word_dict.keys():
word_dict[key] = [x for x in word_dict[key] if len(x)>7]
或者您可以通過以下方式使用list的remove屬性: -
for word in new_list:
print(word)
letter_length = len(word)
if letter_length <= 7:
new_list.remove(word)
替代方式: -
print(new_list)
for word in new_list:
print(word)
letter_length = len(word)
if letter_length <= 7:
new_list.pop(new_list.index(word))
你應該使用remove
功能而不是pop
remove()
函數將單個元素作為參數。
pop()
函數接受一個參數(索引)並刪除該索引處的項目。
更多細節 :
word_dict = {'show': ['display', 'exhibit', 'convey', 'communicate', 'manifest', 'disclose'],
'slow': ['unhurried', 'gradual', 'leisurely', 'late', 'behind', 'tedious', 'slack']}
res =[]
for i in word_dict:
tmp =[j for j in word_dict[i] if len(j)<=7]
res.extend(tmp)
print(res)
# output ['display', 'exhibit', 'convey', 'gradual', 'late', 'behind', 'tedious', 'slack']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.