简体   繁体   English

如何在 python 中将两个列表组合在一起

[英]how can I combine two list together in python

Hi I'm trying to combine both list together in python therefore the output will be ['apple', 'cherry'].嗨,我正在尝试在 python 中将两个列表组合在一起,因此 output 将是 ['apple', 'cherry']。 I only get this [] as an answer but I'm trying to get the output of 'apple and cherry added together.我只得到这个 [] 作为答案,但我试图将“苹果和樱桃”的 output 加在一起。 Thanks in Advance.提前致谢。

FruitArr = []
VeggieArr = []


def one():
list1 = ["apple", "beans", "cabbage", "broccoli"]
if 'apple' in list1:
    FruitArr.append('apple')
print(FruitArr)


def two():
list2 = ["rice", "carrot", "garlic", "cherry"]
if 'cherry' in list2:
    VeggieArr.append('cherry')
print(VeggieArr)

listcombined = FruitArr + VeggieArr

print(listcombined)

# OUTPUT ['apple','cherry']
for i in FruitArr:
    VeggieArr.append(i)

Or use extend() which would look like或者使用看起来像的extend()

FruitArr.extend(VeggieArr)

Also make sure the functions are being called from somewhere in order to reach the code.还要确保从某处调用函数以访问代码。

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

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