简体   繁体   English

如何删除列表中项目中的字符(Python)

[英]How can I delete a character in an item in a list (Python)

I'm learning python and I've made a quick grade calculator that checks if the first letter is D for daily grade, or M for major grade (Major is weighed differently than daily). 我正在学习python,并且已经制作了一个快速成绩计算器,该计算器可以检查第一个字母的日常成绩是D,还是主要成绩的M(主要指标与每日的称谓不同)。 However I need to actually remove the D and M once I move it to their respective lists. 但是,一旦我将D和M移到它们各自的列表中,我实际上就需要删除它。 How would I go about doing this? 我将如何去做呢? Do me a favor and keep it simple :D. 帮我一个忙,并保持简单:D。

grades = ['D 85', 'D 100', 'M 20', 'D 70']

I have code that puts the Daily grades in the proper list and vice versa for major. 我的代码可以将“每日”成绩放入适当的列表,反之亦然。

Daily = ['D 85', 'D 100', 'D 70']
Major = ['M 20']

Now I need to remove the D and the M from each item in the list. 现在,我需要从列表中的每个项目中删除D和M。 daily.replace didn't work and I tried a daily.replace无效,我尝试了

for i in list: 
   for c in i:
      newdaily.replace('D',"")

And same for the major list. 与主要清单相同。 How would I go about this? 我将如何处理?

It seems like really you want to be splitting the string and taking the second of the two parts. 似乎您确实想拆分字符串并采用两个部分中的第二个。 If you take a single string you can do so with 如果您使用单个字符串,则可以使用

'D 85'.split()[1] # "85"

To do this to the whole list: 要对整个列表执行此操作:

grades = ['D 85', 'D 100', 'M 20', 'D 70']
numeric_grades = [g.split()[1] for g in grades]

Know that there is nothing in the above to convert the strings to int s, but that can be easily added 知道上面没有任何将字符串转换为int的东西,但是可以轻松添加

numeric_grades = [int(g.split()[1]) for g in grades]

It's not so matter of a replacement, as a string cut. 它不是一个替换的问题,而是一个字符串切割。 From what I understand, you want cut first character or two? 据我了解,您想删掉第一个字符还是两个?

grade = "D 85"
grade[2:]
> "85"

String is an array, and you can ask for some part of it. 字符串是一个数组,您可以要求它的一部分。 [2:] means since third (we count from 0) character to end. [2:]表示自第三个字符(我们从0开始计数)到结尾。

EDIT: To process all grades and remove first character and store result in new list ( gradesClean ): 编辑:处理所有成绩并删除第一个字符并将结果存储在新列表中( gradesClean ):

grades = ["D85", "D100", "M20", "D70"]
gradesClean = []
for g in grades:
    gradesClean.append(g[1:])

gradesClean
> ['85', '100', '20', '70']

You could try something like this. 您可以尝试这样的事情。

raw_grades = ["D 85", "D 100", "M 20", "D 70"]
chars_to_replace = ["D", "M"]

sanitized_grades = []

for i in raw_grades:
    for char in chars_to_replace:
        i = i.replace(char,"")

    sanitized_grades.append(i.strip())

print(sanitized_grades) # ['85', '100', '20', '70']

Hope this helps. 希望这可以帮助。

In-buit strip() function would also work here, with list-comprehension. 内置的strip()函数在这里也可以使用list-comprehension。

>>> grades = ["D 85", "D 100", "M 20", "D 70"]
>>> grades_num = [ele.strip('D ').strip('M ') for ele in raw_grades]
['85', '100', '20', '70']  # output

You can use remove Example: 您可以使用remove示例:

list=['A','B','C']

list.remove('A')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM