gpt4 book ai didi

python - 从 Python 列表中获取整数

转载 作者:行者123 更新时间:2023-11-28 21:58:38 25 4
gpt4 key购买 nike

我是编程新手,刚开始学习 Python。我在下面写的代码是一个程序,让你输入一个等级(例如 14/20 或 70 超过 100),然后给你一个从 A 到 E 的等级。基本上我想知道的是如果有一种方法可以“取出”列表中的整数,而不是一个一个地取出它们并将它们乘以 10、100 等等。

我总结一下:我希望等级 ["14 sur 20"] 变成 a = 14 和 b =20,而不必编写我编写的所有代码。

PS:我想我的代码太长了,但我是 Python 的新手,我还没有足够的知识来缩短它,所以不要太难 ;)

import os

grade = input ("Entrez votre note :")
deter = []
redet = []

i = z = a = b = x = 0

while i < len(grade):
if grade[i] == "s" and grade[i+1] == "u" and grade [i+2] == "r" : #checking if the grade is written as " x sur y"
while z < i-1 : #building a list for the grade
deter.append (grade[z])
z += 1
z += 5 #jumping to the scale
while z < len(grade) : #building a list for the scale
redet.append (grade[z])
z += 1

elif grade[i] == "/" : #means grade is written as "x/y"
while z < i : #building a list for the grade
deter.append (grade[z])
z += 1
z += 1 #jumping to the scale
while z < len(grade) : #building a list for the scale
redet.append (grade[z])
z += 1

i += 1

redet = list (map(float, redet)) #converting to integers
deter = list (map(float, deter))

if len(deter)>1 :
y = 10**(len(deter)-1)
else:
y = 1

while x < len(deter) : #making variables
a = a + deter[x]*y
x += 1
y /= 10

x = 0
if len(redet)>1 :
y = 10**(len(redet)-1)
else :
y = 1

while x < len(redet) : #making variables
b = b + redet[x]*y
x += 1
y /= 10

grade = a/b
if grade >= 0.8 :
print("A")
elif grade >= 0.6 :
print("B")
elif grade >= 0.5 :
print("C")
elif grade >= 0.4 :
print("D")
elif grade <= 0.4 :
print("E")

os.system ("pause")

最佳答案

您可以使用 re.split14 sur 2014/20 等字符串分成两部分。

您可以使用 bisect.bisect 将分数转换为字母等级。

import bisect
import re

def lettergrade(score, breakpoints = [40, 50, 60, 80], grades = 'EDCBA'):
"""
>=80 -> A
>=60 -> B
>=50 -> C
>=40 -> D
else -> E
"""
i = bisect.bisect(breakpoints, score)
return grades[i]


grade = input("Entrez votre note : ")
a, b = map(int, re.split(r'sur|/', grade))
print(lettergrade(100.0*a/b))

正则表达式模式的解释:

`re.split(r'sur|/', grade)` splits the string `grade` into a list of strings. It splits on the regex pattern `r'sur|/'`. This regex pattern matches the literal string `sur` or the forward-slash `/`. The `|` is the regex syntax for "or". 

'sur|/' 前面的 r 是 Python 语法,它导致 Python 将 'sur|/' 解释为 raw string .这会影响反斜杠的解释方式。 docs for the re module这样解释它的用法:

Regular expressions use the backslash character ('\') to indicate special forms or to allow special characters to be used without invoking their special meaning. This collides with Python’s usage of the same character for the same purpose in string literals; for example, to match a literal backslash, one might have to write '\\\\' as the pattern string, because the regular expression must be \\, and each backslash must be expressed as \\ inside a regular Python string literal.

The solution is to use Python’s raw string notation for regular expression patterns; backslashes are not handled in any special way in a string literal prefixed with 'r'. So r"\n" is a two-character string containing '\' and 'n', while "\n" is a one-character string containing a newline. Usually patterns will be expressed in Python code using this raw string notation.

有关原始字符串的完整故事,请参阅 the language reference doc

尽管在这种情况下原始字符串 r'sur|/' 与普通字符串 'sur|/' 相同,但始终使用原始字符串制作正则表达式模式。在这种情况下没有坏处,在其他情况下肯定有帮助。

由于 re.split 返回一个字符串列表,map(int, ...) 用于将字符串转换为 int :

In [37]: grade = '14 sur 20'

In [38]: re.split(r'sur|/', grade)
Out[38]: ['14 ', ' 20']

In [39]: map(int, re.split(r'sur|/', grade))
Out[39]: [14, 20]

关于python - 从 Python 列表中获取整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17867146/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com