i=input - storing the function in i to be called later
zip(i(),i()) - if the user entered 1010 and 1101, this would return [(1, 1), (0, 1), (1, 0), (0, 1)] - the elements of both strings paired up
for a,b in - a and b are the pair from the zip (in the example they are first set to a=1 and b=1, then a=0 and b=1, etc.)
'01'[a!=b] - a!=b will return a boolean - either True or False. True = 1 and False = 0 so you can use this for indexing. Basically this says if a!=b: '1', if a==b: '0'
''.join - finally, join all the '0's and '1's together to make one string and print
发布评论
评论(2)
逐步说明:
i = input
- 将函数存储在i
中,以后称为zip(i(),i(),i())
-如果用户输入1010
和1101
,这将返回[(1,1),(0,1),(1,1,0),(0,(0,(0,) 的两个字符串的元素是来自
1)] - in a a 和
b
zip
(在示例中,它们首先设置为a = 1和b = 1,然后a = 0和b = 1等)'01'[a!= b] < /code> -
a!= b
将返回布尔值 - 是的。true
= 1和false
= 0,因此您可以将其用于索引。基本上,这说明如果A!= B:'1'
,如果a == b:'0'
'''。join
- 最后,最后,将所有“ 0和1”一起加入一个字符串并打印Explained step by step:
i=input
- storing the function ini
to be called laterzip(i(),i())
- if the user entered1010
and1101
, this would return[(1, 1), (0, 1), (1, 0), (0, 1)]
- the elements of both strings paired upfor a,b in
-a
andb
are the pair from thezip
(in the example they are first set to a=1 and b=1, then a=0 and b=1, etc.)'01'[a!=b]
-a!=b
will return a boolean - either True or False.True
= 1 andFalse
= 0 so you can use this for indexing. Basically this saysif a!=b: '1'
,if a==b: '0'
''.join
- finally, join all the '0's and '1's together to make one string and print如果您真的想将其制作为Oneliner,请删除分号并这样做:
zip
用元组创建一个值,所以“ HI”和“ HO”将成为[(h,h),(i,o)][a!= b]
确实,它评估true
(1)或false
(0),''。加入
串联列表中的所有元素,然后将其打印If you really want to make it a oneliner, remove the semicolon and do it like this:
zip
creates an iterable with tuples, so "hi" and "ho" will become [(h,h), (i,o)][a!=b]
does, it evaluates to eitherTrue
(1) orFalse
(0)''.join
concatenates all elements in your list, and this gets printed