简体   繁体   English

在列表中打印不是 Python 3 中 3 的倍数的数字

[英]Print numbers in list which are not multiples of 3 in Python 3

Given a list of numbers, you have to print those numbers which are not multiples of 3 in python 3给定一个数字列表,您必须在 python 3 中打印那些不是 3 的倍数的数字

Input Format:输入格式:

The first line contains the list of numbers separated by a space.第一行包含由空格分隔的数字列表。

Output Format:输出格式:

Print the numbers in a single line separated by a space which are not multiples of 3.在一行中打印由空格分隔的不是 3 的倍数的数字。

Example:例子:

Input:输入:

1 2 3 4 5 6 1 2 3 4 5 6

Output:输出:

1 2 4 5 1 2 4 5

Explanation:解释:

Here the elements are 1,2,3,4,5,6 and since 3,6 are multiples of 3, removing them the list leaves 1,2,4,5.这里的元素是 1,2,3,4,5,6 并且因为 3,6 是 3 的倍数,删除它们列表留下 1,2,4,5。

Please don't ask by just copying your nptel assignment questions and ask for a solution, first try to solve and find where the bug is, how the input is given and what is the desired output.请不要仅仅通过复制您的 nptel 分配问题并寻求解决方案来提问,首先尝试解决并找出错误所在,如何给出输入以及所需的输出是什么。 I also got stuck on this assignments before.我之前也被这个任务困住了。 Anyways here's the solution that should work fine, if it gets the job done, don't forget to accept the solution by clicking the tick on the left of this post.无论如何,这里的解决方案应该可以正常工作,如果它完成了工作,请不要忘记通过单击本文左侧的勾号来接受该解决方案。

x = input()
num = list(map(int, x.split()))
l =[]
for i in num:  
    if i%3 != 0:
        l.append(i)
print(*l, sep = " ")

There are any number of one-liner solutions.有多种单线解决方案。 How about:怎么样:

print(*(item for item in map(int, input().split()) if item % 3))

Use the modulo operator.使用模运算符。 (%) This operator will yield the remainder from the division of the first argument with the second. (%) 此运算符将产生第一个参数与第二个参数相除的余数。 So when you are wanting numbers that are not multiples of 3, another way to say that is you are looking for numbers whose remainder is not 0 when divided by 3.因此,当您想要不是 3 倍数的数字时,另一种说法是您正在寻找除以 3 时余数不是 0 的数字。

x % 3 != 0.

The Logic is really simple, You need to remove all the number from the list that is divisible by 3.逻辑非常简单,您需要从列表中删除所有可被 3 整除的数字。

Since list is used list.remove(item) method can be used.由于使用了列表,因此可以使用 list.remove(item) 方法。

and the output should be space separated so the list of the item can be joined together using space in between them,并且输出应该以空格分隔,以便可以使用它们之间的空格将项目列表连接在一起,

Following code does the trick以下代码可以解决问题

numbers = input()
list = numbers.split()

for item in list:
    if int(item)%3==0:
        list.remove(item)

print(' '.join(list)) 

Input: 1 2 3 4 5 6输入: 1 2 3 4 5 6

Output: 1 2 4 5输出: 1 2 4 5

list(filter(lambda x: x%3 != 0 , yourlist))

@Souvikavi's answer looks good. @Souvikavi 的回答看起来不错。 But the for loop can be simplified as:但是 for 循环可以简化为:

numbers = input()
list = numbers.split()

newList = [item for item in list if int(item)%3!=0]
print(' '.join(newList))

or:或者:

numbers = input()

newList = [item for item in numbers.split() if int(item)%3!=0]
print(' '.join(newList))

or even:甚至:

numbers = input()

print(' '.join([item for item in numbers.split() if int(item)%3!=0]))
a=list(map(int,input().split()))
b=list()
for i in range(len(a)):
    if a[i]%3!=0:
        b.append(a[i])      
print(*b,sep=" ")

In the above code using two list namely a and b.The numbers which are not multiples of 3 from list a are appended to the list b and list b is printed在上面的代码中使用了两个列表,即 a 和 b。列表 a 中不是 3 的倍数的数字被附加到列表 b 并打印列表 b

n=(input("How many number you want to insert in a list"))
list=[ ]
for i in range(0,n):
    a=int(input("enter elements"))
    list.append(a)
    for i in(list):
        if(i%3!=0):
            print(i,"are not multiply of 3")
        else:
            print("multiply of 3")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM