我是python的新手,我需要帮助来创建/表示此数据类型的数组。
link1|2-3,6-9,12-13|4-5,10-11,14-16
格式为link_name|boundary1|boundary2
我需要将值A
添加到boundary1
和B
添加到boundary2
这应该导致:
position : 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
value : A A B B A A A A B B A A B B B
答案 0 :(得分:1)
虽然可能没有基于字典的方法那么高效,但这也完成了工作:
data = "link1|2-3,6-9,12-13|4-5,10-11,14-16"
link_name, boundary_1, boundary_2 = data.split("|")
boundary_1 = [(b, "A") for b in boundary_1.split(",")]
boundary_2 = [(b, "B") for b in boundary_2.split(",")]
temp = boundary_1 + boundary_2
# We sort the temp list based on the starting point
temp = sorted(temp, key=lambda x: int(x[0].split("-")[0]))
position = []
value = []
for pair, letter in temp:
start, stop = pair.split("-")
# We loop through all the intermittent values that are not in the input
for val in range(int(start), int(stop)+1):
position.append(val)
value.append(letter)
print "position:\t{}".format("\t".join([str(x) for x in position]))
print "value: \t{}".format("\t".join([x for x in value]))
这会按照您的要求生成输出(使用制表符分隔):
position: 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
value: A A B B A A A A B B A A B B B
答案 1 :(得分:0)
这就是我提出的:
data = data.split('|')[1]
data = data.split(',')
data = [d.split('-') for d in data]
data = [(int(d[0]), int(d[1])) for d in data]
positions = range(2, 17)
values = ['A' if any(data[j][1] >= i >= data[j][0] for j in range(len(data))) else 'B' for i in positions]
基本上,除了第一个范围列表之外,不需要任何信息,因为默认情况下,任何非“A”的信息都是“B”。我使用我的代码的前四行将您的范围数据放入这种格式:
[(2,3), (6,9), (12,13)]
然后进行列表理解以对数据进行分类。
答案 2 :(得分:0)
你可能想要像字典一样的东西。
line = "link1|2-3,6-9,12-13|4-5,10-11,14-16"
a, b = line.split("|")[1:]
a = [item for i in a.split("-") for item in i.split(",")]
b = [item for i in b.split("-") for item in i.split(",")]
data = {k:"A" for k in a}
data.update({k:"B" for k in b})
print data
答案 3 :(得分:0)
你可能需要在python中使用字典:
data = {}
def mapToDict(boundary, value):
for item in boundary.split(','):
for position in item.split('-'):
data[position]=value
s='link1|2-3,6-9,12-13|4-5,10-11,14-16'
items = s.split('|')
mapToDict(items[1],'A')
mapToDict(items[2],'B')
print data
输出:
{'11': 'B', '10': 'B', '13': 'A', '12': 'A', '14': 'B', '16': 'B', '3': 'A', '2': 'A', '5': 'B', '4': 'B', '6': 'A', '9': 'A'}
答案 4 :(得分:0)
def create_lsit(boundry_list):
list_t = []
for i in boundry_list:
l = i.split('-')
ran = range(eval(l[0]),eval(l[1])+1)
for j in ran:
list_t.append(j)
return list_t
def create_dic(dic,input_list, val):
for i in input_list:
dic[i] = val
def get_some(link_name,boundary1,boundary2):
main_dic = {}
boundry1 = create_lsit(boundary1.split(','))
create_dic(main_dic,boundry1, 'a')
boundry2 = create_lsit(boundary2.split(','))
create_dic(main_dic,boundry2, 'b')
for key in sorted(main_dic.keys()):
print key, ' ', main_dic[key]
data = 'link1|2-3,6-9,12-13|4-5,10-11,14-16'.split('|')
get_some(data[0],data[1],data[2])
这可能很冗长,但我认为这就是你要找的东西。