Python 中两个列表中的元素的串联

concatenation of the elements in two list in Python

例如, “拯救我们的灵魂”消息将被加密为:“Sv u oliaeorSu” 然后我想将“Sv u oliaeorSu”解密为原来的。 我已经执行了加密,但解密看起来很困难。 最后,我想将两个列表的项目连接成一个新列表。

例如lst1=[a,c,e,g] lst2=[b,d,f]

我要的名单是lst3=[a,b,c,d,e,f,g]

str1=input("Enter the String ??")
mystr=""
list1=[]
list2=[]
for i in range(len(str1)):
    if i%2==0:
        list1.append(str1[i])
    else:
        list2.append(str1[i])
list1.append("i")
encrypted="".join(map(str,list1+list2))
print("Encryption :",encrypted)



# (b) Decryption
list3=[];list4=[]
dec1=[];dec2=[]
length=len(list1+list2)
for i in range(length):
    list3.append(encrypted[i])
else:
    list4.append(encrypted[i])
if "i" in (list3):
    list3.pop(list3.index("i"))
print(*list3)
len1=len(list3)
if len1%2==0:
    for i in range(len1//2):
        dec1.append(list3[i])
        dec2.append(list3[i+len1//2])
    print(dec1)
    print(dec2)
    decX=[]
    i=0
    while(i<len(dec1)):
        print(dec1[i],end="")
        print(dec2[i],end="")
        i+=1
else:
    for i in range(len1//2+1):
        dec1.append(list3[i])
        dec2.append(list3[i+len1//2])
    if len1%2!=0:
        dec2.pop(0)
    print(dec1)
    print(dec2)
    i=0
    while(i<len(dec1)):
            print(dec1[i],end="")
            if len(dec2) != len(dec1):
                i+=1
                print(dec2[i-1],end="")
                i+=1
            i-=1

输出

Enter the String ??1234567
Traceback (most recent call last):
  File "C:/Users/SHUBHAM/PycharmProjects/Hello World/Basic Programs/Ass 4.py", line 460, in <module>
Encryption : 1357i246
1 3 5 7 2 4 6
['1', '3', '5', '7']
['2', '4', '6']
1234567    print(dec2[i-1],end="")
IndexError: list index out of range

Process finished with exit code 1

您可以使用 zip() 方法。然后使用列表理解来展平列表。

lst1 = ['a','c','e','g']
lst2 = ['b','d','f']

def merge(lst1, lst2):
    lst3 = list(zip(lst1, lst2))
    lst3 = [l for y in lst3 for l in y]

    extra = None
    if len(lst1) > len(lst2):
        lst3.append(lst1[-1])
    elif len(lst1) < len(lst2):
        lst3.append(lst1[-2])
    return lst3

merge_list = merge(lst1,lst2)
print(merge_list)

您可以使用 itertools.zip_longest:

from itertools import zip_longest

def encrypt(s):
    odd = s[::2]
    even = s[1::2]
    return odd + even

def decrypt(s):
    odd = s[:(len(s)+1)//2]
    even = s[(len(s)+1)//2:]
    lst = [x + y for x, y in zip_longest(odd, even, fillvalue='')]
    return(''.join(lst))

s = "Save our Soul"
print(encrypt(s)) # Sv u olaeorSu
print(decrypt(encrypt(s))) # Save our Soul

如果您出于某种原因不想使用 zip_longest,那么您可以使用 zip,但是当 [=15] 时需要注意 odd 的最后一个元素=] 长于 even:

def decrypt(s):
    odd = s[:(len(s)+1)//2]
    even = s[(len(s)+1)//2:]
    lst = [x + y for x, y in zip(odd, even)]
    if len(odd) > len(even):
        lst.append(odd[-1])
    return(''.join(lst))

您可以执行以下操作以获得 final_list

def merge_list(lst1, lst2):
    lst3 = []
    while lst1 and lst2:
        lst3.append(lst1.pop(0))
        lst3.append(lst2.pop(0))
    if lst1:
        lst3.extend(lst1)
    if lst2:
        lst3.extend(lst2)
    return lst3
    

lst1 = ['a', 'c', 'e', 'g']
lst2 = ['b', 'd', 'f']

print(merge_list(lst1, lst2))