Python 如何从键值对列表中搜索其是否在列表中的数据
我有这样一份清单:Python 如何从键值对列表中搜索其是否在列表中的数据,python,arrays,Python,Arrays,我有这样一份清单: a = [('X', '63.658'), ('Y', '21.066'), ('Z', '230.989'), ('X', '63.424'), ('Y', '21.419'), ('Z', '231.06'), ('X', '63.219'), ('Y', '21.805'), ('Z', '231.132'), ('X', '63.051'), ('Y', '22.206'), ('Z', '231.202'), ('X'
a = [('X', '63.658'), ('Y', '21.066'), ('Z', '230.989'),
('X', '63.424'), ('Y', '21.419'), ('Z', '231.06'),
('X', '63.219'), ('Y', '21.805'), ('Z', '231.132'),
('X', '63.051'), ('Y', '22.206'), ('Z', '231.202'),
('X', '62.915'), ('Y', '22.63'), ('Z', '231.272'),
('X', '62.811'), ('Y', '23.073'), ('Z', '231.341')]
从这个列表中,我为X
、Y
和Z
的最大值和最小值编写了代码
但是,我还想打印如果我删除了X对,那么X将不在列表中,它应该打印“X为空”。同样,如果A-Z中有A、B、G、S、X等不在列表中的对,则应打印为“A为空..B为空..G为空..S为空..X为空..”还应打印列表中其余对的最小值和最大值..
这是我用python编写的代码:
with open(r'/path of file...txt') as f:
lines=f.readlines()
lines=''.join(lines)
lines=lines.split()
a=[]
for i in lines:
match=re.match(r"([a-z]+)([-+]?[0-9]*\.?[0-9]+)",i,re.I)
if match:
a.append(match.groups())
print a
for tuples in a:
key, value = tuples[0], float(tuples[1])
groups[key].append(value)
for key, values in groups.iteritems():
print key, max(values), min(values)
我会这样做:
from string import ascii_uppercase
elems = set(c for c, n in a)
for c in ascii_uppercase: # Or whatever string of characters you want to check.
if c in elems:
relevant = [t for t in a if t[0] == c]
print((c, max(relevant)[1], min(relevant)[1]))
else:
print((c, None))
这应该很有效。上面的代码并没有格式化为具有漂亮的输出,但这只是您稍微使用print
函数的问题。如果您使用a来保存所读取的值,可能会更容易
from collections import defaultdict
items = defaultdict(list)
groups = match.groups()
items[groups[0]].append(float(groups[1]))
然后,每个元素在字典中只有一个键,只需使用
del items['X']
你可以用
if 'X' not in items:
print('X is empty')
else:
values = items['X']
avg = sum(values) / len(values)
min = min(values)
max = max(values)
您可以使用带过滤器的集合:
k=set([i[0] for i in a])
for ki in k:
l=filter(lambda x:x[0]==ki, a)
print "max:", max(l), "min: ", min(l)
输出:
max: ('Y', '23.073') min: ('Y', '21.066')
max: ('X', '63.658') min: ('X', '62.811')
max: ('Z', '231.341') min: ('Z', '230.989')
我会使用字典保存每个字符的值:
a = [('X', '63.658'), ('Y', '21.066'), ...]
processed = {}
for char, value in data:
if char not in processed:
processed[char] = []
processed[char].append(value)
然后遍历所有ASCII大写字符,打印计算值或例如“N为空…”
您可以通过以下方式稍微简化:
一般来说,我建议将您的程序拆分一点——将元组数据从文本文件导入到一个函数中,并将列表处理到另一个函数中。这使得单独开发和测试每个代码变得更容易。您当前的代码有什么问题?错误(提供完整的回溯)?意外输出(提供输入以及预期和实际输出)?我当前的代码没有问题…工作正常..但我不知道如何以及在何处放置我在问题中提到的条件。。!!你问题的粗体部分对我来说毫无意义:去掉哪一对?什么是
A
,B
,G
,等等。;其他元组列表?('X','63.658'),('X','63.424'),('X','62.811')这些对如果我全部删除,那么它应该打印Y和Z的最小值和最大值,以及打印“X为空”…它们是基于什么“对”?何时以及为什么要删除它们?代码是否应该报告列表中未包含的每个大写字母的缺失值?如果我有多个字符,如A、B、C、D、X、Y、Z或say for all chars..?然后将if
包装在for
循环导入字符串中;对于字符串形式的ascii字母:
import string
for char in string.ascii_uppercase:
if char not in processed:
print("{0} is empty...".format(char))
else:
print("{0}: min={1}, max={2}".format(char,
min(processed[char]),
max(processed[char])))
from collections import defaultdict
processed = defaultdict(list)
for char, value in data:
processed[char].append(value)