[英]How to append an element to list that is in dictionary in Python
我查看了這些帖子: append list inside dictionary with update和Append element to minimum list in dictionary of lists 。 他們沒有幫助我。 我想做這樣的事情:有一個result = {}
和spl = input().split(' ')
,我做了一些事情,結果變成了result = {'text': [1, 2]}
(對於例子)。 這個怎么做?
我試圖在第一個鏈接中做類似的事情: result.update({'text': result['text'] + [1, 2]})
,但它沒有奏效。 我還嘗試了第二個鏈接的事情:
result = {'text': []}
result['text'].append(1, 2)
但它給了我一個錯誤AttributeError: 'str' object has no attribute 'append'
。 真正的代碼部分,直到預期的部分在下面。
編碼:
def checkThru(txt, wordsDesc=1, countMoreMost=False, indexInOutput=False):
result = {}
spl = txt.split(' ')
badChars = ['?', ',', '.', '!',]
wordam = list(range(0, wordsDesc))
for lol in range(len(spl)):
for sublol in badChars:
spl[lol] = spl[lol].replace(sublol, "")
for i in spl:
iinspl = spl.index(i)
if indexInOutput == True:
if i == 'are' or i == 'am' or i == 'is' or i == 'were' or i == \
'was' or i == 'will' or i == 'shall':
if spl[iinspl + 1] == 'a' or spl[iinspl + 1] == 'an' or \
spl[iinspl + 1] == 'the':
if countMoreMost == False:
if spl[iinspl + 2] == 'more' or spl[iinspl + 2] == 'most':
result.update({iinspl-1: []})
for add in wordam:
result.update({spl[iinspl-1].append(iinspl+3+add)}) #???(Here's where the error says something is wrong.)
#Actually, spl[iinspl-1] is going to be a list, because of the line <<result.update({iinspl-1: []})>>
追溯:
Traceback (most recent call last):
File "D:\python\I MADE A MODULE!!! indeX.py", line 16, in <module>
print(indeX.checkThru('Hello, I am David. My sister is the most Ann babe', 1, False, True))
File "C:\Users\Danil\AppData\Local\Programs\Python\Python37-32\lib\indeX.py", line 287, in checkThru
result.update({spl[iinspl-1].append(iinspl+1+add)})
AttributeError: 'str' object has no attribute 'append'
我想讓它制作一本特征字典。 例如:
print(checkThru('Hi, I am David, and my sister is Ann!'))
>>> {'I': ['David'], 'sister': ['Ann']}
你的代碼:
result = {'text': []}
result['text'].append(1, 2)
不應給出AttributeError: 'str' object has no attribute 'append'
除非您在嘗試 append 之前將result['text']
的值定義為字符串。
但是, append
不會像這樣占用兩個 arguments。 如果要將1
和2
添加到列表中,可以執行以下操作之一:
result = {'text': []}
result['text'].append(1)
result['text'].append(2)
print(result)
印刷:
{'text': [1, 2]}
或者
result = {'text': []}
result['text'] += [1, 2]
print(result)
印刷:
{'text': [1, 2]}
result = {'text': []} result['text'].append(1, 2)
會給
TypeError: append() 只接受一個參數(給定 2 個),因為 append 只接受一個參數。 不是你提到的那個。
如果您不想返回列表列表,則 go 的一種方法是 append 元素一個元素。 result = {'text':[]} result['text'].append(1) result['text'].append(2) result {'text': [1, 2]}
會給出想要的結果
或者改用update
。 語法看起來不錯:
result.update({'text': [1, 2]}) result {'text': [1, 2]}
話雖如此,在快速查看代碼之后:
True
以便您的result
字典被修改,請檢查一次。False
開頭,代碼所做的檢查是針對True
的,因此它不會在第一次檢查自身后退出程序。{注意:代碼的空運行將打印語句可以幫助您獲得正確的邏輯}
您發布的第二個鏈接內容:
result = {'text': []}
result['text'].append(1, 2)
...將工作。 你得到的錯誤是因為它說的 - 你試圖在一個字符串上運行 append 。
您的spl
列表是一個字符串列表,由txt.split()
產生。 你的代碼:
spl[iinspl-1].append()
...因此將始終嘗試將 append 轉換為字符串,這(正如您所發現的)不起作用。
我想可能在你的意思是附加到result
的那段代碼中,而不是spl
?
編輯:或者你想要類似的東西:
result[spl[iinspl-1]].append(iinspl+3+add))
可能是更新而不是 append,具體取決於那里的內容和您想要的內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.