简体   繁体   English

输出格式化Python 3.3

[英]Output formatting Python 3.3

So this is probably a very basic question about output formatting in python using '.format' and since I'm a beginner, I can't figure this out for the life of me. 所以这可能是一个非常基本的问题,关于使用'.format'的python中的输出格式化,因为我是初学者,我无法想象我的生活。 I've tried to be as detailed as possible, just to make sure that there's no confusion. 我试图尽可能详细,只是为了确保没有混乱。

Let me give you an example so that you can better understand my dilemma. 让我举个例子,以便您更好地理解我的困境。 Consider the following program 考虑以下程序

list = (['wer', 'werwe', 'werwe' ,'wer we'])   # list[0], list[1], list[2], list[3]
list.append(['vbcv', 'cvnc', 'bnfhn', 'mjyh']) # list[4]
list.append(['yth', 'rnhn', 'mjyu', 'mujym'])  # list[5]
list.append(['cxzz', 'bncz', 'nhrt', 'qweq'])  # list[6]

first = 'bill'
last = 'gates'

print ('{:10} {:10} {:10} {:10}'.format(first,last,list[5], list[6]))     

Understandably that would give the output: 可以理解的是,这将产生输出:

bill       gates      ['yth', 'rnhn', 'mjyu', 'mujym'] ['cxzz', 'bncz', 'nhrt', 'qweq']

So here's my real question. 所以这是我真正的问题。 I was doing this practice problem from the book and I don't understand the answer. 我从书中做了这个练习题,我不明白答案。 The program below will give you a good idea of what kind of output we are going for: 下面的程序将让您了解我们的输出类型:

students = []
students.append(['DeMoines', 'Jim', 'Sophomore', 3.45]) #students[0]
students.append(['Pierre', 'Sophie', 'Sophomore', 4.0]) #students[1]
students.append(['Columbus', 'Maria', 'Senior', 2.5])   #students[2]
students.append(['Phoenix', 'River', 'Junior', 2.45])   #students[3]
students.append(['Olympis', 'Edgar',  'Junior', 3.99])  #students[4]
students.append(['van','john', 'junior', 3.56])         #students[5]
def Grades(students):
    print ('Last       First      Standing        GPA')
    for students in students:
        print('{0:10} {1:10} {2:10} {3:8.2f}'.format(students[0],students[1],students[2],students[3]))

The output we're trying to get is a kind of a table that gives all the stats for all the students - 我们想要获得的输出是一种表格,可以为所有学生提供所有统计数据 -

Last       First      Standing        GPA
DeMoines   Jim        Sophomore      3.45
Pierre     Sophie     Sophomore      4.00
Columbus   Maria      Senior         2.50
Phoenix    River      Junior         2.45
Olympis    Edgar      Junior         3.99
van        john       junior         3.56

So here's what I don't understand. 所以这就是我不明白的地方。 We are working with basically the same thing in the two examples ie a list inside a list. 我们在两个示例中基本相同,即列表中的列表。 For my first example, the print statement was: 对于我的第一个例子,print语句是:

print('{:10} {:10} {:10} {:10}'.format(first, last, list[5], list[6]))  

where list[5] and list[6] are lists themselves and they are printed in entirety, as you can see from the output. list[5]list[6]本身就是列表,它们是完整打印的,正如您从输出中看到的那样。 But that doesn't happen in the book problem . 但这不会发生在书籍问题上 There, the print statement says 在那里,印刷声明说

print('{0:10} {1:10} {2:10} {3:8.2f}'.format(students[0], students[1], students[2], students[3])) 

As you can see from the table output, here students[0] refers only to 'DeMoines' . 从表格输出中可以看出, students[0]仅指'DeMoines' But if you just run the statement students[0] in the Python interpreter, it gives the whole sub list, as it should. 但是如果你只是在Python解释器中运行students[0]语句,它就会给出整个子列表。

['DeMoines', 'Jim', 'Sophomore', 3.45]

So, basically, I've got two questions, why does students[0] have two different meanings and why does students[0] not print the whole list like we did with list[5] and list[6] ? 所以,基本上,我有两个问题,为什么students[0]有两种不同的含义,为什么students[0]不像list[5]list[6]那样打印整个列表?

Look at the for loop : 看看for循环

for students in students:
#   ^^^^^^^^

So, students (inside loop) does not actually refers to list of list . 因此, students (内部循环)实际上并不是指列表 And students[0] refers to first element from element from list of lists , as expected. 并且students[0]按照预期从列表列表中的 元素引用第一个元素

I suggest replace students from function argument, say, with all_students or something like that. 我建议用all_students或类似的东西替换函数参数中的students

Try renaming the variable list into something that's not a reserved word or built-in function or type. 尝试将变量list重命名为不是保留字或内置函数或类型的内容。

What's confusing to beginners - and it happens to everyone sooner or later - is what happens if you redefine or use in unintended ways a reserved word or a builtin. 让初学者感到困惑的是 - 如果你以非预期的方式重新定义或使用保留词或内置词,那么它迟早会发生在每个人身上。

If you do 如果你这样做

list = [1, 2, 3, 4]

you re-bind the name list to no longer point to the builtin list data type but to the actual list [1, 2, 3, 4] in the current scope. 您重新绑定名称list不再指向内置list数据类型,而是指向当前作用域中的实际列表[1, 2, 3, 4] That is almost always not what you intend to do. 这几乎总是不是你打算做的。 Using a variable dir is a similar pitfall. 使用变量dir是一个类似的陷阱。

Also do not use additional parantheses () around the square brackets of the list assignment. 也不要在列表赋值的方括号周围使用额外的parantheses () Something like words = ['wer', 'werwe', 'werwe' ,'wer we'] suffices. words = ['wer', 'werwe', 'werwe' ,'wer we']就足够了。

Generally consider which names you choose for a variable. 通常考虑为变量选择的名称。 students is descriptive, helpful commentary, list is not. students是描述性的,有用的评论, list不是。 Also if list currently holds a list, your algorithm might be changed later on with the variable holding a set or any other container type. 此外,如果list当前包含列表,则稍后可能会使用包含set或任何其他容器类型的变量更改您的算法。 Then a type-based variable name will be even misleading. 那么基于类型的变量名称甚至会产生误导。

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

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