简体   繁体   English

如何在列表中打印字符串及其索引在同一行?

[英]How to print a string in a list and its index on the same line?

This probably has a very simple solution, but I'm just a beginner. 这可能有一个非常简单的解决方案,但我只是一个初学者。

What I have is: 我有的是:

def content(L):  
    for i in range(len(L)):  
        print (i), (L[i])

Right now it only prints the index and not the string. 现在它只打印索引而不是字符串。 I'm not entirely sure what the issue is, but when I switch the ordering in the last line so that (L[i]) comes first then it prints the string but not the index. 我不完全确定问题是什么,但是当我在最后一行中切换排序以便(L [i])首先出现时,它会打印字符串而不是索引。 I want to print both on the same line. 我想在同一行上打印。

In Python 3.x, print is not a statement as in Python 2.x, but rather a function. 在Python 3.x中, print不是Python 2.x中的语句,而是一个函数。 You can use it like this: 你可以像这样使用它:

print(i, L[i])

Additionally, enumerate is great for what you're trying to do: 此外, enumerate非常适合您要做的事情:

for i, c in enumerate(L):
    print(i, c)

Note that you can use print as a Python 3.x-style function in Python 2 if you import it from the __future__ package (for Python 2.6 and up): 请注意,如果从__future__包中导入,则可以在Python 2中使用print作为Python 3.x样式的函数(对于Python 2.6及更高版本):

from __future__ import print_function

In Python 3.x, print is a function. 在Python 3.x中, print是一个函数。 So, you have to pass both the values as parameters like this 因此,您必须将这两个值作为参数传递

print (i, L[i])

When you say 当你说

print (i), (L[i])

Python 3.x, accepts i as the parameter to print and considers (L[i]) as a separate expression and evaluates it, but that doesn't get passed to the print function. Python 3.x,接受i作为print参数并将(L[i])视为单独的表达式并对其进行求值,但这不会传递给print函数。 That is why always the first element is getting printed. 这就是为什么总是要打印第一个元素。

Apart from that, your entire statement gets evaluated to the following tuple 除此之外,您的整个语句将被评估为以下元组

(None, L[i])

To understand that, try something like this, in the Python REPL 要理解这一点,请在Python REPL中尝试这样的事情

>>> print (1), (2)
1
(None, 2)

When the inner print with 1 getting executed, it will return None and the next element is 2 . 当内部print 1执行时,它将返回None ,下一个元素是2 In Python, when you write two expressions separated by comma, in parenthesis, they will become a tuple. 在Python中,当你用逗号分隔两个用逗号分隔的表达式时,它们将成为一个元组。

You can confirm the same with Python's generated bytecodes, like this 您可以使用Python生成的字节码来确认相同的内容,如下所示

from dis import dis
code1 = compile("print (1), (2)", "<string>", "exec")
dis(code1)

Output 产量

  1           0 LOAD_NAME                0 (print) 
              3 LOAD_CONST               0 (1) 
              6 CALL_FUNCTION            1 (1 positional, 0 keyword pair) 
              9 LOAD_CONST               1 (2) 
             12 BUILD_TUPLE              2 
             15 POP_TOP              
             16 LOAD_CONST               2 (None) 
             19 RETURN_VALUE         

BUILD_TUPLE corresponds to the tuple construction and as we don't use it, it gets discarded immediately. BUILD_TUPLE对应于元组结构,因为我们不使用它,它会立即被丢弃。

def content(L):  
    for ind, val in enumerate(L):  
        print (ind, val)

As others said, print is now a function, so what python is doing is interpreting the line: print(i), (L[i]) as a tuple. 正如其他人所说, print现在是一个函数,所以python正在做的是将行: print(i), (L[i])为元组。 Do print(i, L[i]) instead. 请改为print(i, L[i])

You'll also want to use enumerate: 您还需要使用枚举:

for i,line in enumerate(L):
    print(i, line)

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

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