AttributeError: 'list' object has no attribute 'split' cannot figure out error
AttributeError: 'list' object has no attribute 'split' cannot figure out error
问题是不断要求一个人说出他们最喜欢的网站,并在他们输入完成后停止。我们要提取“www”。和“.com”并将该部分附加到列表中,最后打印列表。我不知道我做错了什么。
myweb=[]
while True:
mywebsite=input("what is your favorite website?")
if mywebsite=="done":
print(myweb)
break
else:
myweb.append(mywebsite)
continue
mywebsite=mywebsite.split('www.')
mywebsite=mywebsite.split('.com')
print(mywebsite)
根据你的问题,我做了这个功能
myweb=[]
while True:
mywebsite=input("what is your favorite website?")
if mywebsite=="done":
print(myweb)
break
else:
myweb.append(mywebsite)
continue
# allocating enough space
mywebsite = [0]*len(myweb)
# extract it
for i in range(len(myweb)):
# split based on www.domain.com
mywebsite[i] = myweb[i].split('.')[1]
print(mywebsite)
对列表中的每个项目使用正则表达式
>>> import re
>>> website = 'www.amazon.com'
>>> website = re.search('www\.(.*)\.com',website).group(1)
>>> website
'amazon'
如果所有输入的模式都是www.SOME_WEB_SITES.com,那么你可以使用
websites = [ '.'.join(x.split('.')[1:-1]) for x in a]
也考虑子域的情况,例如:
www.a.com --> a
www.a.b.com --> a.b
x.split('.')[1:-1] # we will get an array ["a","b"]
'.'.join(....) # we need to join them by .
您使用 python 字符串 .strip()
函数来执行此操作。
myweb=[]
while True:
mywebsite=input("what is your favorite website?")
if mywebsite=="done":
for x in myweb:
print(x.strip("www." + ".com")) #Strips www. and .com from the string.
break
else:
myweb.append(mywebsite)
这可以使用正则表达式轻松完成。但是看起来您是 python 的新手。因此,以下更正后的代码可能会对您有所帮助。
myweb_name=[]
while True:
mywebsite=input("what is your favorite website?")
if mywebsite=="done":
break
else:
myweb_name.append(mywebsite.split('www.')[1] # remove the www part
.split('.com')[0]) # remove the .com part
continue
print(myweb_name)
问题是不断要求一个人说出他们最喜欢的网站,并在他们输入完成后停止。我们要提取“www”。和“.com”并将该部分附加到列表中,最后打印列表。我不知道我做错了什么。
myweb=[]
while True:
mywebsite=input("what is your favorite website?")
if mywebsite=="done":
print(myweb)
break
else:
myweb.append(mywebsite)
continue
mywebsite=mywebsite.split('www.')
mywebsite=mywebsite.split('.com')
print(mywebsite)
根据你的问题,我做了这个功能
myweb=[]
while True:
mywebsite=input("what is your favorite website?")
if mywebsite=="done":
print(myweb)
break
else:
myweb.append(mywebsite)
continue
# allocating enough space
mywebsite = [0]*len(myweb)
# extract it
for i in range(len(myweb)):
# split based on www.domain.com
mywebsite[i] = myweb[i].split('.')[1]
print(mywebsite)
对列表中的每个项目使用正则表达式
>>> import re
>>> website = 'www.amazon.com'
>>> website = re.search('www\.(.*)\.com',website).group(1)
>>> website
'amazon'
如果所有输入的模式都是www.SOME_WEB_SITES.com,那么你可以使用
websites = [ '.'.join(x.split('.')[1:-1]) for x in a]
也考虑子域的情况,例如:
www.a.com --> a
www.a.b.com --> a.b
x.split('.')[1:-1] # we will get an array ["a","b"]
'.'.join(....) # we need to join them by .
您使用 python 字符串 .strip()
函数来执行此操作。
myweb=[]
while True:
mywebsite=input("what is your favorite website?")
if mywebsite=="done":
for x in myweb:
print(x.strip("www." + ".com")) #Strips www. and .com from the string.
break
else:
myweb.append(mywebsite)
这可以使用正则表达式轻松完成。但是看起来您是 python 的新手。因此,以下更正后的代码可能会对您有所帮助。
myweb_name=[]
while True:
mywebsite=input("what is your favorite website?")
if mywebsite=="done":
break
else:
myweb_name.append(mywebsite.split('www.')[1] # remove the www part
.split('.com')[0]) # remove the .com part
continue
print(myweb_name)