Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
删除python中带有坐标的字符串的一部分_Python_String_List_Tuples - Fatal编程技术网

删除python中带有坐标的字符串的一部分

删除python中带有坐标的字符串的一部分,python,string,list,tuples,Python,String,List,Tuples,您好,我有一个元组列表,例如: indexes_to_delete=((6,9),(20,22),(2,4)) 我可以用Biopython打开一个序列: Sequence1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 从索引到\u删除文件,我想从以下文件中删除该部分: 6 to 9 20 to 22 及 所以如果我遵循这些坐标,我应该有一个新的序列: A B C D E F G H I J K L M N O P Q R S T U V W X

您好,我有一个元组列表,例如:

indexes_to_delete=((6,9),(20,22),(2,4))
我可以用Biopython打开一个序列:

Sequence1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
索引到\u删除
文件,我想从以下文件中删除该部分:

6 to 9
20 to 22

所以如果我遵循这些坐标,我应该有一个新的序列:

A B C D E F G H I J  K  L  M  N  O  P  Q  R  S  T  U  V  W  X  Y  Z
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 
如果我去掉坐标,我得到:

A E J  K  L  M  N  O  P  Q  R  S  W  X  Y  Z
1 5 10 11 12 13 14 15 16 17 18 19 23 24 25 26 

下面是使用几个模块的另一种方法

from string import ascii_uppercase
from intspan import intspan
from operator import itemgetter

indexes_to_delete=((6,9),(20,22),(2,4))

# add dummy 'a' so count begins with 1 for uppercase letters
array = ['a'] + list(ascii_uppercase) 

indexes_to_keep = intspan.from_ranges(indexes_to_delete).complement(low = 1, high=26)

slice_of = itemgetter(*indexes_to_keep)

print(' '.join(slice_of(array)))
print(' '.join(map(str,indexes_to_keep)))
印刷品:

AEJKLMNOPQRSWXYZ
A E J K L M N O P Q R S W X Y Z
1 5 10 11 12 13 14 15 16 17 18 19 23 24 25 26

下面是使用几个模块的另一种方法

from string import ascii_uppercase
from intspan import intspan
from operator import itemgetter

indexes_to_delete=((6,9),(20,22),(2,4))

# add dummy 'a' so count begins with 1 for uppercase letters
array = ['a'] + list(ascii_uppercase) 

indexes_to_keep = intspan.from_ranges(indexes_to_delete).complement(low = 1, high=26)

slice_of = itemgetter(*indexes_to_keep)

print(' '.join(slice_of(array)))
print(' '.join(map(str,indexes_to_keep)))
印刷品:

AEJKLMNOPQRSWXYZ
A E J K L M N O P Q R S W X Y Z
1 5 10 11 12 13 14 15 16 17 18 19 23 24 25 26
您可以使用此函数获取新序列

new_sequence = delete_indexes(Sequence1, indexes_to_delete)
当然,新的_序列仍然是python字典。您可以将其转换为
list
str
,或其他任何形式。例如,要将其转换为
str
作为旧的
Sequence1

print(''.join(list(new_sequence.values())))
Out[7]:
AEJKLMNOPQRSWXYZ
您可以使用
new\u sequence.keys()
获取它们的坐标

您可以使用此函数获取新序列

new_sequence = delete_indexes(Sequence1, indexes_to_delete)
当然,新的_序列仍然是python字典。您可以将其转换为
list
str
,或其他任何形式。例如,要将其转换为
str
作为旧的
Sequence1

print(''.join(list(new_sequence.values())))
Out[7]:
AEJKLMNOPQRSWXYZ

您可以使用
new_sequence.keys()

更可读的版本获取它们的坐标:

index_to_delete=((6,9)、(20,22)、(2,4))
Sequence1=“abcdefghijklmnopqrstuvxyz”
newSequence1=“”
对于idx,枚举中的字符(Sequence1):
对于startIndex,索引中的endIndex要删除:

如果startIndex的可读性更强一些:

index_to_delete=((6,9)、(20,22)、(2,4))
Sequence1=“abcdefghijklmnopqrstuvxyz”
newSequence1=“”
对于idx,枚举中的字符(Sequence1):
对于startIndex,索引中的endIndex要删除:
如果startIndex