簡體   English   中英

如何從 python 的列表中刪除某些元素?

[英]How do I remove certain elements from a list in python?

我正在從網站上抓取一些數據,但其中一些數據前面帶有“\”。 我嘗試使用此代碼字符串,但出現錯誤消息。

print([s.strip('\') for s in feet])    *EOL while scanning string literal
print([s.replace('\', ') for s in feet])

第一行中'\'之后的代碼變成了斜體,我不知道該怎么做。

from lxml import html
import requests

list1 = []
height = []

user_website = "https://www.disabled-world.com/calculators-charts/height-weight.php"

page = requests.get(user_website)
tree = html.fromstring(page.content)
list2 = tree.xpath('//td/text()')

for x in list2:
    list_holder = x.split(" ")
    for i in list_holder:
        list1.append(i.lower())

subs = "'"
feet = [i for i in list2 if subs in i]

subs2 = '"'
inches = [i for i in list2 if subs in i]

print([s.strip('\') for s in feet])
print([s.replace('\', ') for s in feet])

y = 0

for x in feet:
    height.append(feet[y])
    height.append(inches[y])
    y+=1

print(height)

所以我試圖提取你的代碼,這是:

from lxml import html 
import requests 

list1 = [] 
height = [] 
user_website = "https://disabled-world.com/calculators-charts/height-weight.php" 
page = requests.get(user_website) 
tree = html.fromstring(page.content) 
list2 = tree.xpath('//td/text()')

for x in list2: 
    list_holder = x.split(" ") 
    for i in list_holder: 
        list1.append(i.lower()) 
subs = "'" 
feet = [i for i in list2 if subs in i] 
subs2 = '"' 
inches = [i for i in list2 if subs in i] 
print([s.strip('"') for s in feet]) 
#print([s.replace('\', ') for s in feet]) 
y = 0 

#for x in feet: 
#    height.append(feet[y]) 
#    height.append(inches[y]) 
#    y+=1 
#    print(height)

給我以下 output:

["4' 6", "4' 7", "4' 8", "4' 9", "4' 10", "4' 11", "5' 0", "5' 1", "5' 2", "5' 3", "5' 4", "5' 5", "5' 6", "5' 7", "5' 8", "5' 9", "5' 10", "5' 11", "6' 0", "6' 1", "6' 2", "6' 3", "6' 4", "6' 5", "6' 6", "6' 7", "6' 8", "6' 9", "6' 10", "6' 11", "7' 0"]

根據您的問題,我認為這就是您想要的?

無論如何,問題(據我所見)只是錯誤使用strip() function,它需要一個字符串(包含您要剝離的源字符串的一部分),而不僅僅是一個字符。

在您發布代碼之前,很難弄清楚為什么您的數組格式不正確的錯誤。 但是,您可以使用以下方法來修復此數組:

#copy paste the data into a variable as a string using triple quotes (or convert this variable to a string)
a='''['4' 6"', '4' 6"', '4' 7"', '4' 7"', '4' 8"', '4' 8"', '4' 9"', '4' 9"', '4' 10"', '4' 10"', '4' 11"', '4' 11"', '5' 0"', '5' 0"', '5' 1"', '5' 1"', '5' 2"', '5' 2"', '5' 3"', '5' 3"', '5' 4"', '5' 4"', '5' 5"', '5' 5"', '5' 6"', '5' 6"', '5' 7"', '5' 7"', '5' 8"', '5' 8"', '5' 9"', '5' 9"', '5' 10"', '5' 10"', '5' 11"', '5' 11"', '6' 0"', '6' 0"', '6' 1"', '6' 1"', '6' 2"', '6' 2"', '6' 3"', '6' 3"', '6' 4"', '6' 4"', '6' 5"', '6' 5"', '6' 6"', '6' 6"', '6' 7"', '6' 7"', '6' 8"', '6' 8"', '6' 9"', '6' 9"', '6' 10"', '6' 10"', '6' 11"', '6' 11"', '7' 0"', '7' 0"']'''

m=a.strip('[').strip(']')  #remove braces
x=[]                       
n=m.split(',')             #creaet list of elements
for i in n:
    x.append(i.strip(" ").strip("'"))  #remove the excessive quotes and spaces
print(x)

上面的代碼給了我 x 為:

['4\' 6"', '4\' 6"', '4\' 7"', '4\' 7"', '4\' 8"', '4\' 8"', '4\' 9"', '4\' 9"', '4\' 10"', '4\' 10"', '4\' 11"', '4\' 11"', '5\' 0"', '5\' 0"', '5\' 1"', '5\' 1"', '5\' 2"', '5\' 2"', '5\' 3"', '5\' 3"', '5\' 4"', '5\' 4"', '5\' 5"', '5\' 5"', '5\' 6"', '5\' 6"', '5\' 7"', '5\' 7"', '5\' 8"', '5\' 8"', '5\' 9"', '5\' 9"', '5\' 10"', '5\' 10"', '5\' 11"', '5\' 11"', '6\' 0"', '6\' 0"', '6\' 1"', '6\' 1"', '6\' 2"', '6\' 2"', '6\' 3"', '6\' 3"', '6\' 4"', '6\' 4"', '6\' 5"', '6\' 5"', '6\' 6"', '6\' 6"', '6\' 7"', '6\' 7"', '6\' 8"', '6\' 8"', '6\' 9"', '6\' 9"', '6\' 10"', '6\' 10"', '6\' 11"', '6\' 11"', '7\' 0"', '7\' 0"']

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM