2017-06-02 48 views
2
x_list = ["I", "live", "in", "New", "-", "York", "City", ".", "I", "am" "from", "New", "-", "Delhi"] 

这是我的列表。我想要加入连字符“ - ”前后的单词。这样我的名单就变成了。如何连接符合特定标准的列表元素?

x_list = ["I", "live", "in", "New-York", "City", ".", "I", "am", "from", "New-Delhi"] 

是否有一个简短的方法来做到这一点?

+0

此列表是如何创建的?在分词之前处理这个问题可能会更容易。 – asongtoruin

+2

也不会调用你的变量'list' – asongtoruin

+0

不幸的是,这是在一个数据集中,我希望清理它。如果这是正常的文本,我可以很容易地使用正则表达式来正确标记它们。 – Djokester

回答

2

您可以将enumerate d for -loop:

lst = ["I", "live", "in", "New", "-", "York", "City"] 
for index, item in enumerate(lst): 
    if item == '-': 
     lst[index-1:index+2] = [''.join(lst[index-1:index+2])] 

print(lst) # ['I', 'live', 'in', 'New-York', 'City'] 

或者,如果你与短名单和几个'-'(在你的例子一样)打交道你也可以使用一个while循环。然而,这有二次运行时的行为,所以如果你关心性能不使用这个对于很多'-'大名单:

lst = ["I", "live", "in", "New", "-", "York", "City"] 
while '-' in lst: 
    pos = lst.index('-') 
    lst[pos-1:pos+2] = [''.join(lst[pos-1:pos+2])] 

print(lst) # ['I', 'live', 'in', 'New-York', 'City'] 
+2

第一个版本是连字符数的二次方。 – enedil

+0

@enedil是的,这就是为什么我还包括第二个版本:)你认为在答案中需要更明显的警告吗? – MSeifert

+0

我相信如此。软件工程师(尤其是初学者)对这些问题并不谨慎。 – enedil

4

有点怪异,但优雅的方式:

lst = ["I", "live", "in", "New", "-", "York", "City"] 

pattern = "<erpvinpervin>" 
s = pattern.join(lst) 
s = s.replace("{0}-{0}".format(pattern), "-") 
lst = s.split(pattern) 

由于pattern你可以使用任何不可能在列表中满足的任意字符串。

0
for index, item in enumerate(lista): 
    if item == '-': # Checks if current item is your separator 
     lista[index-1] = ''.join(lista[index-1:index+2]) # Joins the 3 indexes (new-york or whatever.) 
     lista.remove(lista[index]) # Removes the "garbage" that remained from the actual list 
     lista.remove(lista[index]) 

可能有更好的方法做到这一点,但这个工作正常,很容易理解。