新手勿喷
for i in open (v):
_temp = i.split('-')
self._i= gen.gen(_temp[0], _temp[1])
self._i 中是多个列表[] [] [] 怎样合并成一个
cc = []
for i in open (v):
_temp = i.split('-')
self= gen.gen(_temp[0], _temp[1])
for bbc in self:
cc.append(i)
这样解决的 !!!
怎样把结果赋值给 self._i
self._i = cc
print 出来是空白
If you mean to merge multiple lists into one, then it is best to use
itertools.chain
to concatenate. The following is a simple example:For your case:
The following digression.
@松林's method is feasible, and the performance will not be bad. In python, the behaviors of amplification (enhanced) operation and general operation are not necessarily exactly the same. Here we use
+
to discuss.Let’s look at an example:
From this example, we can find that
lst1 + lst2
會產生一個新的 list,但是lst1 += lst2
will not, because for the amplification operation, Python most will follow the following rules:The immutable type will generate a new object after operation, and let the variables refer to the object
Variable types will use in-place operations to expand or update the object that the variable originally refers to
In other words
lst1 += lst2
等價於lst1.extend(lst2)
It depends on whether the type is implemented
__iadd__
(或__imul__
...) 而不是只有實作__add__
(或__mul__
...)If there is no implementation
__iXXX__
的型態,Python 會改呼叫__XXX__
代替,這一定會運算出新的 object,但是__iXXX__
, the original object will be updated in placeIn other words, most of:
Immutable types will not be implemented
__iXXX__
, because it makes no sense to update an immutable objectVariable types will be implemented
__iXXX__
Come and update on the spotWhy do I keep emphasizing the majority?
Because it is optimized in CPython
str
的擴增運算,str += other
and it is so commonly used. When concatenating, Python will not copy the string every timeQuestions I answered: Python-QA
Use the extend function, such as:
Using addition will be more intuitive, but the performance will be worse
Is this more Pythonic?