Artificial creating of [Lists], is it possible? the best way...

Grant Edwards grant.b.edwards at gmail.com
Fri Nov 17 11:44:52 EST 2017


On 2017年11月17日, eth0 <ethernet.zero at gmail.com> wrote:
> On 2017年11月17日 00:04:16 +0000 in comp.lang.python, MRAB said:
>> On 2017年11月16日 18:47, jakub.rajcok at gmail.com wrote:
>> > Hello, im working on school project, its deck game Sorry!
>> > I need to create specific lists:
>> > My idea is about to using for
>> > For i in range (n):
>> > i=[]
>> > I know, that there is no possibility to make it from number, but
>> > i havent idea, how to reach my wants Li/L"i"/L(i), how to make
>> > possible for lists?
>> > 
>> > 
>> If you want multiple lists, make a list of lists:
>>>> my_lists = []
>>>> for i in range(n):
>> my_lists.append([])
>>>> Then you can say my_lists[0], my_lists[1], etc.
>> It'd be even shorter using a list comprehension:
>> my_lists = [[] for i in range(n)]
>> And _even shorter still_ just using the multiply operator:
>> my_lists = [[]] * n

 $ python
 Python 2.7.14 (default, Nov 2 2017, 11:45:51) 
 [GCC 5.4.0] on linux2
 Type "help", "copyright", "credits" or "license" for more information.
 >>> x = [[] for i in 1,2,3,4]
 >>> x
 [[], [], [], []]
 >>> y = [[]] * 4
 >>> y
 [[], [], [], []]
 >>> x[0].append(1)
 >>> y[0].append(1)
 >>> x
 [[1], [], [], []]
 >>> y
 [[1], [1], [1], [1]]
-- 
Grant Edwards grant.b.edwards Yow! Do I have a lifestyle
 at yet?
 gmail.com 


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /