Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.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_List_Dictionary - Fatal编程技术网

Python中的元组列表

Python中的元组列表,python,list,dictionary,Python,List,Dictionary,我有一个python列表,如下所示: test = ['Vlan101', '1.1.1.0/24', 'Vlan111', '2.1.1.0/24', 'Vlan701', '3.1.1.0/24', 'Vlan703', '4.1.1.0/24', '5.5.5.0/24'] 我希望用它创建元组,如下所示: Vlan101,1.1.1.0/24 Vlan111,2.1.1.0/24 Vlan701,3.1.1.0/24 Vlan703,4.1.1.0/24, 5.5.5.0/24 重要的

我有一个python列表,如下所示:

test = ['Vlan101', '1.1.1.0/24', 'Vlan111', '2.1.1.0/24', 'Vlan701', '3.1.1.0/24', 'Vlan703', '4.1.1.0/24', '5.5.5.0/24']
我希望用它创建元组,如下所示:

Vlan101,1.1.1.0/24
Vlan111,2.1.1.0/24
Vlan701,3.1.1.0/24
Vlan703,4.1.1.0/24, 5.5.5.0/24
重要的是最后一个,它对同一个键有两个值。
这是我的头巾

可以使用
zip
+
iter
作为通用方法

i = len(test)%2
s = iter(test[:-(2+i)])
[x for x in zip(s,s)] + [tuple(test[-(2+i):])]
输出

[('Vlan101', '1.1.1.0/24'),
 ('Vlan111', '2.1.1.0/24'),
 ('Vlan701', '3.1.1.0/24'),
 ('Vlan703', '4.1.1.0/24', '5.5.5.0/24')]
如果希望外部对象也是一个
元组

tuple(x for x in zip(s,s)) + (tuple(test[-(2+i):]),)

(('Vlan101', '1.1.1.0/24'),
 ('Vlan111', '2.1.1.0/24'),
 ('Vlan701', '3.1.1.0/24'),
 ('Vlan703', '4.1.1.0/24', '5.5.5.0/24'))

您可以在此处使用
itertools.groupby()
+
zip()

from itertools import groupby

test = [
    "Vlan101",
    "1.1.1.0/24",
    "Vlan111",
    "2.1.1.0/24",
    "Vlan701",
    "3.1.1.0/24",
    "Vlan703",
    "4.1.1.0/24",
    "5.5.5.0/24",
]

print(
    [
        (x,) + y
        for x, y in zip(
            (x_ for x_ in test if x_.startswith("Vlan")),
            (
                tuple(g)
                for k, g in groupby(test, key=lambda x__: x__.startswith("Vlan"))
                if not k
            ),
        )
    ] 
)

# [('Vlan101', '1.1.1.0/24'), ('Vlan111', '2.1.1.0/24'), ('Vlan701', '3.1.1.0/24'), ('Vlan703', '4.1.1.0/24', '5.5.5.0/24')]

元组的第二、第三等元素看起来像ip地址。您可以使用正则表达式来识别特定的格式。(我冒着风险假设你会玩弄ip地址)

这里有另一个解决方案,没有或干净,但允许任意数量的地址

import re

test = [
        'Vlan101',
        '1.1.1.0/24',
        'Vlan111',
        '2.1.1.0/24',
        'Vlan701', 
        '3.1.1.0/24', 
        'Vlan703', 
        '4.1.1.0/24', 
        '5.5.5.0/24']

result = [[]]  # we first store the stuff as lists to allow for appending
for item in test:

    # matches ipaddresses with xxx.xxx.xxx.xxx/xx
    if re.match(r'\d{,3}\.\d{,3}\.\d{,3}\.\d{,3}/\d+', item):
        result[-1].append(item)    # appends an ipaddress
    else:
        result.append([item])      # appends a new 'Vlan...' (i.e. anything but an ipaddress)

tups = list(map(tuple, result))    # cast each of the lists into tuples

print(result)
print(tups)
输出:

[[], ['Vlan101', '1.1.1.0/24'], ['Vlan111', '2.1.1.0/24'], ['Vlan701', '3.1.1.0/24'], ['Vlan703', '4.1.1.0/24', '5.5.5.0/24']]
[(), ('Vlan101', '1.1.1.0/24'), ('Vlan111', '2.1.1.0/24'), ('Vlan701', '3.1.1.0/24'), ('Vlan703', '4.1.1.0/24', '5.5.5.0/24')]
[('Vlan101', '1.1.1.0/24'), ('Vlan111', '2.1.1.0/24'), ('Vlan701', '3.1.1.0/24'), ('Vlan703', '4.1.1.0/24', '5.5.5.0/24')]
空的
[]
()
在开头,以防您的
测试
列表立即以地址开始(它提供了
结果[-1]
的回退)

如果您希望删除这些结果,只需执行一个切片即可

print(tups[1:])
输出:

[[], ['Vlan101', '1.1.1.0/24'], ['Vlan111', '2.1.1.0/24'], ['Vlan701', '3.1.1.0/24'], ['Vlan703', '4.1.1.0/24', '5.5.5.0/24']]
[(), ('Vlan101', '1.1.1.0/24'), ('Vlan111', '2.1.1.0/24'), ('Vlan701', '3.1.1.0/24'), ('Vlan703', '4.1.1.0/24', '5.5.5.0/24')]
[('Vlan101', '1.1.1.0/24'), ('Vlan111', '2.1.1.0/24'), ('Vlan701', '3.1.1.0/24'), ('Vlan703', '4.1.1.0/24', '5.5.5.0/24')]

最后一个是列表tho。请显示您迄今为止所做的尝试。@felipsmartins您的意思是什么?行中的第一项是否总是以
“Vlan”
开头?它显然不是泛化的,这取决于您认为泛化是什么。我假设它适用于所有线对和所有线对+端点处的三元组,中间取2对线对,然后断开。OP可能不需要它,只是提到而已。是的,我们可以想到许多场景,没有解决方案可以涵盖所有场景;)这假设了以下结构:
key1,val1,key2,val2,…
感谢您的评论