[英]Python - looping multiple lists with enumerate for same index
list1 = Csvfile1._getRow(' field1')
list2 = Csvfile2._getRow(' field1')
_list1 = Csvfile1._getRow(' field2')
_list2 = Csvfile2._getRow(' field2')
for i,(a,b) in enumerate(zip(list2, list1)):
value = False
if field == ' field1':
for j,(c,d) in enumerate(zip(_list2, _list1)):
if i == j:
if a != b and c != d:
value = True
else:
value = False
break
if value == True:
continue
if a != b
# do something
Below is the sample : values in both the csv files are compared. 下面是示例:比较两个csv文件中的值。 when the value for field1 is not equal in both csv files, the condition if a != b: should be executed. 当两个csv文件中的field1值都不相等时,应执行a!= b:的条件。
When the value for field1 is not equal in both csv files, and at the same time if the values for field2 is also not equal -> then the condition if a != b: should not be executed. 如果两个csv文件中的field1值都不相等,并且同时如果field2的值也不相等->,则不应该执行a!= b:的条件。
With huge data this seems to be not working. 拥有大量数据,这似乎行不通。 Or is there a better way to achieve this ? 还是有更好的方法来实现这一目标?
Csvfile1 Csvfile1
field1 | field1 | field2 域2
222 | 222 | 4 -> enter if a != b: condition loop 4->如果a!= b,则输入:条件循环
435 | 435 | 5 -> do not enter if a != b: condition loop 5->如果a = b,则不输入:条件循环
Csvfile2 Csvfile2
field1 | field1 | field2 域2
223 | 223 | 4 4
436 | 436 | 6 6
If I got right what you want to do, try something like this: 如果我做对了您想做的事情,请尝试以下操作:
$ cat t1.txt
field1|field2
222|4
435|5
$ cat t2.txt
field1|field2
223|4
436|6
$ python
import csv
with open("t1.txt", "rb") as csvfile:
with open("t2.txt", "rb") as csvfile2:
reader = csv.reader(csvfile, delimiter='|')
reader2 = csv.reader(csvfile2, delimiter='|')
for row1, row2 in zip(reader, reader2):
for elem1, elem2 in zip(row1, row2):
if elem1 != elem2:
print "different: {} and {}".format(elem1, elem2)
different: 222 and 223
different: 435 and 436
different: 5 and 6
#first field(ff) second field(sf) first file(ff) second file(sf)
field1csv1 = Csvfile1._getRow(' field1')
field1csv2 = Csvfile2._getRow(' field1')
field2csv1 = Csvfile1._getRow(' field2')
field2csv2 = Csvfile2._getRow(' field2')
Every time you have huge lists of data you should think about using a generator instead of a list comprehension. 每当您拥有大量数据列表时,都应该考虑使用生成器而不是列表推导。 itertools.izip
is a generator version of zip
. itertools.izip
是zip
的生成器版本。
Plugging it in should give you a considerable improvement, as no temporary lists will be generated: 插入它应该会给您带来很大的改进,因为不会生成任何临时列表:
from itertools import izip
for i, (a, b) in enumerate(izip(list2, list1)):
value = False
if field == ' field1':
for j, (c, d) in enumerate(izip(_list2, _list1)):
if i == j:
if a != b and c != d:
value = True
else:
value = False
break
if value == True:
continue
if a != b
# do something
This is an example of how to refactor your code to get rid of the iteration in python and drop the iteration to the C level: 这是如何重构代码以摆脱python中的迭代并将迭代降至C级别的示例:
#orig
for i, (a, b) in enumerate(zip(list2, list1)):
value = False
if field == ' field1':
for j, (c, d) in enumerate(zip(_list2, _list1)):
if i == j:
if a != b and c != d:
value = True
else:
value = False
break
With generators: 使用发电机:
from itertools import izip
mygen = izip(izip(list2,list1),izip(_list2,_list1))
#[((a, b), (c, d)), ((x, y), (_x, _y)), ...]
values = [tuple1[0]!=tuple1[1] and tuple1[2]!=tuple2[1] for tuple1, tuple2 in mygen]
Also you could use "equality" generators: 您也可以使用“平等”生成器:
field1 = izip(field1csv1, field1csv2)
field2 = izip(field2csv1, field2csv2)
field1equal = (f[0] == f[1] for f in field1)
field2equal = (f[0] == f[1] for f in field2)
I got this far and then gave up. 我走了这么远,然后放弃了。 I have no idea what you're doing. 我不知道你在做什么
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.