映射列表中具有相应位置的2D列表

时间:2017-01-11 23:23:29

标签: python arrays

我跟随2D-Array,如:

[['4470', '4753.5', '5682', '4440', '6113.5', '3661.5', '7555.3', '6090.3', '5147.3', '4296'], ['4468.5', '4742.5', '5297', '5501.8', '5061.3', '2933.5', '6367.5', '6053.8', '5654.3', '3793.3']]

我想将其转换为新列表,每个元素都映射到整个列表中的位置,如:

[[('1','4470'),('2', '4753.5'), ...], [('11', '4468.5'), ...]]

下一个子列表中的位置应该是前一个子列表中的下一个值。此外,每个子列表的长度都相同。

4 个答案:

答案 0 :(得分:2)

为了实现这一目标,您可以将enumerate()嵌套列表理解表达式一起使用:

my_list = [['4470', '4753.5', '5682', '4440', '6113.5', '3661.5', '7555.3', '6090.3', '5147.3', '4296'], ['4468.5', '4742.5', '5297', '5501.8', '5061.3', '2933.5', '6367.5', '6053.8', '5654.3', '3793.3']]
sub_list_len = len(my_list[0])  # all the sub-lists are of constant length

new_list = [[(str(i), j) for i, j in enumerate(sub_list, n*sub_list_len+1)] \
                 for n, sub_list in enumerate(my_list)]

new_list持有的最终价值为:

[[('1', '4470'), ('2', '4753.5'), ('3', '5682'), ('4', '4440'), ('5', '6113.5'), ('6', '3661.5'), ('7', '7555.3'), ('8', '6090.3'), ('9', '5147.3'), ('10', '4296')], [('11', '4468.5'), ('12', '4742.5'), ('13', '5297'), ('14', '5501.8'), ('15', '5061.3'), ('16', '2933.5'), ('17', '6367.5'), ('18', '6053.8'), ('19', '5654.3'), ('20', '3793.3')]]

答案 1 :(得分:1)

这应该有效:

def list2tupple(array):
    return_list = []
    for item in array:
        for iteration, value in enumerate(item):
            return_list.append((str((iteration+1)), str(value)))
    return return_list

<强> Exapmle

a = [['a']]
print(list_tupple(a))
output: [('1', 'a')]

你可以使用列表理解,但我认为这更清楚。

答案 2 :(得分:1)

我想给你一个简单的例子,我创建了一个新的列表,然后计数器知道我在哪个键上。

我通过每个子列表迭代my_list,而不是遍历每个子列表项。

我正在创建一个元组,我将附加到新列表中。

    my_list = [['4470', '4753.5', '5682', '4440', '6113.5', '3661.5', '7555.3', '6090.3', '5147.3', '4296'],
           ['4468.5', '4742.5', '5297', '5501.8', '5061.3', '2933.5', '6367.5', '6053.8', '5654.3', '3793.3']]

new_list = []
count = 1  # the key
for sub_list in my_list:  # gets both of the sub lists
    for item in sub_list:  # gets the items in each list
        new_list.append((str(count), item))  # appends a tuple to a new list
        count += 1

由于您的问题包含以下字词:键和值,我将添加另一种方法来安排my_list为dictionary的键和值通知。

my_list = [['4470', '4753.5', '5682', '4440', '6113.5', '3661.5', '7555.3', '6090.3', '5147.3', '4296'],
           ['4468.5', '4742.5', '5297', '5501.8', '5061.3', '2933.5', '6367.5', '6053.8', '5654.3', '3793.3']]

new_dict = {}  # new dictionary
count = 1  # the key
for sub_list in my_list:  # gets both of the sub lists
    for item in sub_list:  # gets the items in each list
        new_dict[count] = item  # appends key and value to the dict
        count += 1

答案 3 :(得分:0)

相同但不同 - 使用itertools.count

import itertools

a = [['4470', '4753.5', '5682',  .....]]

item_nbrs = itertools.count(1)
def tuples(iterable):
    for thing in iterable:
        yield (next(item_nbrs), thing)

z = [list(tuples(iterable)) for iterable in a]

更具功能性:

item_nbrs = itertools.count(1)
f = map(tuples, a)
g = map(list, f)
h = list(g)

item_nbrs = itertools.count(1)
k = list(map(list, map(tuples, a)))


>>> z == h == k
True
>>>