int*_*lar 0 python comparison dictionary
我有一个字典,看起来像这样:
{'NM_100': [(0,20), (30,40), (70,90)], 'NM_200': [(0,35), (75,85), (90,100), (200,300)]}
Run Code Online (Sandbox Code Playgroud)
以及包含此信息的制表符分隔文件:
isoform strand pos_rein1 pos_rein2
NM_100 - 32 35
NM_100 - 16 16
NM_200 - 76 77
NM_200 - 89 90
Run Code Online (Sandbox Code Playgroud)
我想要做的是测试我的文件中的两个位置是否都在字典中的配对数字范围内.例如,32和35是否位于相同的配对数范围内?(在这种情况下,他们做(30,40))如果他们这样做,继续.如果他们不这样做(作为我文件中的最后一个案例),请不要继续.这是我到目前为止:
import csv
with open('indel_mod0_cdsStart_rein_both.txt') as f:
reader = csv.DictReader(f,delimiter="\t")
for row in reader:
pos = row['pos_rein1']
pos2 = row['pos_rein2']
name = row['isoform']
strand = row['strand']
ppos1 = int(pos)
ppos2 = int(pos2)
if name in exons:
y = exons[name]
for i, (low,high) in enumerate(exons[name]):
if low <= ppos1 <= high: #Is there any way to edit this line to test if ppos2 is also in that range
exonnumber = i+1
Run Code Online (Sandbox Code Playgroud)
我目前只是在测试第一个位置是否属于数字范围,是否有一种简单的方法可以解决这个问题以解决这两个数字?
只需使用and添加第二个测试:
if low <= ppos1 <= high and low <= ppos2 <= high:
Run Code Online (Sandbox Code Playgroud)
如果你有更多的位置要测试,你可以切换到使用all(),并将这些位置存储在一个序列中(这里称为positions):
if all(low <= pos <= high for pos in positions):
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
64 次 |
| 最近记录: |