简体   繁体   English

打印关于相似项目的两个相邻的 python 列表

[英]Printing two python lists next to each other with regard to similar items

I'm currently playing around with python to deal with duplicate music files.我目前正在使用 python 来处理重复的音乐文件。 For that I'm looking for a more pythonic way to print 2 lists (length could be unequal) next to each other.为此,我正在寻找一种更 Pythonic 的方式来打印彼此相邻的 2 个列表(长度可能不相等)。 It is import though, that the items, which have a similar name are in the same row and it would be great, if it wouldn't matter which lists contains more items.重要的是,具有相似名称的项目位于同一行中,如果哪个列表包含更多项目无关紧要,那就太好了。 The Formatting could also be a bit better.格式也可以更好一些。

This is what I came up with so far:到目前为止,这是我想出的:

a = ['01-06 How Does It Feel.mp3', '01-08 Never Walk Away.mp3']

b = ['01 Show Me a Sign.mp3',
 "02 Don't Let Go.mp3",
 '03 All Messed Up.mp3',
 '04 Promise Keeper.mp3',
 '05 Goodbye to You.mp3',
 '06 How Does It Feel.mp3',
 '07 Had Enough of You.mp3',
 '08 Never Walk Away.mp3',
 '09 Nothing Left at All.mp3',
 '10 Reality Show.mp3',
 '11 Killing With Kindness.mp3']


def checkTitleName(org, song):
    for i in org:
        if song in i:
            return i
        
    return None
    

def listViewCompare(org, dub):
    out = ""
    for i in dub:
        match = checkTitleName(org, i)
        if  match == None:
            line = [i, "\t\t --> \t"]
            print(out.join(line))

        else:
            line = [i, "\t\t --> \t", match]
            print(out.join(line))

                
print(listViewCompare(a,b))

Output: Output:

01 Show Me a Sign.mp3        -->    
02 Don't Let Go.mp3      -->    
03 All Messed Up.mp3         -->    
04 Promise Keeper.mp3        -->    
05 Goodbye to You.mp3        -->    
06 How Does It Feel.mp3      -->    01-06 How Does It Feel.mp3
07 Had Enough of You.mp3         -->    
08 Never Walk Away.mp3       -->    01-08 Never Walk Away.mp3
09 Nothing Left at All.mp3       -->    
10 Reality Show.mp3      -->    
11 Killing With Kindness.mp3         -->    
None

You can try this:你可以试试这个:

a = ['01-06 How Does It Feel.mp3', '01-08 Never Walk Away.mp3']

b = [
    '01 Show Me a Sign.mp3',
     "02 Don't Let Go.mp3",
     '03 All Messed Up.mp3',
     '04 Promise Keeper.mp3',
     '05 Goodbye to You.mp3',
     '06 How Does It Feel.mp3',
     '07 Had Enough of You.mp3',
     '08 Never Walk Away.mp3',
     '09 Nothing Left at All.mp3',
     '10 Reality Show.mp3',
     '11 Killing With Kindness.mp3',
]


length = max(len(s) for s in b)

for i in b:
    for j in a:
        if i in j[3:]:
            print(f"{i:{length}} -> {j}")
    print(f"{i:{length}} ->")

Output: Output:

01 Show Me a Sign.mp3        ->
02 Don't Let Go.mp3          ->
03 All Messed Up.mp3         ->
04 Promise Keeper.mp3        ->
05 Goodbye to You.mp3        ->
06 How Does It Feel.mp3      -> 01-06 How Does It Feel.mp3
06 How Does It Feel.mp3      ->
07 Had Enough of You.mp3     ->
08 Never Walk Away.mp3       -> 01-08 Never Walk Away.mp3
08 Never Walk Away.mp3       ->
09 Nothing Left at All.mp3   ->
10 Reality Show.mp3          ->
11 Killing With Kindness.mp3 ->

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

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