简体   繁体   English

比较python中的两个列表项

[英]Comparing two lists items in python

I have two files which I loaded into lists. 我有两个文件加载到列表中。 The content of the first file is something like this: 第一个文件的内容如下所示:

d.complex.1
23
34
56
58
68
76
.
.
.
etc
d.complex.179
43
34
59
69
76
.
.
.
etc

The content of the second file is also the same but with different numerical values. 第二个文件的内容也相同,但数值不同。 Please consider from one d.complex.* to another d.complex.* as one set. 请考虑从一个d.complex。*到另一个d.complex。*作为一组。

Now I am interested in comparing each numerical value from one set of first file with each numerical value of the sets in the second file. 现在,我有兴趣将一组第一个文件中的每个数值与第二个文件中的每个数值进行比较。 I would like to record the number of times each numerical has appeared in the second file overall. 我想记录每个数字整体出现在第二个文件中的次数。

For example, the number 23 from d.complex.1 could have appeared 5 times in file 2 under different sets. 例如,d.complex.1中的数字23可能在文件2中的不同集合下出现了5次。 All I want to do is record the number of occurrences of number 23 in file 2 including all sets of file 2. 我要做的就是在文件2中记录数字23的出现次数,包括文件2的所有集合。

My initial approach was to load them into a list and compare but I am not able to achieve this. 我最初的方法是将它们加载到列表中并进行比较,但是我无法实现这一点。 I searched in google and came across sets but being a python noob, I need some guidance. 我在Google中搜索并遇到了集合,但作为python noob,我需要一些指导。 Can anyone help me? 谁能帮我?

If you feel the question is not clear,please let me know. 如果您觉得问题不清楚,请告诉我。 I have also pasted the complete file 1 and file 2 here: 我还在这里粘贴了完整的文件1和文件2:

http://pastebin.com/mwAWEcTa http://pastebin.com/DuXDDRYT http://pastebin.com/mwAWEcTa http://pastebin.com/DuXDDRYT

Open the file using Python's open function, then iterate over all its lines. 使用Python的open函数打开文件,然后遍历其所有行。 Check whether the line contains a number, if so, increase its count in a defaultdict instance as described here . 检查线路是否包含一个数字,如果是的话,增加在其计数defaultdict所描述的情况在这里

Repeat this for the other file and compare the resulting dicts. 对其他文件重复此操作,然后比较结果字典。

First create a function which can load a given file, as you may want to maintain individual sets and also want to count occurrence of each number, best would be to have a dict for whole file where keys are set names eg complex.1 etc, for each such set keep another dict for numbers in set, below code explains it better 首先创建一个可以加载给定文件的函数,因为您可能想要维护单个集合,并且还希望计算每个数字的出现,最好是对整个文件有一个字典,其中键是集合名,例如complex.1等,对于每个这样的集合,请在集合中保留另一个数字字典,下面的代码可以更好地说明

def file_loader(f):
    file_dict = {}
    current_set = None
    for line in f:
        if line.startswith('d.complex'):
            file_dict[line] = current_set = {}
            continue

        if current_set is not None:
            current_set[line] = current_set.get(line, 0)

    return file_dict

Now you can easily write a function which will count a number in given file_dict 现在,您可以轻松编写一个函数,该函数将在给定的file_dict中计算一个数字

def count_number(file_dict, num):
    count = 0
    for set_name, number_set in file_dict.iteritems():
        count += number_set.get(num, 0)

    return count

eg here is a usage example 例如这是一个用法示例

s = """d.complex.1
10
11
12
10
11
12"""

file_dict = file_loader(s.split("\n"))
print file_dict
print count_number(file_dict, '10')

output is: 输出为:

{'d.complex.1': {'11': 2, '10': 2, '12': 2}}
2

You may have to improve file loader, eg skip empty lines, convert to int etc 您可能需要改进文件加载器,例如,跳过空行,转换为int等

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM