Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.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_Arraylist - Fatal编程技术网

在python中用我的数据生成数组的最佳方法是什么?

在python中用我的数据生成数组的最佳方法是什么?,python,arraylist,Python,Arraylist,我是python新手,需要帮助从该数据类型生成/表示数组 link1|2-3,6-9,12-13|4-5,10-11,14-16 格式为link|u 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 这就是我想到的:

我是python新手,需要帮助从该数据类型生成/表示数组

link1|2-3,6-9,12-13|4-5,10-11,14-16
格式为
link|u 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

这就是我想到的:

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)]

然后进行列表理解以对数据进行分类。

您可能需要类似于字典的东西

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

您可能需要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'}

虽然可能没有基于词典的方法那么有效,但这也完成了工作:

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]))
这将按照您的要求生成输出(使用制表符分隔):


这可能很长,但我认为这正是您想要的。

顺便说一句,边界的长度各不相同-例如2-78、100-125等。上面提供的示例数据仅用于演示目的。希望这能帮上忙。我想在我尝试前4行时,在有人花时间帮助你之前,你需要展示一下解决问题的方法。。。发生错误。。。回溯(最近一次调用last):文件“”,第1行,在索引器中。错误:列表索引超出range@user3267156打印数据并查看其外观。此代码与您在问题中提供的条目非常匹配。非常感谢sashkello,此代码与我的数据库非常匹配。不客气!我希望它解决了你的问题,因为我认为其他一些建议并不完全正确
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
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])