如何统计不同部分之间此字符串中的字母数量? 反过来说:如何在给出数字时获得字母表字符串表示?
输入:'A-G H-O P Q-Z'
输出:(7,8,1,10)
q = list()
for i in seq:
if i is char:
n = ord(i+1) - ord(i)
q.append(n)
答案 0 :(得分:5)
你可以这样做:
input_string = 'A-G H-O P Q-Z'
input_string = input_string.split()
q = []
for dletter in input_string:
if '-' in dletter:
q.append(1 + ord(dletter[2]) - ord(dletter[0]))
else:
q.append(1)
或者没有if
声明:
input_string = 'A-G H-O P Q-Z'
input_string = input_string.split()
q = []
for dletter in input_string:
q.append(1 + ord(dletter[-1]) - ord(dletter[0]))
但这是一个非常good answer
答案 1 :(得分:0)
a = "A-G N B-Z"
a = a.split()
b = []
for i in a:
b.extend([i.split("-")])
print(b)
for lists in b:
if len(lists) > 1:
print(lists, ":", ord(lists[1]) - ord(lists[0]) + 1)
else:
print(lists, ":", 1)
输出
[['A', 'G'], ['N'], ['B', 'Z']]
['A', 'G'] : 7
['N'] : 1
['B', 'Z'] : 25
a = "A-G N B-Z"
b = [i.split("-") for i in a.split()]
print(b)
for lists in b:
if len(lists) > 1:
print(lists, ":", ord(lists[1]) - ord(lists[0]) + 1)
else:
print(lists, ":", 1)
输出
[['A', 'G'], ['N'], ['B', 'Z']]
['A', 'G'] : 7
['N'] : 1
['B', 'Z'] : 25