pythonnext()函数
我正在写一个程序来寻找数字中唯一的数字,并得到了这个解决方案,但我想知道它是如何工作的pythonnext()函数,python,Python,我正在写一个程序来寻找数字中唯一的数字,并得到了这个解决方案,但我想知道它是如何工作的 print(next(len(set(list(x))) if 1<=int(x)<=25000 else "Number should be >=1 and <=25000" for x in [input()])) print(next(len(set(list(x)))如果1让我们用另一种方式重写解决方案并对其进行注释: # Read the number
print(next(len(set(list(x))) if 1<=int(x)<=25000 else "Number should be >=1 and <=25000" for x in [input()]))
print(next(len(set(list(x)))如果1让我们用另一种方式重写解决方案并对其进行注释:
# Read the number (as a string, for example '3455')
x = input()
# Convert the string to an int ('3455' --> 3455) and check if the condition holds
if 1 <= int(x) <= 25000:
# Create a list of digits ('3455' --> ['3', '4', '5', '5'])
lst = list(x)
# Find unique digits (['3', '4', '5', '5'] --> {'3', '4', '5'})
s = set(lst)
# Count the number of unique digits
c = len(s)
# Print the result
print(c)
else:
# If the condition 1 <= x <= 25000 does not hold then print an error message
print("Number should be >=1 and <=25000")
#读取数字(作为字符串,例如“3455”)
x=输入()
#将字符串转换为整数('3455'-->3455),并检查该条件是否成立
如果1{3',4',5'})
s=设定值(lst)
#计算唯一数字的数量
c=长(s)
#打印结果
印刷品(c)
其他:
#如果条件1如果您想要“查找数字中的唯一数字”意味着您想要查看哪些数字是唯一的,那么您可以使用以下代码:
x = input()
print((list(set(x))) if 1<=int(x)<=25000 else "Number should be >=1 and <=25000")
x=input()
打印((列表(设置(x)),如果1它正在做很多事情。您不确定哪一部分?此代码无法“在数字中找到唯一的数字”,问题是next()
call@alec_djinn此代码实际上查找数字中的唯一位数。next
函数也在工作。这有什么问题吗?不要关闭问题,它完全正确clear@alec_djinn:程序正在滥用生成器表达式作为绑定输入()的方法
将值返回到一个变量。生成器表达式生成一个值,然后下一个提取该值。(这实际上应该跨多个语句写入,而不是将所有内容强制写入一行。)@user2357112supportsMonica完全正确!这与打印([len(set(list(x))基本相同)如果1投反对票的人:请告诉我我做错了什么,以及我如何改进这个解决方案。在CASE中,如果你不喜欢,我也可以删除它。请激发你的反对票