Python 如何从列表生成表

Python 如何从列表生成表,python,list,printing,Python,List,Printing,我有一个包含3个值的子列表的列表,我需要打印一个如下所示的列表: 我还需要将第三列的值相互比较,以判断它们在下降时是增加还是减少 bb = 3.9 lowest = 0.4 #appending all the information to a list allinfo= [] while bb>=lowest: everything = angleWithPost(bb,cc,dd,ee) allinfo.append(everything) bb-=0.1

我有一个包含3个值的子列表的列表,我需要打印一个如下所示的列表:

我还需要将第三列的值相互比较,以判断它们在下降时是增加还是减少

bb = 3.9
lowest = 0.4
#appending all the information to a list
allinfo= []
while bb>=lowest:
    everything = angleWithPost(bb,cc,dd,ee)
    allinfo.append(everything)
    bb-=0.1
我认为确定第三列值是增加还是减少的总体思路是:

#Checking whether or not Fnet are increasing or decreasing
ii=0
while ii<=(10*(bb-lowest)):
    if allinfo[ii][2]>allinfo[ii+1][2]:
        abc = "decreasing"
    elif allinfo[ii][2]<allinfo[ii+1][2]:
        abc = "increasing"
    ii+=1
#检查Fnet是否在增加或减少
ii=0
而iAlinfo[ii+1][2]:
abc=“递减”

elif allinfo[ii][2]基本上,您想在内部列表中添加第四列并打印结果吗

#print headers of table here, use .format for consistent padding

previous = 0 
for l in outer_list:
    if l[2] > previous:
        l.append('increasing')
    elif l[2] < previous:
        l.append('decreasing')
    previous = l[2]

    #print row here use .format for consistent padding
#在此处打印表格标题,使用.format进行一致填充
上一个=0
对于外部列表中的l:
如果l[2]>上一个:
l、 追加('增加')
elif l[2]<以前的:
l、 追加('递减')
上一个=l[2]
#在此处打印行使用.格式进行一致的填充
元组列表的更新,:

随机导入
外部_列表=[(i,i,random.randint(0,10),)表示范围(0,10)内的i]
上一个=0
allinfo=[]
对于外部列表中的l:
如果l[2]>上一个:
附加(l+(‘增加’,))
elif l[2]<以前的:
allinfo.append(l+(‘递减’,))
上一个=l[2]
#在此处打印行使用.格式进行一致的填充
打印(allinfo)

这无疑是可以优化的,您可以减少对数据的迭代次数。

回顾一下使用
map
和迭代器技巧的Python2实现

from itertools import izip_longest, islice
from pprint import pprint


data = [
    [1, 2, 3],
    [1, 2, 4],
    [1, 2, 3],
    [1, 2, 5],
]


class AddDirection(object):
    def __init__(self):
        # This default is used if the series begins with equal values or has a
        # single element.
        self.increasing = True

    def __call__(self, pair):
        crow, nrow = pair
        if nrow is None or crow[-1] == nrow[-1]:
            # This is the last row or the direction didn't change. Just return
            # the direction we previouly had.
            inc = self.increasing
        elif crow[-1] > nrow[-1]:
            inc = False
        else:
            # Here crow[-1] < nrow[-1].
            inc = True
        self.increasing = inc

        return crow + ["Increasing" if inc else "Decreasing"]

result = map(AddDirection(), izip_longest(data, islice(data, 1, None)))

pprint(result)
每当您想要转换列表的内容(在本例中为行列表)时,map是一个开始思考的好地方

当算法需要来自列表中多个位置的数据时,偏移列表并压缩所需的值也是一种强大的技术。使用生成器使列表不必被复制,这使它在实际代码中可行

最后,当您需要在调用之间保持状态(在本例中为方向)时,使用对象是最佳选择


对不起,如果代码太简洁了

为什么不直接使用模块列表?它非常适合二维阵列。我不能使用我需要安装的东西。所有这些都必须预先打包到我的班级使用的程序中。那个节目是CanopyOk。。。这有点难看,但它需要一个二维数组作为输入。看一看是的。。。谢谢你的帮助,但我不知道如何使用它,也不认为我会被允许使用它。对不起,我认为这可能会解决问题,但出于某种原因,我的代码说我不能附加元组,但我知道我的“allinfo[]”是一个列表。我应该指出,它是指带有l.append('discreating')的行,因为某种原因它仍然不起作用。我不明白为什么它是一个元组。这应该是一个列表,你能在问题中用post发布angleWithPost的来源吗?谢谢你的帮助,但不幸的是,这对于我正在学习的课程来说太超前了。我必须将我的课程保持在所教内容的范围内。
#print headers of table here, use .format for consistent padding

previous = 0 
for l in outer_list:
    if l[2] > previous:
        l.append('increasing')
    elif l[2] < previous:
        l.append('decreasing')
    previous = l[2]

    #print row here use .format for consistent padding
import random
outer_list = [ (i, i, random.randint(0,10),)for i in range(0,10)]

previous = 0
allinfo = []
for l in outer_list:
    if l[2] > previous:
        allinfo.append(l +('increasing',))
    elif l[2] < previous:
        allinfo.append(l +('decreasing',))
    previous = l[2]

   #print row here use .format for consistent padding

print(allinfo)
from itertools import izip_longest, islice
from pprint import pprint


data = [
    [1, 2, 3],
    [1, 2, 4],
    [1, 2, 3],
    [1, 2, 5],
]


class AddDirection(object):
    def __init__(self):
        # This default is used if the series begins with equal values or has a
        # single element.
        self.increasing = True

    def __call__(self, pair):
        crow, nrow = pair
        if nrow is None or crow[-1] == nrow[-1]:
            # This is the last row or the direction didn't change. Just return
            # the direction we previouly had.
            inc = self.increasing
        elif crow[-1] > nrow[-1]:
            inc = False
        else:
            # Here crow[-1] < nrow[-1].
            inc = True
        self.increasing = inc

        return crow + ["Increasing" if inc else "Decreasing"]

result = map(AddDirection(), izip_longest(data, islice(data, 1, None)))

pprint(result)
pts/1$ python2 a.py 
[[1, 2, 3, 'Increasing'],
 [1, 2, 4, 'Decreasing'],
 [1, 2, 3, 'Increasing'],
 [1, 2, 5, 'Increasing']]