중첩 목록을 사용하고 적절한 명사를 제거하는 새로운 목록을 반환하려고하는 중입니다.중첩 목록에서 고유 명사 제거하기 python
L = [['The', 'name', 'is', 'James'], ['Where', 'is', 'the', 'treasure'], ['Bond', 'cackled', 'insanely']]
I 반환 할 : ''삭제됩니다
['the', 'name', 'is', 'is', 'the', 'tresure', 'cackled', 'insanely']
를 적어 둡니다을 여기
은 예입니다. 중첩 목록의 다른 곳에서는 나타나지 않기 때문에 괜찮습니다. 중첩 된 각 목록은 문장입니다. 내 접근 방식은 중첩 목록의 모든 첫 번째 요소를 newList에 추가하는 것입니다. 그런 다음 newList의 요소가 중첩 목록에 있는지 비교합니다. 나는 그 요소를 newList에 소문자로 검사 할 것이다. 나는이 프로그램을 반쯤 마쳤지만 끝에있는 newList에서 요소를 제거하려고 할 때 오류가 발생합니다. 새로운 업데이트 목록이 생기면 newList에있는 항목을 nestedList에서 삭제하려고합니다. 마지막으로 중첩 목록에있는 모든 항목을 newerList에 추가하고 소문자로 지정합니다. 그렇게해야합니다.누군가가보다 효율적인 방법을 사용하면 기꺼이 들어 줄 것입니다. 이 코드는 두 번째의 오류로 인해 완료되지
def lowerCaseFirst(L):
newList = []
for nestedList in L:
newList.append(nestedList[0])
print newList
for firstWord in newList:
sum = 0
firstWord = firstWord.lower()
for nestedList in L:
for word in nestedList[1:]:
if firstWord == word:
print "yes"
sum = sum + 1
print newList
if sum >= 1:
firstWord = firstWord.upper()
newList.remove(firstWord)
return newList
주 라인 여기
가 newerList (updatedNewList) 함께 지속한다 :
def lowerCaseFirst(L):
newList = []
for nestedList in L:
newList.append(nestedList[0])
print newList
updatedNewList = newList
for firstWord in newList:
sum = 0
firstWord = firstWord.lower()
for nestedList in L:
for word in nestedList[1:]:
if firstWord == word:
print "yes"
sum = sum + 1
print newList
if sum >= 1:
firstWord = firstWord.upper()
updatedNewList.remove(firstWord)
return updatedNewList
오류 메시지 :
Traceback (most recent call last):
File "/Applications/WingIDE.app/Contents/MacOS/src/debug/tserver/_sandbox.py", line 1, in <module>
# Used internally for debug sandbox under external interpreter
File "/Applications/WingIDE.app/Contents/MacOS/src/debug/tserver/_sandbox.py", line 80, in lowerCaseFirst
ValueError: list.remove(x): x not in list
오류가 실제로 무엇인지는 언급하지 않지만 반복하면서 목록을 변경할 수는 없습니다.이전 목록에서 가져 오지 않은 항목을 제거하지 않고 원하는 항목을 새 목록에 추가하는 것이 어떻습니까? 더 일반적인 피드백이 필요하면 http://codereview.stackexchange.com을 시도하십시오. – jonrsharpe
첫 번째 "The"소문자입니까? – MxyL