简体   繁体   English

将str元素从列表耦合到元组列表

[英]coupling str elements from a list to a tuple list

I have the following list: 我有以下清单:

lines
['line_North_Mid', 'line_South_Mid',
 'line_North_South', 'line_Mid_South',
 'line_South_North','line_Mid_North' ]

I would like to couple them in a tuple list as follows, with respect to their names: 关于它们的名称,我想将它们结合在元组列表中,如下所示:

tuple_list
[('line_Mid_North', 'line_North_Mid'),
 ('line_North_South', 'line_South_North'),
 ('line_Mid_South', 'line_South_Mid')]

I thought maybe I could do a string search in the elements of the lines but it wont be efficient. 我想也许我可以做在的元素的字符串搜索lines ,但它不会是有效的。 Is there a better way to order lines elements in a way which would look like tuple_list 有没有更好的方式来排序lines元素,就像元组tuple_list一样

Paring Criteria: 配对条件:

If the both elements have the same Area_name: ('North', 'Mid', 'South') 如果两个元素都具有相同的Area_name :( ('North', 'Mid', 'South')

Eg: 'line_North_Mid' should be coupled with 'line_Mid_North' 例如: 'line_North_Mid'应与'line_Mid_North'结合使用

An order-agnostic O( n ) solution is possible using collections.defaultdict . 使用collections.defaultdict可以实现与订单无关的O( n )解决方案。 The idea is to use as our dictionary keys the last 2 components of your strings delimited by '_' , appending values from your input list. 这个想法是使用由字符串'_'分隔的字符串的最后2个部分作为我们的字典键,并在输入列表中附加值。 Then extract values and convert to a list of tuples. 然后提取值并转换为元组列表。

from collections import defaultdict

L = ['line_North_Mid', 'line_South_Mid',
     'line_North_South', 'line_Mid_South',
     'line_South_North', 'line_Mid_North']

dd = defaultdict(list)
for item in L:
    dd[frozenset(item.rsplit('_', maxsplit=2)[1:])].append(item)

res = list(map(tuple, dd.values()))

# [('line_North_Mid', 'line_Mid_North'),
#  ('line_South_Mid', 'line_Mid_South'),
#  ('line_North_South', 'line_South_North')]

Try this: 尝试这个:

from itertools import combinations

tuple_list = [i for i in combinations(lines,2) if i[0].split('_')[1] == i[1].split('_')[2] and i[0].split('_')[2] == i[1].split('_')[1]]

or I think this is better: 或者我认为这样更好:

[i for i in combinations(lines,2) if i[0].split('_')[1:] == i[1].split('_')[1:][::-1]]

You can use the following list comprehension: 您可以使用以下列表理解:

lines = ['line_Mid_North', 'line_North_Mid',
         'line_North_South', 'line_South_North',
         'line_Mid_South', 'line_South_Mid']

[(j,i) for i in lines for j in lines if j not in i 
       if set(j.split('_')[1:]) < set(i.split('_'))][::2]

[('line_Mid_North', 'line_North_Mid'),
 ('line_North_South', 'line_South_North'),
 ('line_Mid_South', 'line_South_Mid')]

I suggest you have a function that returns the same key for string that are supposed to be together (a grouping-key). 我建议您有一个函数,该函数为应该在一起的字符串返回相同的键(一个分组键)。

def key(s):
    # ignore first part and sort other 2 parts, so they will always be in same order
    _, part_1, part_2 = s.split('_')
    return tuple(sorted([part_1, part_2]))

The you have to use some grouping method; 您必须使用某种分组方法; I used defaultdict for example: 我以defaultdict为例:

import collections

lines = [
    'line_North_Mid', 'line_South_Mid',
    'line_North_South', 'line_Mid_South',
    'line_South_North','line_Mid_North',
]

dd = collections.defaultdict(list)
for s in lines:
    dd[key(s)].append(s)     # those with same key get grouped

print(list(tuple(v) for v in dd.values()))
# [
#     ('line_North_Mid', 'line_Mid_North'),
#     ('line_South_Mid', 'line_Mid_South'),
#     ('line_North_South', 'line_South_North'),
# ]

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

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