对每一行中的单词进行排序

时间:2018-12-20 13:45:52

标签: python sorting

我在一行中加上

ΜΔΣ,ΘΟΡ,ΑΛΒ

和一个按字母顺序对单词进行排序的python脚本

items = input("Input comma separated sequence of words")
words = [word for word in items.split(",")]
print(",".join(sorted(list(set(words)))))

输出正确答案

ΑΛΒ,ΘΟΡ,ΜΔΣ

我想输入多行输入,例如

ΜΔΣ,ΘΟΡ,ΑΛΒ
ΜΔΣ,ΣΥΝ,ΑΛΒ

并获得

ΑΛΒ,ΘΟΡ,ΜΔΣ
ΑΛΒ,ΜΔΣ,ΣΥΝ

我必须对代码进行哪些更改?谢谢。

1 个答案:

答案 0 :(得分:3)

编写一个函数,将其映射到您的行列表中。

Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "/home/rytse/anaconda3/envs/gerry/lib/python3.6/site-packages/geopandas/__init__.py", line 4, in <module>
        from geopandas.io.file import read_file
    File "/home/rytse/anaconda3/envs/gerry/lib/python3.6/site-packages/geopandas/io/file.py", line 3, in <module>
        import fiona
    File "/home/rytse/anaconda3/envs/gerry/lib/python3.6/site-packages/fiona/__init__.py", line 69, in <module>
        from fiona.collection import Collection, BytesCollection, vsi_path
    File "/home/rytse/anaconda3/envs/gerry/lib/python3.6/site-packages/fiona/collection.py", line 9, in <module>
        from fiona.ogrext import Iterator, ItemsIterator, KeysIterator
    ImportError: /home/rytse/anaconda3/envs/gerry/lib/python3.6/site-packages/fiona/../../../libgdal.so.20: undefined symbol: _ZN9kmlengine4Href5ParseERKSs

我的功能与您已经完成的功能非常相似,请注意

>>> def sort_unique_words(words): 
...:     return ','.join(sorted(set(words.split(',')))) 
...:                                                                                                                                                                         
>>> line = 'ΜΔΣ,ΘΟΡ,ΑΛΒ'                                                                                                                                                     
>>> sort_unique_words(line)                                                                                                                                                  
'ΑΛΒ,ΘΟΡ,ΜΔΣ'
>>>                                                                                                                                                                          
>>> lines = '''ΜΔΣ,ΘΟΡ,ΑΛΒ 
...: ΜΔΣ,ΣΥΝ,ΑΛΒ'''                                                                                                                                                          
>>>                                                                                                                                                                          
>>> '\n'.join(map(sort_unique_words, lines.splitlines()))                                                                                                                    
'ΑΛΒ,ΘΟΡ,ΜΔΣ\nΑΛΒ,ΜΔΣ,ΣΥΝ'
>>> print(_)                                                                                                                                                                 
ΑΛΒ,ΘΟΡ,ΜΔΣ
ΑΛΒ,ΜΔΣ,ΣΥΝ

相同
[word for word in items.split(",")]

如果要在结果中包含重复项,请摆脱items.split(",") 构造函数。