使用Python将多个列表组合成特定的字符串格式

时间:2018-03-13 23:05:27

标签: python

我有一个非常具体的问题。 我有六个列表需要组合成一个大字符串。 每个列表都将括在最后一个大字符串的括号()中。

例如,我已声明如下六个列表:

2018/03/13 22:25:06 [error] 2104#2104: *44 upstream prematurely closed connection while reading response header from upstream, client: 139.162.251.201, server: _, request: "GET / HTTP/1.0", upstream: "http://unix:/home/django/gunicorn.socket:/"
2018/03/13 22:28:19 [error] 2104#2104: *46 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 93.55.242.118, server: _, request: "GET / HTTP/1.1", upstream: "http://unix:/home/django/gunicorn.socket:/", host: "46.101.7.245"
2018/03/13 22:31:51 [error] 2104#2104: *49 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 93.55.242.118, server: _, request: "GET / HTTP/1.1", upstream: "http://unix:/home/django/gunicorn.socket:/", host: "46.101.7.245"
2018/03/13 22:32:05 [error] 2104#2104: *52 connect() to unix:/home/django/gunicorn.socket failed (111: Connection refused) while connecting to upstream, client: 93.55.242.118, server: _, request: "GET /admin HTTP/1.1", upstream: "http://unix:/home/django/gunicorn.socket:/admin", host: "46.101.7.245"
2018/03/13 22:40:18 [error] 2104#2104: *55 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 93.55.242.118, server: _, request: "GET /admin HTTP/1.1", upstream: "http://unix:/home/django/gunicorn.socket:/admin", host: "46.101.7.245"
2018/03/13 22:40:23 [error] 2104#2104: *55 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 93.55.242.118, server: _, request: "GET / HTTP/1.1", upstream: "http://unix:/home/django/gunicorn.socket:/", host: "46.101.7.245"
2018/03/13 22:44:08 [error] 2104#2104: *59 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 93.55.242.118, server: _, request: "GET / HTTP/1.1", upstream: "http://unix:/home/django/gunicorn.socket:/", host: "46.101.7.245"
2018/03/13 22:44:08 [error] 2104#2104: *62 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 93.55.242.118, server: _, request: "GET /robots.txt HTTP/1.1", upstream: "http://unix:/home/django/gunicorn.socket:/robots.txt", host: "46.101.7.245"
2018/03/13 22:56:30 [error] 2104#2104: *64 connect() to unix:/home/django/gunicorn.socket failed (111: Connection refused) while connecting to upstream, client: 93.55.242.118, server: _, request: "GET / HTTP/1.1", upstream: "http://unix:/home/django/gunicorn.socket:/", host: "46.101.7.245"
2018/03/13 22:58:36 [error] 2104#2104: *67 connect() to unix:/home/django/gunicorn.socket failed (111: Connection refused) while connecting to upstream, client: 93.55.242.118, server: _, request: "GET / HTTP/1.1", upstream: "http://unix:/home/django/gunicorn.socket:/", host: "46.101.7.245"

现在我想将所有六个列表组合成一个大字符串,如下所示:

primaryP=['PSUP']
primaryG=['NSUP']
primaryAin=['IBIAS_200N','VREF']
primaryAout=['IOUTN','IOUTP','IBIAS_OUT<1:0>','ICAL<1:0>']
primaryDin=['DN', 'DNB', 'EN', 'FAST_START', 'RESET', 'UP', 'UPB', 'DEGEN_TRIM<2:0>']
primaryDout=[]

注意我希望每个列表中的每个列表()和每个元素都用双引号&#34;&#34;。另外,我想在最终字符串中单独拆分总线/向量位,如上所示。

这是我到目前为止所尝试的内容:

CSRPGPrimary='//CSRPG PRIMARY ("PSUP") ("NSUP") ("IBIAS_200N" "VREF") ("IOUTN" "IOUTP" "IBIAS_OUT<1>" "IBIAS_OUT<0>" "ICAL<1>" "ICAL<0>") ("DN" "DNB" "EN" "FAST_START" "RESET" "UP" "UPB" "DEGEN_TRIM<2>" "DEGEN_TRIM<1>" "DEGEN_TRIM<0>") ()'

但最终输出并不是我想要的:

primaryP=['PSUP']
primaryG=['NSUP']
primaryAin=['IBIAS_200N','VREF']
primaryAout=['IOUTN','IOUTP','IBIAS_OUT<1:0>','ICAL<1:0>']
primaryDin=['DN', 'DNB', 'EN', 'FAST_START', 'RESET', 'UP', 'UPB', 'DEGEN_TRIM<3:0>']
primaryDout=[]

primaryMaster=[primaryP, primaryG, primaryAin, primaryAout, primaryDin, primaryDout]
CSRPGPrimary=['("'+' '.join(item)+'")' if item else '()' for item in primaryMaster]
CSRPGPrimary="//CSRPG PRIMARY " + ' '.join(CSRPGPrimary)

print("CSRPGPrimary=", CSRPGPrimary)

任何建议,特别是什么是分割矢量/总线位的最佳方法?

1 个答案:

答案 0 :(得分:1)

在步骤中执行此操作可能更简单。如果你真的想将它们全部合并到一个你无法调试的怪物列表中,那么你可以在它运行之后就这样做。

我们要做的第一件事就是将每个总线位规范转换为单独的元素。这是最棘手的一点。我不确定您使用的具体规则,但它会是这样的:

def split_bus_bits(lst):
    for element in lst:
        if element.endswith('>'):
            base, _, bits = element[:-1].partition('<')
            hi, lo = bits.split(':')
            for i in range(int(lo), int(hi)+1):
                yield f'{base}<{i}>'
        else:
            yield element

或者,如果你能理解正则表达式,这对于一个人来说似乎是一个好工作:

rbusbits = re.compile(r'(\w+)(?:<(\d+):(\d+)>)?')
def split_bus_bits(lst):
    for element in lst:
        base, hi, lo = rbusbits.match(element).groups()
        if hi:
            for i in range(int(lo), int(hi)+1):
                yield f'{base}<{i}>'
        else:
            yield base

现在,我们可以扩展每个子列表:

busbitsified = (split_bus_bits(sublist) for sublist in primaryMaster)            

剩下的步骤都简单得多。对于每个列表,我们希望将每个元素放在引号中:

quoted = ([f'"{element}"' for element in sublist] for sublist in busbitsified)

接下来,加入每个子列表:

joined = (' '.join(sublist) for sublist in quoted)

接下来,在每个字符串周围放置一个parens-that-a-a-up-up-sublist:

parenthesized = (f'({sublist})' for sublist in joined)

最后,将他们加入一个大字符串:

CSRPGPrimary="//CSRPG PRIMARY " + ' '.join(parenthesized)

结果似乎是你想要的结果:

'//CSRPG PRIMARY ("PSUP") ("NSUP") ("IBIAS_200N" "VREF") ("IOUTN" "IOUTP" "IBIAS_OUT<1:0>" "ICAL<1:0>") ("DN" "DNB" "EN" "FAST_START" "RESET" "UP" "UPB" "DEGEN_TRIM<3:0>") ()'

请注意,我没有为()做任何特别的事情。空子列表将不引用任何元素,将那些没有元素的元素连接到空字符串中,然后将该空字符串括起来。

如果你没有使用Python 3.6+,或者只是不理解f字符串,你可以用你最喜欢的str.format%来电替换它们,或者只是连接(例如,'"' + element + '"')。

从您的输出中,您似乎正在使用Python 2.7,但编写代码就像使用Python 3.在这种情况下,您应该from __future__ import print_function或更改print }语句删除无关的括号。除非你实际上想要打印两个字符串的reprs元组,而不是只打印两个字符串。在那种情况下,继续。