【问题标题】:Adding list to a list in python将列表添加到python中的列表
【发布时间】:2020-09-10 19:57:40
【问题描述】:

我的代码通过向其附加字符串 (token) 来生成列表 (target = [ ])。

现在我有另一个列表说A = [['A', 'B'], ['C', 'D']],我想将它添加到target 列表中,而不触及原始列表。把原版加进去就行了,能知道怎么做吗?谢谢

    def read_samples_by_string(self, path):
        for tokens in self.read_tokens(path):

            target =  []
            print(type(target))  # this is <type 'list'>
            
            for token in tokens:
                
                target.append(token)
                print(type(token))   # this is <type 'str'>               
            yield target
            print(target)

Input list : 
A = [['A', 'B'], ['C', 'D']]

output :
['O','X', 'Y', 'Z']
['L', 'M'] 
['J', 'K', 'W'] 

Expected output
['O','X', 'Y', 'Z']
['L', 'M'] 
['J', 'K', 'W'] 
['A', 'B']
['C', 'D']

【问题讨论】:

  • 你是如何导出查询的?

标签: python pandas list dataframe numpy


【解决方案1】:
final_list = []
list1 = [['A', 'B'], ['C', 'D']]
list2 = [['O','X', 'Y', 'Z'],['L', 'M'],['J', 'K', 'W']]

for l2 in list2:
    final_list.append(l2)

for l1 in list1:
    final_list.append(l1)

for l in final_list:
    print(l)

【讨论】:

    猜你喜欢
    • 2016-01-09
    • 1970-01-01
    • 1970-01-01
    • 2012-02-13
    • 2014-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-21
    相关资源
    最近更新 更多