简体   繁体   English

对 Python 中的数字序列 (1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1.....) 使用列表理解

[英]Using List Comprehension for a number sequence (1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1 .....) in Python

I'm doing exercise questions from A Practical Introduction to Python Programming by Brian Heinold (pg 83) and there was a simpler question:我正在做 Brian Heinold(第 83 页)的 A Practical Introduction to Python Programming 中的练习题,还有一个更简单的问题:

  1. Using a for loop, create the list below, which consists of ones separated by increasingly many zeroes.使用 for 循环创建下面的列表,其中由越来越多的零分隔的 1 组成。 The last two ones in the list should be separated by ten zeroes.列表中的最后两个应该用十个零分隔。 [1,1,0,1,0,0,1,0,0,0,1,0,0,0,0,1,....] [1,1,0,1,0,0,1,0,0,0,1,0,0,0,0,1,....]
# Question 11
L = [1]
for i in range(11):
  if i == 0:
    L.append(1)
  else:
    for j in range(i):
      L.append(0)
    L.append(1)
print(L)
  1. Use a list comprehension to create the list below, which consists of ones separated by increasingly many zeroes.使用列表推导来创建下面的列表,其中由越来越多的零分隔。 The last two ones in the list should be separated by ten zeroes.列表中的最后两个应该用十个零分隔。 [1,1,0,1,0,0,1,0,0,0,1,0,0,0,0,1,....] [1,1,0,1,0,0,1,0,0,0,1,0,0,0,0,1,....]

I'm having difficulty with converting it into one line using list comprehension.我很难使用列表理解将它转换成一行。 Best I could do was我能做的就是

# Question 15
L = [[1, [0]*i] for i in range(11)]
print(L)
L = [j for row in L for j in row]
print(L)

Which would print:哪个会打印:

[[1, []], [1, [0]], [1, [0, 0]], [1, [0, 0, 0]], [1, [0, 0, 0, 0]], [1, [0, 0, 0, 0, 0]], [1, [0, 0, 0, 0, 0, 0]], [1, [0, 0, 0, 0, 0, 0, 0]], [1, [0, 0, 0, 0, 0, 0, 0, 0]], [1, [0, 0, 0, 0, 0, 0, 0, 0, 0]], [1, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]]
[1, [], 1, [0], 1, [0, 0], 1, [0, 0, 0], 1, [0, 0, 0, 0], 1, [0, 0, 0, 0, 0], 1, [0, 0, 0, 0, 0, 0], 1, [0, 0, 0, 0, 0, 0, 0], 1, [0, 0, 0, 0, 0, 0, 0, 0], 1, [0, 0, 0, 0, 0, 0, 0, 0, 0], 1, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]

Flattening the list any further would result in a TypeError: 'int' object is not iterable and I would have to add another 1 at the end of the list using a seperate command which I guess would be fine.进一步扁平化列表会导致 TypeError: 'int' object is not iterable 并且我必须使用单独的命令在列表末尾添加另一个 1 我想这没问题。 To add the code for flattening the list kinda goes over my head.添加用于展平列表的代码有点让我头疼。

This is a second attempt using if/else statements, that gives the produces the same output and gives the same TypeError for when I try to flatten it completely.这是使用 if/else 语句的第二次尝试,它给出相同的 output 并在我尝试完全展平它时给出相同的 TypeError。

L = [1 if i%2 == 0 else [0]*(i//2) for i in range(22)]
L = [j for row in L for j in row]
print(L)

I just want this as output:我只想要这个作为 output:

[1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]

Thanks!谢谢!

You can solve it like this:你可以这样解决:

L = [j for i in range(11) for j in [1, *([0]*i)]]

or或者

L = [j for i in range(11) for j in [1, *(0 for k in range(i))]]

Edit编辑
I missed that it should end with a one.我错过了它应该以一个结束。 Here's a solution that does that:这是一个解决方案:

L = [1, *(j for i in range(11) for j in [*(0 for k in range(i)), 1])]

or with list comprehension instead of generators:或者使用列表理解而不是生成器:

L = [1, *[j for i in range(11) for j in [*[0 for k in range(i)], 1]]]

You could use a nested comprehension that produces an increasing number of values that you convert to 0s and 1s by returning 0s for all but the last of each group:您可以使用嵌套推导来产生越来越多的值,您可以通过为每个组中除最后一个以外的所有值返回 0 来将这些值转换为 0 和 1:

n = 10
r = [1]+[b//z for z in range(1,n+2) for b in range(1,z+1)]
print(r)
[1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]

If you want to avoid the need to add the initial [1] , you could use a similar approach starting z at -1 instead of 1:如果你想避免需要添加初始[1] ,你可以使用类似的方法从 -1 而不是 1 开始 z:

r = [ 1-(b<z) for z in range(-1,n+1) for b in range(z+1 or 1)] 

I know it's not allowed in the problem, but assume we can use math library, there's a really interesting solution that goes like this:我知道这在问题中是不允许的,但假设我们可以使用math库,有一个非常有趣的解决方案是这样的:

from math import sqrt, floor

print([1] + [1 * ((-1+sqrt(1+8*i))/2 > 0 and floor((-1+sqrt(1+8*i))/2) == (-1+sqrt(1+8*i))/2 or (-1-sqrt(1+8*i))/2 > 0 and floor((-1-sqrt(1+8*i))/2) == (-1-sqrt(1+8*i))/2) for i in range(1, 67)])

The key observation is that all ones in the target list has an index that's a triangular number except for the one on index 0(no pun intended).关键观察是目标列表中的所有索引都有一个三角形数字,索引 0 上的索引除外(无双关语意)。 So we can just check if the current index is a triangular number.所以我们可以检查当前索引是否是一个三角数。

The nth triangular number can be expressed as:nth三角数可以表示为:

第 n 个三角数的表达式

Let i be the current index we are checking.让我成为我们正在检查的当前索引。 All we need to do is to solve:我们需要做的就是解决:

在此处输入图像描述

And check if one of the roots is a natural number.并检查其中一个根是否是自然数。

Again, I know this is not a valid answer.同样,我知道这不是一个有效的答案。 I just want to show it as the idea is interesting.我只是想展示它,因为这个想法很有趣。

p=[] p=[]

for i in range(11):对于我在范围内(11):

p.append(1)

for j in range(i):

    p.append(i*0)

print(p)打印(p)

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

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