Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何消除python列表理解中的代码重复?_Python_List Comprehension_Code Duplication - Fatal编程技术网

如何消除python列表理解中的代码重复?

如何消除python列表理解中的代码重复?,python,list-comprehension,code-duplication,Python,List Comprehension,Code Duplication,如何通过合并成一个列表来解决以下代码重复问题 myList =[ //someList ] thierList=[ //someOtherList ] if name: [x for x in range(2,100) if x%2 and x in mylist and x not in theirList] else: [x for x in range(2,100) if x in mylist an

如何通过合并成一个列表来解决以下代码重复问题

 myList   =[ //someList      ]      
 thierList=[ //someOtherList ]      

 if name:
          [x for x in range(2,100) if x%2 and x in mylist and x not in theirList]

 else:
          [x for x in range(2,100) if x in mylist and x not in theirList] 
也可以使用集合

[x for x in range(2,100) if x in mylist and x not in theirList if not name or x%2 ]
set(myList + thierList)