[英]Python function sublist from input with while loop
My problem is this: Write a function, sublist, that takes in a list of numbers as the parameter.我的问题是:写一个函数,sublist,接受一个数字列表作为参数。 In the function, use a while loop to return a sublist of the input list.在函数中,使用 while 循环返回输入列表的子列表。 The sublist should contain the same values of the original list up until it reaches the number 5 (it should not contain the number 5).子列表应包含与原始列表相同的值,直到达到数字 5(它不应包含数字 5)。
I tinker with it and I get the question partially correct sometimes.我修改它,有时我会部分正确地回答问题。
def sublist(x):
a = [int(x) for x in input()]
while x < 5:
x = x + 1
return(x)
Try:尝试:
import itertools
def sublist(x):
return list(itertools.takewhile(lambda n: n != 5, x))
Update : If this is a homework question, my answer won't work for you - but nor should we just give you an answer , so, look at while
and break
.更新:如果这是一个家庭作业问题,我的回答对你不起作用——但我们也不应该只给你一个答案,所以,看看while
和break
。 Think about creating an empty list to start with, adding things to it until you need to stop, then returning it.考虑从创建一个空列表开始,向其中添加内容直到您需要停止,然后返回它。
If you want to use a while
loop with a check on the number value, you'd better create a generator from the input list and use next()
to iterate over it:如果你想使用一个while
循环来检查数字值,你最好从输入列表创建一个生成器并使用next()
迭代它:
def sublist(x):
sub = []
x = (num for num in x) # create a generator
num = next(x, 5)
while num != 5:
sub.append(num)
num = next(x, 5) # iterate
return sub
x = [1, 3, 4, 5, 1, 2, 3]
sublist(x)
>>> [1, 3, 4]
def sublist(lst):
sub = lst
for i in lst:
if i == 5:
x = lst.index(i)
sub = lst[:x]
return sub
list3 = [1,56,34,2,3,1,4,5,5,5]
sublist(list3)
I want to give an answer with using slicing and index method.我想用切片和索引方法给出答案。 that's very useful for Python beginners.这对 Python 初学者非常有用。
If you are looking to write a function that takes every element of a list until the number 5 (eg [1, 2, 3, 4, 5] -> [1, 2, 3, 4]) then you could do that like this:如果你想写一个函数来获取列表中的每个元素直到数字 5(例如 [1, 2, 3, 4, 5] -> [1, 2, 3, 4])那么你可以这样做这个:
def sublist(input_list):
output_list = []
index = 0
while index < len(input_list):
if input_list[index] != 5:
output_list.append(input_list[index])
index += 1
else:
break
return output_list
The while loop is broken when you reach 5. Until then, each value is added to a new list which is then returned by the function.当你到达 5 时,while 循环被打破。直到那时,每个值都被添加到一个新列表中,然后由函数返回。
Update: Change while condition to check index is less than the length of the input list更新:更改条件以检查索引小于输入列表的长度
It seems like you don't understand the question.好像你不明白这个问题。
Write a function,
sublist
, that takes a list of numbers as the parameter.编写一个函数sublist
,它将数字列表作为参数。
This means that if we have this:这意味着如果我们有这个:
def sublist(x):
pass
then x
is going to be a list
— not , as in your example, a number.那么x
将是一个list
——而不是像您的示例中那样是一个数字。 Also, you don't need to do anything with input()
;此外,您不需要对input()
做任何事情; you've already got the list, so don't need that line at all.你已经有了列表,所以根本不需要那一行。
In the function, use a
while
loop to return a sublist of the input list.在函数中,使用while
循环返回输入列表的子列表。
Well, Python has a feature called "generators" that let you do this very easily, I'm going to cheat a bit, and not use a while
loop.好吧,Python 有一个叫做“生成器”的特性,可以让你很容易地做到这一点,我要作弊一点,而不是使用while
循环。 Instead, I'll use a for
loop:相反,我将使用for
循环:
def sublist(x):
for num in x:
if num == 5:
# we need to stop; break out of the for loop
break
# output the next number
yield num
Now this code works:现在这段代码有效:
>>> for num in sublist([3, 4, 2, 5, 6, 7]):
... print(num)
3
4
2
>>>
However, sublist
doesn't technically return a list .但是,从技术上讲, sublist
并不返回list 。 Instead, let's use some MAGIC to make it return a list:相反,让我们使用一些 MAGIC 来让它返回一个列表:
from functools import wraps
return_list = lambda f:wraps(f)(lambda *a,**k:list(f(*a,**k)))
(You don't need to know how this works.) Now, when we define our function, we decorate it with return_list
, which will make the output a list
: (你不需要知道它是如何工作的。)现在,当我们定义我们的函数时,我们用return_list
装饰它,这将使输出成为一个list
:
@return_list
def sublist(x):
for num in x:
if num == 5:
# we need to stop; break out of the for loop
break
# output the next number
yield num
And now this also works:现在这也有效:
>>> print(sublist([3, 4, 2, 5, 6, 7]))
[3, 4, 2]
>>>
Hooray!万岁!
def sublist(x):
accum = 0
sub = []
while accum < len(x):
if x[accum]== 5:
return sub
else:
sub.append(x[accum])
accum = accum +1
return sub
x = [1, 3, 4, 5,6,7,8]
print(sublist(x))
def sublist(lst):
output_list = []
for i in lst:
while i==5:
return output_list
output_list.append(i)
return output_list
I did this like that.我这样做了。
def sublist(x):
count = 0
new = []
if 5 in x:
while(x[count] != 5):
new.append(x[count])
count += 1
return new
else:
return x
def sublist(num):
list = []
for x in num:
if x == 5:
break
while x != 5:
list.append(x)
break
return list
num = [1,2,4,6,7,8,9,5,1,3,4]
print(sublist(num))
num = [1, 2, 3, 17, 1, 3, 5, 4, 3, 7, 5, 6, 9]
new = []
def check_nums(x):
idx = 0
while idx < len(x) and x[idx] != 7:
print(idx)
new.append(x[idx])
idx += 1
print(idx)
return new
print(check_nums(num))
def sublist(a):
i=0;
lst=[];
while(i<len(a) and a[i]!=5):
lst.append(a[i]);
i+=1;
return lst;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.