[英]Returning values from a function in Python3 with correct formatting
working on a coding program that I think I have licked and I'm getting the correct values for.开发一个我认为我已经完成并且我得到正确值的编码程序。 However the test conditions are looking for the values formatted in a different way.
然而,测试条件正在寻找以不同方式格式化的值。 And I'm failing at figuring out how to format the return in my function correctly.
而且我无法弄清楚如何正确格式化我的 function 中的返回格式。
I get the values correctly when looking for the answer:在寻找答案时,我得到了正确的值:
[4, 15, 7, 19, 1, 20, 13, 9, 4, 14, 9, 7, 8, 20] [4、15、7、19、1、20、13、9、4、14、9、7、8、20]
but the test condition expects但测试条件期望
should equal '20 8 5 14 1 18 23 8 1 12 2 1 3 15 14 19 1 20 13 9 4 14 9 7 8 20'应该等于'20 8 5 14 1 18 23 8 1 12 2 1 3 15 14 19 1 20 13 9 4 14 9 7 8 20'
and for the life of me I haven't been able to figure this out yet.对于我的一生,我还没有弄清楚这一点。
View the original problem here: https://www.codewars.com/kata/546f922b54af40e1e90001da/train/python在此处查看原始问题: https://www.codewars.com/kata/546f922b54af40e1e90001da/train/python
Still very new to Python, but tackling these problems best I can.对 Python 来说仍然很新,但我可以尽我所能解决这些问题。 Code may be ugly, but it's mine =D
代码可能很难看,但它是我的 =D
EDIT: I am looking for a way to reformat my return statement as a string instead of a list of integers.编辑:我正在寻找一种将我的 return 语句重新格式化为字符串而不是整数列表的方法。
Thanks for the help in advance, Any help is appreciated.提前感谢您的帮助,感谢任何帮助。 even how to post better questions here.
甚至如何在这里发布更好的问题。
Koruptedkernel.损坏的内核。
import string
def alphabet_position(positions):
#Declaring final position list.
position = []
#Stripping punctuation from the passed string.
out1 = positions.translate(str.maketrans("","", string.punctuation))
#Stripping digits from the passed string.
out = out1.translate(str.maketrans("","", string.digits))
#Removing Spaces from the passed string.
outter = out.replace(" ","")
#reducing to lowercase.
mod_text = str.lower(outter)
#For loop to iterate through alphabet and report index location to position list.
for letter in mod_text:
#Declare list of letters (lower) in the alphabet (US).
alphabet = list('abcdefghijklmnopqrstuvwxyz')
position.append(alphabet.index(letter) + 1)
return(position)
#Call the function with text.
alphabet_position("The sunset sets at twelve o'clock.")
Here's your homework with some explanations (uses a comprehension which are the BEST):这是您的家庭作业和一些解释(使用最好的理解):
# full alphabet (is technically a list)
alphabet = 'abcdefghijklmnopqrstuvwxyz'
# letter and index+1 for each letter
alphabet_dict = {u: str(i + 1) for i, u in enumerate(alphabet)}
def alphabet_position(text):
# map the text chars to their corresponding aplhabet_dict item
pos_list = [alphabet_dict.get(char.lower())
for char in text if char.lower() in alphabet_dict]
# convert to the insane string format they asked for
return ' '.join(pos_list)
alphabet_position("The sunset sets at twelve o' clock.")
Output: Output:
'20 8 5 19 21 14 19 5 20 19 5 20 19 1 20 20 23 5 12 22 5 15 3 12 15 3 11'
Had some great homework with dictionaries and tried and successfully did it THAT way.用字典做了一些很棒的作业,并尝试并成功地做到了这一点。
Went back to the original way I did it (not as clean or efficient), but managed to get the list successfully converted by changing my for loop to回到我原来的方式(不是干净或高效),但通过将我的 for 循环更改为
position.append(str(alphabet.index(letter) + 1))
and then used然后用
return ' '.join(position)
to get the answer in the format they were test for.以他们测试的格式获得答案。
YES!是的! I SOLVED A THING!
我解决了一个问题!
Thanks for all the help, I will try and get better at formulating questions to get better help.感谢所有的帮助,我会努力更好地提出问题以获得更好的帮助。
TYTY TYTY
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.