2014-01-27 34 views
0

大概这个笑话,但不知何故我无法解决这个问题。我从我想从每两个元素剥离一些字符,使列表的另一个列表我有一个Python列表中的列表,我想从每两个元素中去掉一些字符

list = [['A*68:02:01:03', 'A*33:01:01', '1'], 
     ['A*68:02:01:02', 'A*33:01:01', '1'], 
     ['A*68:02:01:01', 'A*33:01:01', '1']] 

required output = [['A*68:02', 'A*33:01', '1'], 
        ['A*68:02', 'A*33:01', '1'], 
        ['A*68:02', 'A*33:01', '1']] 

最后,我只想打印独特的元素列表清单。如上面的情况下,所有三个元素是相同的,所以输出应该是:

output = ['A*68:02', 'A*33:01', '1'] 

感谢您的帮助

+0

对于独特的列表式的东西,看看[sets](http://docs.python.org/2/library/sets.html)。 – nmichaels

回答

1

你可以做这样的事情:

def prefix(string): 
    """ 
    Returns the prefix of a string, including all characters 
    up until the second colon. 
    """ 
    return ":".join(string.split(":", 2)[:2]) 

def unique(iterable): 
    """ 
    Returns the unique elements in iterable, maintaining the 
    elements' relative order. 
    """ 
    result = [] 
    seen = set() 
    for el in iterable: 
     if el not in seen: 
      seen.add(el) 
      result.append(el) 
    return result 

L = [ 
    ['A*68:02:01:03', 'A*33:01:01', '1'], 
    ['A*68:02:01:02', 'A*33:01:01', '1'], 
    ['A*68:02:01:01', 'A*33:01:01', '1'], 
] 
prefixes = [(prefix(el[0]), prefix(el[1]), el[2]) for el in L] 

# The built-in class set accepts an iterable and returns a set, 
# an object with all duplicate elements removed. Since sets are 
# unordered, converting the set back to a list will likely 
# produce a list in which the original elements have lost their 
# relative order. 
# If this is a problem you can use the unique function from above. 
uniq = list(set(prefixes)) 

# If you really need a list of lists, instead of a list of tuples. 
uniq = [list(el) for el in uniq] 

我已经改名为您输入列表为L,因为命名为list会隐藏内置函数列表。

2
>>> lst = [['A*68:02:01:03', 'A*33:01:01', '1'], ['A*68:02:01:02', 'A*33:01:01', '1'], ['A*68:02:01:01', 'A*33:01:01', '1']] 
>>> newLst = [tuple(':'.join(data.split(':', 2)[:2]) for data in sublist) for sublist in lst] 
>>> set(newLst) 
{('A*68:02', 'A*33:01', '1')} 

有趣的是':'.join(data.split(':', 2)[:2]。该分会将冒号分开data,只取前两个部分并再次加入。这样,我们在第二个冒号(包括那个)之后将所有东西都剥离。

其余的只是一个列表理解通过嵌套列表。我们还需要将内部列表转换为元组,因此当我们调用set()时,它们是可散列的。这样做会摆脱所有重复。

2

我想你想:

lst = [['A*68:02:01:03', 'A*33:01:01', '1'], 
     ['A*68:02:01:02', 'A*33:01:01', '1'], 
     ['A*68:02:01:01', 'A*33:01:01', '1']] 

output = [] 

for item in lst: 
    processed = [":".join(s.split(":")[:2]) for s in item] 
    if processed not in output: 
     output.append(processed) 

注意:不要叫自己的变量之类的东西list

相关问题