简体   繁体   English

python 将元组元素与索引/键合并

[英]python merge tuples elements with index/key

I'm trying to merge columns values from tuples with an index:我正在尝试将元组中的列值与索引合并:

source tuples with a lot of timestamps (1440 ~):带有大量时间戳的源元组(1440 ~):

tuples = [('2022-10-15 01:16:00', '5', '', '', 'hdd1', '1234'),
          ('2022-10-15 01:16:00', '', '4', '', 'hdd1', '1234'),
          ('2022-10-15 01:17:00', '10', '', '', 'hdd1', '1234'),
          ('2022-10-15 01:17:00', '', '25', '', 'hdd1', '1234'),
          ('2022-10-15 01:18:00', '1', '', '', 'hdd1', '1234'),
          ('2022-10-15 01:18:00', '', '2', '', 'hdd1', '1234'),
          ...]

the index is the first element.索引是第一个元素。

desired tuples output:所需的元组 output:

[('2022-10-15 01:16:00', '5', '4', '', 'hdd1', '1234'),
 ('2022-10-15 01:17:00', '10', '25', '', 'hdd1', '1234'),
 ('2022-10-15 01:18:00', '1', '2', '', 'hdd1', '1234')]

my code:我的代码:

tuples =   [('2022-10-15 01:16:00', '5', '', '', 'hdd1', '1234'), ('2022-10-15 01:16:00', '', '4', '', 'hdd1', '1234'),('2022-10-15 01:17:00', '10', '', '', 'hdd1', '1234'), ('2022-10-15 01:17:00', '', '25', '', 'hdd1', '1234'), ('2022-10-15 01:18:00', '1', '', '', 'hdd1', '1234'), ('2022-10-15 01:18:00', '', '2', '', 'hdd1', '1234')]
result = []
key = lambda t: t[0]
for letter,items in itertools.groupby(sorted(tuples,key=key),key):
    items = list(items)
    if len(items) == 1:
        result.append(items[0]+(0,0))
    else:
        result.append(items[0]+items[1][1:])
print(result)

many thanks for any help非常感谢您的帮助

I think something like this is what you want:我认为这样的事情就是你想要的:

from itertools import groupby
result = []
key = lambda t: t[0]
for _,items in groupby(sorted(tuples, key=key), key):
    item = None
    for i, it in enumerate(items):
        # First item in group. Need to convert to list to edit.
        if not item: item = list(it)
        # Not first. Update item at correct index.
        else: item[1 + i] = it[1 + i]
    # Convert back to tuple and save.
    result.append(tuple(item))

for item in result: print(item)

Output: Output:

('2022-10-15 01:16:00', '5', '4', '', 'hdd1', '1234')
('2022-10-15 01:17:00', '10', '25', '', 'hdd1', '1234')
('2022-10-15 01:18:00', '1', '2', '', 'hdd1', '1234')

Here is a solution using a dictionary to store the date while iterating over the tuples.这是一个在遍历元组时使用字典存储日期的解决方案。

#empty dict with date as key and a list placeholder as value
r = {t[0]:["", "", "", "", ""] for t in tuples} 


#iterate over the tuples and populate the dict
for (date, *other_fields) in tuples:
    for i, value in enumerate(other_fields):
        if value: #skip if it's empty
            r[date][i] = value


#convert the dictionary in a list of tuples
r = [tuple([k, *v]) for k,v in r.items()]
print(r)

#[('2022-10-15 01:16:00', '5', '4', '', 'hdd1', '1234'), ('2022-10-15 01:17:00', '10', '25', '', 'hdd1', '1234'), ('2022-10-15 01:18:00', '1', '2', '', 'hdd1', '1234')]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM