Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
310 views
in Technique[技术] by (71.8m points)

python 3.x - how to find minimum and maximum values inside collections.defaultdict

good day!

I'm trying to find the minimum and maximum values for a given dataset

foo,1,1
foo,2,5
foo,3,0
bar,1,5
bar,2,0
bar,3,0
foo,1,1
foo,2,2
foo,3,4
bar,1,4
bar,2,0
bar,3,1
foo,1,4
foo,2,2
foo,3,3
bar,1,1
bar,2,3
bar,3,0

I try to sort my data using the 1st and the 2nd columns as ID and the 3rd column as value

from collections import defaultdict

data = defaultdict(list)

with open("file1.txt", 'r') as infile:
    for line in infile:
        line = line.strip().split(',')
        meta = line[0]
        id_ = line[1]
        value = line[2]
        try:
            value = int(line[2])
            data[meta+id_].append(value)
        except ValueError:
            print ('nope', sep='')

the output of my function is:

defaultdict(list,
            {'foo1': ['1', '1', '4'],
             'foo2': ['5', '2', '2'],
             'foo3': ['0', '4', '3'],
             'bar1': ['5', '4', '1'],
             'bar2': ['0', '0', '3'],
             'bar3': ['0', '1', '0']})

please advice how can I get minimum and maximum values for each ID?

so I need output of something like this:

 defaultdict(list,
                {'foo1': ['1', '4'],
                 'foo2': ['2', '5'],
                 'foo3': ['0', '4'],
                 'bar1': ['1', '5'],
                 'bar2': ['0', '3'],
                 'bar3': ['0', '1']})

Update:

with @AndiFB help I add sorting to my lists:

def sorting_func(string):
    return int(string)

from collections import defaultdict

data = defaultdict(list)

with open("file1.txt", 'r') as infile:
    for line in infile:
        line = line.strip().split(',')
        meta = line[0]
        id_ = line[1]
        value = line[2]
        try:
            if value != "-":
                value = int(line[2])
                data[meta+id_].append(value)
                data[meta+id_].sort(key=sorting_func)
                print("max:", *data[meta+id_][-1:], 'min:',*data[meta+id_][:1])
        except ValueError:
            print ('nope', sep='')
                        
data

Output:

max: 1 min: 1
max: 5 min: 5
max: 0 min: 0
max: 5 min: 5
max: 0 min: 0
max: 0 min: 0
max: 1 min: 1
max: 5 min: 2
max: 4 min: 0
max: 5 min: 4
max: 0 min: 0
max: 1 min: 0
max: 4 min: 1
max: 5 min: 2
max: 4 min: 0
max: 5 min: 1
max: 3 min: 0
max: 1 min: 0
defaultdict(list,
            {'foo1': [1, 1, 4],
             'foo2': [2, 2, 5],
             'foo3': [0, 3, 4],
             'bar1': [1, 4, 5],
             'bar2': [0, 0, 3],
             'bar3': [0, 0, 1]})

Please advice how to save only min and max(the first and the last) values in the list?

to get something like this:

defaultdict(list,
                {'foo1': ['1', '4'],
                 'foo2': ['2', '5'],
                 'foo3': ['0', '4'],
                 'bar1': ['1', '5'],
                 'bar2': ['0', '3'],
                 'bar3': ['0', '1']})
question from:https://stackoverflow.com/questions/65557705/how-to-find-minimum-and-maximum-values-inside-collections-defaultdict

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
def sorting_func(string):
    return int(string)


d = defaultdict(list)
d['python'].append('10')
d['python'].append('2')
d['python'].append('5')

print("d['python'].__contains__('10'): {}".format(d['python'].__contains__('10')))
print(str(d['python']))
d['python'].sort(key=sorting_func)
print('d["python"]: ' + str(d['python']))
print('d["python"][0]: ' + d['python'][0])
print('d["python"][2]: ' + d['python'][2])
print(str(len(d['python'])))

Resulting in the following output

d['python'].__contains__('10'): True
['10', '2', '5']
d["python"]: ['2', '5', '10']
d["python"][0]: 2
d["python"][2]: 10
3

You can sort the List leaving in the first position the minimum value, and in the last one the max value

Be aware that if the string contained in the dic can not be casted to Int will result in an exception. The sorting function expects a number to compare. For example another sorting function could be:

def sorting_func(string):
    return len(string)

This one sorts by the length of the string.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...