简体   繁体   English

这两个 python 代码有什么区别,为什么输出不一样?

[英]what is the difference between these 2 python codes, and why the out put is not same?

these are the 2 python3 code,这些是2个python3代码,

{w for w in words if w[::-1] in words and len(w)==4}
{w for w in words if w==w[::-1]and len(w)==4}

In my point, w is the word in words, so w should equal with words ,but the output is differently.在我的观点中,w 是单词中的单词,所以w应该等于words ,但是 output 是不同的。 Could someone help me, why the output is different?有人可以帮助我,为什么 output 不同?

if words is {'abba', 'abcd', 'dcba'} then the result of the first code will be {'abba', 'abcd', 'dcba'} while the second will return {'abba'} .如果words{'abba', 'abcd', 'dcba'}那么第一个代码的结果将是{'abba', 'abcd', 'dcba'}而第二个将返回{'abba'}

w==w[::-1] checks if the word is equal to its reverse while w[::-1] in words checks if the reverse of w is in words . w==w[::-1]检查单词是否等于它的反向,而w[::-1] in words检查w的反向是否在words中。

I guess those code sanples are doing really different things.我猜那些代码 sanples 正在做不同的事情。

{w for w in words if w[::-1] in `words` and len(w)==4}

It includes element w from itterable words if only reversed w ("ABCD" => "DCBA") is also present at words and it's length is 4.它包括来自可迭代words的元素w ,如果只有反转的w ("ABCD" => "DCBA") 也出现在words中并且它的长度是 4。

{w for w in words if w==w[::-1]and len(w)==4}

This includes element w from words if only w is equal to reversed w itself ("abba" = "abba") and it's length is 4这包括words中的元素w如果只有w等于反转w本身(“abba”=“abba”)并且它的长度是 4

暂无
暂无

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

相关问题 这两个python代码有什么区别? 为什么结果不同? - what's the difference between these 2 python codes? why different results? 这些代码之间有什么区别? 为什么它们有不同的输出? - What's the difference between these codes? Why do they have different outputs? 这两个Python子集递归函数代码有什么区别? - What is the difference between these two codes of Python subset recursion function? 为什么Python和Cython中这两个代码之间存在巨大的性能差异? - Why there is a huge performance difference between these two codes in Python and Cython? 这两个代码有什么区别? - What's the difference between the two of the codes? 这两个代码有什么区别? - What's the difference between these two codes? python:if-in运算符。 这两个代码之间有区别吗? - python: if - in operator. Is there a difference between these two codes? Python3.5和Python3.6的源代码在这里有什么区别? - What's the difference here between the source codes of Python3.5 and Python3.6? Python.Pandas /://第一和第二代码之间有什么区别,因此以下代码发生了吗? - Python.Pandas/:/What is the difference between the first and second codes, so did the following code occur? 两个 Python 代码之间的详细区别是什么,它们都是为了在三个输入中找到最大数而编写的 - What is the detailed difference between two Python codes, both written to find the largest number among three inputs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM