从列表中删除元素或设置它是否包含特定字符
Remove element from list or set if it contains certain character
我得到了一个包含月份的 txt 文件,我将其打开并填写到列表或集合中。我需要遍历列表并删除任何包含字母 'r' 或 "R" 的月份。我尝试使用集合和列表进行此操作,但不断收到 RuntimeError。这是我使用集合的代码:
monthList = {}
def main():
monthList = fillList()
print(monthList)
removeRMonths(monthList)
def fillList():
infile = open("SomeMonths.txt")
monthList = {line.rstrip() for line in infile}
return monthList
def removeRMonths(monthList):
for month in monthList:
for ch in month:
if ch == "r" or ch == "R":
monthList.remove(month)
print(monthList)
main()
我收到的错误是:
Traceback (most recent call last):
File "/Users/hwang/Desktop/Week7.py", line 115, in <module>
main()
File "/Users/hwang/Desktop/Week7.py", line 99, in main
removeRMonths(monthList)
File "/Users/hwang/Desktop/Week7.py", line 107, in removeRMonths
for month in monthList:
RuntimeError: Set changed size during iteration
>>>
这是我尝试使用列表的代码:
monthList = ()
def main():
monthList = fillList()
print(monthList)
removeRMonths(monthList)
def fillList():
infile = open("SomeMonths.txt")
monthList = (line.rstrip() for line in infile)
return monthList
def removeRMonths(monthList):
for month in monthList:
for ch in month:
if ch == "r" or ch == "R":
monthList.remove(month)
else:
continue
print(monthList)
main()
我的错误是:
Traceback (most recent call last):
File "/Users/hwang/Desktop/Week7.py", line 117, in <module>
main()
File "/Users/hwang/Desktop/Week7.py", line 99, in main
removeRMonths(monthList)
File "/Users/hwang/Desktop/Week7.py", line 110, in removeRMonths
monthList.remove(month)
AttributeError: 'generator' object has no attribute 'remove'
出现这些错误的原因是什么?我尝试用谷歌搜索每条错误消息,但找不到任何我能理解的答案。我是初学者,所以我希望能得到一个容易理解的答案。提前致谢!
您不应该在迭代列表器集时修改它。
首先使用列表理解来创建列表。然后使用另一个列表理解来过滤掉元素。最后,在过滤完成后,使用分片赋值将原始列表更新到位。
monthList = ()
def main():
monthList = fillList()
print(monthList)
removeRMonths(monthList)
def fillList():
infile = open("SomeMonths.txt")
monthList = [line.rstrip() for line in infile]
return monthList
def removeRMonths(monthList):
monthList[:] = [month for month in monthList if 'r' not in month.lower()]
print(monthList)
我得到了一个包含月份的 txt 文件,我将其打开并填写到列表或集合中。我需要遍历列表并删除任何包含字母 'r' 或 "R" 的月份。我尝试使用集合和列表进行此操作,但不断收到 RuntimeError。这是我使用集合的代码:
monthList = {}
def main():
monthList = fillList()
print(monthList)
removeRMonths(monthList)
def fillList():
infile = open("SomeMonths.txt")
monthList = {line.rstrip() for line in infile}
return monthList
def removeRMonths(monthList):
for month in monthList:
for ch in month:
if ch == "r" or ch == "R":
monthList.remove(month)
print(monthList)
main()
我收到的错误是:
Traceback (most recent call last):
File "/Users/hwang/Desktop/Week7.py", line 115, in <module>
main()
File "/Users/hwang/Desktop/Week7.py", line 99, in main
removeRMonths(monthList)
File "/Users/hwang/Desktop/Week7.py", line 107, in removeRMonths
for month in monthList:
RuntimeError: Set changed size during iteration
>>>
这是我尝试使用列表的代码:
monthList = ()
def main():
monthList = fillList()
print(monthList)
removeRMonths(monthList)
def fillList():
infile = open("SomeMonths.txt")
monthList = (line.rstrip() for line in infile)
return monthList
def removeRMonths(monthList):
for month in monthList:
for ch in month:
if ch == "r" or ch == "R":
monthList.remove(month)
else:
continue
print(monthList)
main()
我的错误是:
Traceback (most recent call last):
File "/Users/hwang/Desktop/Week7.py", line 117, in <module>
main()
File "/Users/hwang/Desktop/Week7.py", line 99, in main
removeRMonths(monthList)
File "/Users/hwang/Desktop/Week7.py", line 110, in removeRMonths
monthList.remove(month)
AttributeError: 'generator' object has no attribute 'remove'
出现这些错误的原因是什么?我尝试用谷歌搜索每条错误消息,但找不到任何我能理解的答案。我是初学者,所以我希望能得到一个容易理解的答案。提前致谢!
您不应该在迭代列表器集时修改它。
首先使用列表理解来创建列表。然后使用另一个列表理解来过滤掉元素。最后,在过滤完成后,使用分片赋值将原始列表更新到位。
monthList = ()
def main():
monthList = fillList()
print(monthList)
removeRMonths(monthList)
def fillList():
infile = open("SomeMonths.txt")
monthList = [line.rstrip() for line in infile]
return monthList
def removeRMonths(monthList):
monthList[:] = [month for month in monthList if 'r' not in month.lower()]
print(monthList)