简体   繁体   English

Python:将多个参数附加到元组

[英]Python : append multiple arguments to the tuple

I'd like to append multiple arguments to the tuple, but I don't know how 我想将多个参数附加到元组,但是我不知道如何

Here's my dict() : 这是我的dict()

OBJECTS = { item1 : 'None', item2 : 'None' }

CODE: 码:

def a(file):
    fp = open(file, 'Ur')
    data_list = []
    for line in fp:
        data_list.append(tuple(line.strip().split('|')),OBJECTS)
    fp.close()
    return data_list

And Python return Error: 和Python返回错误:

TypeError: append() takes exactly one argument (2 given) TypeError:append()仅接受一个参数(给定2个)

You can use the + operator to concatenate collections: 您可以使用+运算符来连接集合:

[1, 2, 3] + [4, 5, 6]

(1, 2, 3) + (4, 5, 6)

As the error states, you can only append one item to the list at a time. 由于错误状态,您一次只能将一个项目追加到列表中。

So, you want to call append twice: 因此,您想两次调用append

data_list.append(tuple(line.strip().split('|')))
data_list.append(OBJECTS)

However, I'm not sure if you want to actually append OBJECTS to the array for each line, considering that OBJECTS is not dependent on the line. 但是,考虑到OBJECTS并不依赖于该行,因此我不确定是否要将OBJECTS实际附加到每一行的数组中。

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

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