将整数列表,数字字符串转换为整数列表
converting list of integers ,numerical strings into a integer list
名为 solve 的给定方法将列表 A 作为参数。
此列表包含以下类型的值:
integers (between 1 and 99, both inclusive)
numbers in form of strings (between 1 and 99, both inclusive) eg, '37', '91'
word form of numbers (between 1 and 99, both inclusive) eg, 'twenty', 'seventeen', 'forty-two'.
请注意,单词总是小写!
必须生成一个列表,其中包含所有整数形式的值,并且 return 它。该列表必须按升序排序。
例子
输入:
[1, 3, '4', '1', 'three', 'eleven', 'forty-seven', '3']
输出:
[1, 3, 4, 11, 47]
代码:
def solve(A):
for i in A:
if type(A[i])==int:
return A.extend(A[i])
elif type(A[i])==str:
return int(A[i])
我们甚至需要使用集合来删除重复项而且它不起作用。 !谁能帮忙写代码!
下面是我用代码注释解释的方法。
def solve(lst):
# Dictionary of words to numbers
NUMBERS_DIC = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6, 'seven': 7, 'eight': 8, 'nine': 9, 'ten': 10, 'eleven': 11, 'twelve': 12, 'thirteen': 13, 'fourteen': 14, 'fifteen': 15, 'sixteen': 16, 'seventeen': 17, 'eighteen': 18, 'nineteen': 19, 'twenty': 20, 'thirty': 30, 'forty': 40, 'fifty': 50, 'sixty': 60, 'seventy': 70, 'eighty': 80, 'ninety': 90}
# list to be returned
retLst = []
for num in lst:
# if num is an int
if type(num)==int: # type() tells you the type of the variable
retLst.append(num)
# if num is a string that is numeric
elif num.isnumeric():
retLst.append(int(num)) # Turn to int
# if num is a string word such as forty-seven"
else:
# turns "forty-seven" -> ["forty","seven"]
word_values_lst = num.split("-")
val = 0
for word in word_values_lst:
val+=NUMBERS_DIC[word] # Get value from NUMBERS_DIC
retLst.append(val)
# Sort lst
retLst.sort()
return list(set(retLst)) # removes duplicates
lst = [1, 3, '4', '1', 'three', 'eleven', 'forty-seven', '3']
retLst = solve(lst)
print(retLst)
名为 solve 的给定方法将列表 A 作为参数。
此列表包含以下类型的值:
integers (between 1 and 99, both inclusive)
numbers in form of strings (between 1 and 99, both inclusive) eg, '37', '91'
word form of numbers (between 1 and 99, both inclusive) eg, 'twenty', 'seventeen', 'forty-two'.
请注意,单词总是小写!
必须生成一个列表,其中包含所有整数形式的值,并且 return 它。该列表必须按升序排序。
例子
输入:
[1, 3, '4', '1', 'three', 'eleven', 'forty-seven', '3']
输出:
[1, 3, 4, 11, 47]
代码:
def solve(A):
for i in A:
if type(A[i])==int:
return A.extend(A[i])
elif type(A[i])==str:
return int(A[i])
我们甚至需要使用集合来删除重复项而且它不起作用。 !谁能帮忙写代码!
下面是我用代码注释解释的方法。
def solve(lst):
# Dictionary of words to numbers
NUMBERS_DIC = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6, 'seven': 7, 'eight': 8, 'nine': 9, 'ten': 10, 'eleven': 11, 'twelve': 12, 'thirteen': 13, 'fourteen': 14, 'fifteen': 15, 'sixteen': 16, 'seventeen': 17, 'eighteen': 18, 'nineteen': 19, 'twenty': 20, 'thirty': 30, 'forty': 40, 'fifty': 50, 'sixty': 60, 'seventy': 70, 'eighty': 80, 'ninety': 90}
# list to be returned
retLst = []
for num in lst:
# if num is an int
if type(num)==int: # type() tells you the type of the variable
retLst.append(num)
# if num is a string that is numeric
elif num.isnumeric():
retLst.append(int(num)) # Turn to int
# if num is a string word such as forty-seven"
else:
# turns "forty-seven" -> ["forty","seven"]
word_values_lst = num.split("-")
val = 0
for word in word_values_lst:
val+=NUMBERS_DIC[word] # Get value from NUMBERS_DIC
retLst.append(val)
# Sort lst
retLst.sort()
return list(set(retLst)) # removes duplicates
lst = [1, 3, '4', '1', 'three', 'eleven', 'forty-seven', '3']
retLst = solve(lst)
print(retLst)