我正在尝试使用Python根据TableB的第一列中的值在两个列(TABLEA)中找到值范围。列表中的第1列和第2列代表值的范围,每当TableB中的第1列的值范围内,我想从tablea中提取此类行,如输出中所示,也知道它们是多少。
。tablea:
1 524
677 822
902 1103
1239 1790
2001 2321
3900 4567
tableb:
351 aux
1256 sle
4002 aim
所需的输出:
1 524
1239 1790
3900 4567
Total count = 3
这是我的尝试不起作用:
datA = open('TableA.txt','r')
datB = open('TableB.txt','r')
count=0
for line1 in datB:
line1 = line1.strip().split()
for line2 in datA:
line2 = line2.strip().split('t')
for col1, col2 in zip(line2[0], line2[1]):
if line2 > col1 and line2 < col2:
print(col1 + 't' + col2)
count=+1
print(count)
datA.close()
datb.close()
有人可以帮忙吗?谢谢
您可以这样尝试:
tableBcol1=[int(i.split()[0]) for i in open('TableB.txt')]
tableA=[i.strip() for i in open('TableA.txt')]
count=0
for bcol1 in tableBcol1:
for line in tableA:
lbound,hbound=line.split()
if bcol1 in range(int(lbound),int(hbound)+1):
print(line.strip())
count+=1
print(count)
tableBcol1
包含整数形式的TableB.txt
的列1的所有值(ie 351,1256,4002)。
lbound
和 hbound
包含COLAN11和TableA.txt
列的值。
最后,您在if
语句中检查会员资格。如果TableB.txt
的列1中的值在该范围内,则从TableA.txt
打印行。请注意,由于上界不包含在内,因此将一个添加到hbound
中。
TableA = """1 524
677 822
902 1103
1239 1790
2001 2321
3900 4567"""
TableB = """351 aux
1256 sle
4002 aim"""
#TableA = open('TableA.txt', 'r').read()
#TableB = open('TableB.txt', 'r').read()
ranksA = [map(int, e.split()) for e in TableA.split("n")]
valuesB = [int(e.split()[0]) for e in TableB.split("n")]
resultsraw = [(v, [(ri, rf) for ri, rf in ranksA if v >= ri and v <= rf]) for v in valuesB]
results = "n".join(["%6st%6s" % e[1][0] for e in resultsraw])
print results
print "Total count: %s" % (len(resultsraw))
输出:
1 524
1239 1790
3900 4567
Total count: 3
报告和表B中每个项目的总范围落在表A。
中的范围内这是使用defaultdict
作为计数器的另一种方法,用于读取表格数据的csv
和用于安全打开/关闭文件的with
语句:
import csv
from collections import defaultdict
# Build reference dict
with open("Table A.txt", "r") as f:
reader = csv.reader(f)
# reference = defaultdict(list)
reference = defaultdict(int)
for row in reader:
reference[row[0]]
# Read data and tally
with open("Table B.txt", "r") as f:
reader2 = csv.reader(f)
# header = next(reader2)
for row in reader2:
col1 = int(row[0].split()[0])
for key in reference:
first, last = map(int, key.split())
if col1 >= first and col1 <= last:
# reference[key].append(row)
reference[key] += 1
reference
结果是一个词典,该字典记录了适合给定范围内的条目。
defaultdict(int,
{'1 524': 1,
'1239 1790': 1,
'2001 2321': 0,
'3900 4567': 1,
'677 822': 0,
'902 1103': 0})
defaultdict
使您可以选项存储整数或将值附加到列表中(请参阅注释行)。但是,对于您所需的输出:
for k, v in reference.items():
if v:
print(k)
print("Total:", total)
最终输出:
1 524
1239 1790
3900 4567
Total: 3