Python 将数组附加到数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40336601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:24:16  来源:igfitidea点击:

Python appending array to an array

pythonarrays

提问by shubhamj

I am currently working on DES implementation.In one part of the code I have to append array to an array.Below is my code:

我目前正在研究 DES 实现。在代码的一部分中,我必须将数组附加到数组中。下面是我的代码:

C0=[1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1]

def Fiestel():
    C=[]
    C.append(C0)
    temp=[0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1]
    C.append(temp)
    print(C)
Fiestel()

How do I append an array to an existing array.I even tried declaring C as 2d array.Thankx in advance for helping.

如何将数组附加到现有数组。我什至尝试将 C 声明为 2d 数组。提前感谢您的帮助。

Each element is an array itself.

每个元素本身就是一个数组。

enter image description here

在此处输入图片说明

回答by stackoverflowuser2010

You can append the elements of one list to another with the "+" operator.

您可以使用“+”运算符将一个列表的元素附加到另一个列表中。

a = [1, 2, 3]
b = [10, 20]

a = a + b
print a
# [1, 2, 3, 10, 20]

If you want to append the lists and keep them as lists, then try:

如果要附加列表并将它们保留为列表,请尝试:

result = []
result.append(a)
result.append(b)
print result
# [[1, 2, 3], [10, 20]]

回答by Issac Saji

C0=[1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1]

def Fiestel():
    C=C.append(C0)
    temp=[0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1]
    C0=C0.append(temp)
    return C

C0=Fiestel()
print (C0)

Try this, I think this is what you are looking for.

试试这个,我想这就是你要找的。