简体   繁体   English

将列表中的一项替换或(分解)为基于原始项目的多个项目 - Python

[英]Replacing or (break down) one item in a list with (into) multiple items based on the original item - Python

I have a list as shown below:我有一个列表,如下所示:

a = ['3X0', '0', '0.7596', '0.7525', '2X0', '0', '0', '0', '0.7543', '2X0.1', '0.1'] 

I want to replace the items which have (X) with multiple items according to the item itself:我想根据项目本身将具有 (X) 的项目替换为多个项目:

I want to replace 3X0 with three zeros, 2X0 with two zeros and 2X0.1 with two 0.1 so the list becomes:我想用三个零替换 3X0,用两个零替换 2X0,用两个 0.1 替换 2X0.1,因此列表变为:

a_break = ['0', '0', '0', '0', '0.7596', '0.7525', '0', '0', '0', '0', '0', '0.7543', '0.1', '0.1', '0.1] 

Then, I want to sum up just the identical items which are close to each other (rewrite them using the original X form) as follows:然后,我只想总结彼此接近的相同项目(使用原始 X 形式重写它们)如下:

final_a= ['4X0', '0.7596', '0.7525', '5X0', '0.7543', '3X0.1']  

I am stuck, I tried to split the items with (X) using the split function but this did not work as it made the list even more complicated我被卡住了,我尝试使用拆分 function 将项目拆分为 (X) 但这没有用,因为它使列表变得更加复杂

A solution with two fairly simple generators:具有两个相当简单的生成器的解决方案:

def unpack_x(xs):
    for x in xs:
        n, x = (1, x) if 'X' not in x else x.split('X')
        for _ in range(int(n)):
            yield x


def pack_x(xs):
    n, p = 0, None
    for x in xs:
        if x == p or p is None:
            n, p = n + 1, x
        else:
            yield f'{n}X{p}' if n > 1 else p
            n, p = 1, x
    yield f'{n}X{p}' if n > 1 else p


sample = ['3X0', '0', '0.7596', '0.7525', '2X0', '0', '0', '0', '0.7543', '2X0.1', '0.1']
print(list(unpack_x(sample)))
print(list(pack_x(unpack_x(sample))))

Output: Output:

['0', '0', '0', '0', '0.7596', '0.7525', '0', '0', '0', '0', '0', '0.7543', '0.1', '0.1', '0.1']
['4X0', '0.7596', '0.7525', '5X0', '0.7543']

Or almost the same solution which returns lists, if you prefer:或者几乎相同的返回列表的解决方案,如果你愿意的话:

def unpack_x(xs):
    result = []
    for x in xs:
        n, x = (1, x) if 'X' not in x else x.split('X')
        for _ in range(int(n)):
            result.append(x)
    return result


def pack_x(xs):
    result = []
    n, p = 0, None
    for x in xs + [None]:
        if x == p or p is None:
            n, p = n + 1, x
        else:
            result.append(f'{n}X{p}' if n > 1 else p)
            n, p = 1, x
    return result


sample = ['3X0', '0', '0.7596', '0.7525', '2X0', '0', '0', '0', '0.7543', '2X0.1', '0.1']
print(list(unpack_x(sample)))
print(list(pack_x(unpack_x(sample))))

Here's one way of doing it:这是一种方法:

a = ['3X0', '0', '0.7596', '0.7525', '2X0', '0', '0', '0', '0.7543', '0.1', '0.1']

# construct a new list with additional zeros
new = []
for item in a:
    if "X" in item:
        # append a list containing the required zeros
        new += [item.split("X")[1] for _ in range(int(item.split("X")[0]))]
    else:
        new.append(item)

print(new)
['0', '0', '0', '0', '0.7596', '0.7525', '0', '0', '0', '0', '0', '0.7543', '0.1', '0.1']

# construct the final list with the zeros counted
final = []
nsame = 0  # a counter for the current number of consecutive identical values

l0 = new[0]
for nextitem in new[1:]:
    if nextitem == l0:
        # count the same values
        nsame += 1
    else:
        if nsame > 0:
            # add the number count
            final.append(f"{nsame + 1}X{l0}")
        else:
            final.append(l0)
        nsame = 0
        l0 = nextitem

# add in any final values
if nsame > 0:
    final.append(f"{nsame + 1}X{l0}")

print(final)
['4X0', '0.7596', '0.7525', '5X0', '0.7543', '2X0.1']

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

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