简体   繁体   English

如何从 python 的列表中删除某些元素?

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

I'm scraping some data from a website but some of it is coming out with "\" in front of it.我正在从网站上抓取一些数据,但其中一些数据前面带有“\”。 I tried to use this string of code but an error message occurred.我尝试使用此代码字符串,但出现错误消息。

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

The code after the '\' in the first line became italicized, I have no clue what to do about this.第一行中'\'之后的代码变成了斜体,我不知道该怎么做。

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)

So I tried to extract your code, and this:所以我试图提取你的代码,这是:

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)

gives me the following output:给我以下 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"]

From your question, I assume this is what you want?根据您的问题,我认为这就是您想要的?

Anyway, the problem (as far as I could see) was simply wrong usage of the strip() function, which expects a string (containing the part of the source string you want to strip), not just a single character.无论如何,问题(据我所见)只是错误使用strip() function,它需要一个字符串(包含您要剥离的源字符串的一部分),而不仅仅是一个字符。

Until you post your code, it's hard to figure out the bug as to why your array is improperly formatted.在您发布代码之前,很难弄清楚为什么您的数组格式不正确的错误。 However, you can use the following to fix this array:但是,您可以使用以下方法来修复此数组:

#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)

This above code gives me x as:上面的代码给了我 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.

相关问题 Python:如何从字典中删除元素并将其作为列表返回? - Python: How do I remove elements from a dictionary and return it as a list? Python:如何将列表中以某个字母开头的元素复制到新列表中或从列表中删除不以字母开头的元素 - Python: How to copy elements from list that start with a certain letter into new list or remove elements from list that do not start with letter 如何跳过 python for 循环中列表中的某些元素? - How do I skip certain elements in a list in a python for loop? 如何从列表中删除所有元素,该列表是 python 中同一列表中另一个更大元素的子序列? - How do I remove all elements from a list which is a subsequence of another bigger element in the same list in python? 在 Python 中,如何从列表中删除包含某些类型字符的任何元素? - In Python, how do I remove from a list any element containing certain kinds of characters? 如果字符串不包含 Python 中的某些字符,我如何从列表中删除它 - How do i remove a string from a list if it DOES NOT contain certain characters in Python 如何从列表中删除某些值的对象? - How do I remove objects of certain values from a list? 在Python中,如何从列表列表中删除某个元素? - In Python, how can I remove a certain element from a list of lists? 如何从 Python 中的嵌套字典中删除某些键? - How do I remove certain keys from a nested dictionary in Python? 如何从python中的图像中删除某些文本? - How do I remove certain text from an image in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM