def removeDuplicates(somelist): list = [] for i in somelist: if i not in list: list.append(i) def main(): somelist = [1, 1, 2, 3, 3, 4, 3, 5] print("原来的列表为:", somelist) removeDuplicates(somelist) print("现在的列表为:", list) if __name__=="__main__": main()哪里不对
时间: 2023-11-27 19:04:29 浏览: 40
你的代码有两个问题:
1. `list` 是 Python 内置函数名,不能用作变量名,否则会覆盖内置函数,导致后续出现错误。应该使用其他变量名。
2. `removeDuplicates` 函数没有返回值,因此在 `main` 函数中无法获取去重后的列表。可以将去重后的列表作为返回值,然后在 `main` 函数中接收返回值,并打印输出。
以下是修改后的代码:
```python
def removeDuplicates(somelist):
new_list = []
for i in somelist:
if i not in new_list:
new_list.append(i)
return new_list
def main():
somelist = [1, 1, 2, 3, 3, 4, 3, 5]
print("原来的列表为:", somelist)
new_list = removeDuplicates(somelist)
print("现在的列表为:", new_list)
if __name__=="__main__":
main()
```
运行结果如下:
```
原来的列表为: [1, 1, 2, 3, 3, 4, 3, 5]
现在的列表为: [1, 2, 3, 4, 5]
```
相关问题
import collections def solve_method(n: int) -> None: asc = True list_ = collections.deque() x = 1 for i in range(1, n + 1): arr = [0] * i if asc: for j in range(i): arr[j] = x x += 1 else: for j in range(i - 1, -1, -1): arr[j] = x x += 1 list_.appendleft(arr) asc = not asc res = [] head = "" for ints in list_: content = head for j in range(len(ints)): num = ints[j] content += str(num) content += "*" * (4 - len(str(num))) if j != len(ints) - 1: content += " " res.append(content) head += " " for s in reversed(res): print(s) if __name__ == "__main__": n = int(input()) solve_method(n) 改写以上代码,python
import collections
def solve_method(n):
asc = True
list_ = collections.deque()
x = 1
for i in range(1, n + 1):
arr = [0] * i
if asc:
for j in range(i):
arr[j] = x
x += 1
else:
for j in range(i - 1, -1, -1):
arr[j] = x
x += 1
list_.appendleft(arr)
asc = not asc
res = []
head = ""
for ints in list_:
content = head
for j in range(len(ints)):
num = ints[j]
content += str(num)
content += "*" * (4 - len(str(num)))
if j != len(ints) - 1:
content += " "
res.append(content)
head += " "
for s in reversed(res):
print(s)
if __name__ == "__main__":
n = int(input())
solve_method(n)
import requests import re def getHTMLText(url): try: headers={'User - Agent': 'Mozilla/5.0 (Window NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/77.0.3865.120 Safari/537.36 chrome-extension'} r=requests.get(url,headers=headers) r.raise_for_status() r.encoding=r.apparent_encoding return r.text except: print("Erro_get") #用于提取所需的电影信息 def parsePage(ilt,html): tlt=re.findall(r'data-title\=\".* ?\"',html) for i in range(len(tlt)): plt=eval(tlt[i].split('=')[1]) if plt in ilt: pass else: ilt.append(plt) def printInfo(ilt): print("上 海 热 映") for i in ilt: print(i) def main(): url='https://movie.douban.com/cinema/nowplaying/shanghai/' list=[] html=getHTMLText(url) parsePage(list,html) printInfo(list) main()
这段代码没有语法错误,但是有一些需要注意的问题:
1. 在 headers 中,User-Agent 后面应该有一个冒号,而不是空格。
2. 在函数 parsePage 中,应该避免使用 eval 函数,因为它会执行字符串中的代码,存在安全隐患。应该使用正则表达式提取出电影名称。
3. 在函数 printInfo 中,电影名称前面多了几个空格,可以使用字符串的 strip 方法进行去除。
修正后的代码如下:
```
import requests
import re
def getHTMLText(url):
try:
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36 chrome-extension'}
r = requests.get(url, headers=headers)
r.raise_for_status()
r.encoding = r.apparent_encoding
return r.text
except:
print("Erro_get")
# 用于提取所需的电影信息
def parsePage(ilt, html):
tlt = re.findall(r'data-title="(.*?)"', html)
for i in range(len(tlt)):
plt = tlt[i]
if plt in ilt:
pass
else:
ilt.append(plt)
def printInfo(ilt):
print("上海热映")
for i in ilt:
print(i.strip()) # 去除电影名称前后的空格
def main():
url = 'https://movie.douban.com/cinema/nowplaying/shanghai/'
movie_list = []
html = getHTMLText(url)
parsePage(movie_list, html)
printInfo(movie_list)
main()
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)