[英]Replace integers in a list with a string Python
I am working on a coding assignment in Python and I am struggling to understand why my code is not working.我正在使用 Python 进行编码作业,我正在努力理解为什么我的代码不起作用。
Write a function in Python called "BooHoo" which takes in an integer, n, and stores all integers from 1 to n in a list.用 Python 编写一个名为“BooHoo”的函数,它接受一个整数 n,并将从 1 到 n 的所有整数存储在一个列表中。 However, if an integer in the list is divisible by 3, the list should instead contain "Boo".但是,如果列表中的整数可被 3 整除,则该列表应改为包含“Boo”。 If the integer is divisible by 5 the list should instead contain "Hoo".如果整数可被 5 整除,则列表应改为包含“Hoo”。 If the integer is divisible by both 3 and 5, the list should instead contain "BooHoo".如果整数可被 3 和 5 整除,则列表应改为包含“BooHoo”。
def BooHoo(n):
'''
A function which returns a list of integers, and replaces integers divisible by 3 with "Boo" and integers
divisible by 5 with "Hoo" and integers divisible by 3 and 5 with "BooHoo"
Parameters
-------
n: an integer
Returns
--------
final_list: a Python list
'''
main_list = []
for x in range(n):
main_list.append(x)
for idx, j in enumerate(main_list):
if not (idx % 3) and not (idx % 5):
main_list.pop(idx)
main_list.insert(idx, 'BooHoo')
elif not (idx % 3):
main_list.pop(idx)
main_list.insert(idx, 'Boo')
elif not (idx % 5):
main_list.pop(idx)
main_list.insert(idx, 'Hoo')
else:
continue
final_list = [main_list]
return final_list
There were some logical errors regarding the index and actual elements of the list.关于列表的索引和实际元素存在一些逻辑错误。 I have highlighted the modified/added lines by a comment #
.我通过注释#
突出显示了修改/添加的行。 Mainly, you needed to replace idx
by j
because idx
was an index but j
was an actual element.主要是,您需要用j
替换idx
,因为idx
是一个索引,而j
是一个实际元素。 If you start by range(n)
, it doesn't matter because the index is the same as j
.如果您从range(n)
,则没有关系,因为索引与j
相同。 But since you mentioned in your question, you want to store numbers from 1
up to n
, you need to use range(1, n+1)
但是由于您在问题中提到,您想存储从1
到n
数字,您需要使用range(1, n+1)
def BooHoo(n):
main_list = []
for x in range(1,n+1): # replaced range(n) to start from 1
main_list.append(x)
for idx, j in enumerate(main_list):
if not (j % 3) and not (j % 5): # replaced idx by j
main_list.pop(idx)
main_list.insert(idx, 'BooHoo')
elif not (j % 3): # replaced idx by j
main_list.pop(idx)
main_list.insert(idx, 'Boo')
elif not (j % 5): # replaced idx by j
main_list.pop(idx)
main_list.insert(idx, 'Hoo')
else:
continue
return main_list # Removed unnecessary second list
# Call the function
print (BooHoo(15))
Output输出
[1, 2, 'Boo', 4, 'Hoo', 'Boo', 7, 8, 'Boo', 'Hoo', 11, 'Boo', 13, 14, 'BooHoo']
A better way to solve your problem is to construct the right list from the start (with a loop or list comprehension) than to modify a sequential list of numbers.解决问题的更好方法是从一开始(使用循环或列表推导式)构建正确的列表,而不是修改数字的顺序列表。
def BooHoo(n):
return ['BooHoo' if not (i % 5 or i % 3) else
'Hoo' if not i % 5 else
'Boo' if not i % 3 else
i for i in range(1,n+1)]
And, more for the sake of fun than usefulness, a dictionary-based solution:而且,更多的是为了好玩而不是有用,一个基于字典的解决方案:
def BooHoo(n):
words = {(1,1): 'BooHoo', (1,0): 'Boo', (0,1): 'Hoo'}
return [words.get((i % 3 == 0, i % 5 == 0), i) for i in range(1, n+1)]
Why go through all that trouble of appending and popping so much, thats not necessary为什么要经历这么多附加和弹出的麻烦,那没有必要
def BooHoo(n):
for index, item in enumerate(list(range(1, (n+1))):
if not item % 5 and not item % 3:
lista[index] = "BooHoo"
elif not item % 3:
lista[index] = "Boo"
elif not item % 5:
lista[index] = "Hoo"
else:
pass
return lista
print(BooHoo(25))
Output输出
(xenial)vash@localhost:~/python/stack_overflow$ python3.7 boohoo.py [1, 2, 'Boo', 4, 'Hoo', 'Boo', 7, 8, 'Boo', 'Hoo', 11, 'Boo', 13, 14, 'BooHoo', 16, 17, 'Boo', 19, 'Hoo', 'Boo', 22, 23, 'Boo', 'Hoo']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.