python-3.x - 如何在 collections.defaultdict 中找到最小值和最大值
问题描述
再会!
我正在尝试找到给定数据集的最小值和最大值
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
我尝试使用第 1 列和第 2 列作为 ID 和第 3 列作为值对数据进行排序
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='')
我的函数的输出是:
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']})
请建议我如何获得每个 ID 的最小值和最大值?
所以我需要这样的输出:
defaultdict(list,
{'foo1': ['1', '4'],
'foo2': ['2', '5'],
'foo3': ['0', '4'],
'bar1': ['1', '5'],
'bar2': ['0', '3'],
'bar3': ['0', '1']})
更新:
在@AndiFB 的帮助下,我将排序添加到我的列表中:
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
输出:
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]})
请建议如何在列表中仅保存最小值和最大值(第一个和最后一个)值?
得到这样的东西:
defaultdict(list,
{'foo1': ['1', '4'],
'foo2': ['2', '5'],
'foo3': ['0', '4'],
'bar1': ['1', '5'],
'bar2': ['0', '3'],
'bar3': ['0', '1']})
解决方案
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'])))
导致以下输出
d['python'].__contains__('10'): True
['10', '2', '5']
d["python"]: ['2', '5', '10']
d["python"][0]: 2
d["python"][2]: 10
3
您可以对列表进行排序,在第一个位置留下最小值,在最后一个位置留下最大值
请注意,如果 dic 中包含的字符串不能强制转换为 Int 将导致异常。排序函数需要一个数字来比较。例如,另一个排序函数可能是:
def sorting_func(string):
return len(string)
这个按字符串的长度排序。
推荐阅读
- date - 为什么提交表单后我的日期公式消失了?
- python - Python 请求使用 ISO 日期时间不起作用的参数
- android - 无法在 Android 物理设备上创建 SQLite 表
- makefile - 如何为同一目标文件的不同依赖项集运行不同的配方
- c++ - NEON:将 int8x16_t 拆包成一对 int16x8 并将一对 int16x8_t 打包成 int8x16_t
- apexcharts - Apex 图表线条颜色设置不正确
- java - 由于侵犯用户隐私,Android 应用已从 Play 商店下架
- windows - 如何为 SSH 使用新的 Windows 终端应用程序?
- spring-boot - 多个消费者组
- airflow - 有什么方法可以计算气流中的日期吗?