Python 更优雅/更具蟒蛇风格的列表解包方式?

Python 更优雅/更具蟒蛇风格的列表解包方式?,python,list,Python,List,有没有一种更优雅/更像蟒蛇的方式来打开这个列表 feature_set = [['flew'], ['homes'], ['fly home']] 为此: flew|homes|fly home 没有这个丑陋的代码: output = '' for feature in feature_set: if len(feature) < 2: output += ''.join(feature) + '|' if len(feature) > 1: output +=

有没有一种更优雅/更像蟒蛇的方式来打开这个列表

feature_set = [['flew'], ['homes'], ['fly home']]
为此:

flew|homes|fly home
没有这个丑陋的代码:

output = ''
for feature in feature_set:
    if len(feature) < 2: output += ''.join(feature) + '|'
    if len(feature) > 1: output += ' '.join(feature) + '|'
print(output[:-1])
output=''
对于要素集合中的要素:
如果len(特征)<2:output+=''.join(特征)+'|'
如果len(特征)>1:output+=''.join(特征)+'|'
打印(输出[:-1])

使用
chain.from\u iterable
将列表展平,然后使用
str.join

Ex:

from itertools import chain
feature_set = [['flew'], ['homes'], ['fly home']]

print("|".join(chain.from_iterable(feature_set)))
flew|homes|fly home
输出:

from itertools import chain
feature_set = [['flew'], ['homes'], ['fly home']]

print("|".join(chain.from_iterable(feature_set)))
flew|homes|fly home

使用
chain.from_iterable
将列表展平,然后使用
str.join

Ex:

from itertools import chain
feature_set = [['flew'], ['homes'], ['fly home']]

print("|".join(chain.from_iterable(feature_set)))
flew|homes|fly home
输出:

from itertools import chain
feature_set = [['flew'], ['homes'], ['fly home']]

print("|".join(chain.from_iterable(feature_set)))
flew|homes|fly home

我希望你想要这样的东西

'|'.join([inList[0] for inList in feature_set])

我希望你想要这样的东西

'|'.join([inList[0] for inList in feature_set])

首先通过
map
连接每个内部列表的每个元素,然后再次连接
map
以获得
map
结果

"|".join(map(lambda x: " ".join(x), feature_set))

首先通过
map
连接每个内部列表的每个元素,然后再次连接
map
以获得
map
结果

"|".join(map(lambda x: " ".join(x), feature_set))
“|”.join(itertools.chain.from_iterable(feature_set))
“|”.join(itertools.chain.from_iterable(feature_set))