hujiezhong 发表于 2020-9-19 22:44:42

大佬们,这个结果为什么是这样的啊

      list1 = [(x, y) for x in range(10) for y in range(10) if x%2==0 if y%2!=0]
>>> lsit1

大佬们,我的思考是这样的,因为x%2 == 0是偶数, y%2 != 0是奇数.
所以x为0到9之间的所有偶数,y为0到9之间的所有奇数,但是结果是是真没想到.
各位大佬能解决这个吗,求解。
拜托了!

疾风怪盗 发表于 2020-9-19 22:53:18

list1 = [(x, y) for x in range(10) for y in range(10) if x % 2 == 0 if y % 2 != 0]

list2 = []
for x in range(10):
    for y in range(10):
      if x % 2 == 0 and y % 2 != 0:
            list2.append((x, y))
print(list1)
print(list2)


拆来开,结果是这样的:
[(0, 1), (0, 3), (0, 5), (0, 7), (0, 9), (2, 1), (2, 3), (2, 5), (2, 7), (2, 9), (4, 1), (4, 3), (4, 5), (4, 7), (4, 9), (6, 1), (6, 3), (6, 5), (6, 7), (6, 9), (8, 1), (8, 3), (8, 5), (8, 7), (8, 9)]
[(0, 1), (0, 3), (0, 5), (0, 7), (0, 9), (2, 1), (2, 3), (2, 5), (2, 7), (2, 9), (4, 1), (4, 3), (4, 5), (4, 7), (4, 9), (6, 1), (6, 3), (6, 5), (6, 7), (6, 9), (8, 1), (8, 3), (8, 5), (8, 7), (8, 9)]

hujiezhong 发表于 2020-9-20 21:12:11

疾风怪盗 发表于 2020-9-19 22:53
拆来开,结果是这样的:
[(0, 1), (0, 3), (0, 5), (0, 7), (0, 9), (2, 1), (2, 3), (2, 5), (2, 7), ...

谢了,老兄,昨晚我都是揪着心睡的{:10_243:}

疾风怪盗 发表于 2020-9-20 21:13:58

hujiezhong 发表于 2020-9-20 21:12
谢了,老兄,昨晚我都是揪着心睡的

{:10_256:}有啥可揪心的,就是两个数字不断重复而已,010101010101010101010.....
页: [1]
查看完整版本: 大佬们,这个结果为什么是这样的啊