在Python中的列表理解中打印

时间:2021-09-06 21:42:13

I am getting a syntax error when executing the following code. I want to print within a list comprehension. As you see, I tried a different approach (commented out line) by using print(). But I think this syntax is supported in Python 3 as earlier versions of Python treat print as a statement.

执行以下代码时出现语法错误。我想在列表理解中打印。如您所见,我尝试使用print()尝试不同的方法(注释掉行)。但我认为Python 3支持这种语法,因为早期版本的Python将print视为一种语句。

  1 import sys
  2 import nltk
  3 import csv
  4 from prettytable import PrettyTable
  5 CSV_FILE = sys.argv[1]
  6 # Handle any known abbreviations, # strip off common suffixes, etc.
  7 transforms = [(', Inc.', ''), (', Inc', ''), (', LLC', ''), (', LLP', '')]
  8 csvReader = csv.DictReader(open(CSV_FILE), delimiter=',', quotechar='"')
  9 contacts = [row for row in csvReader]
 10 companies = [c['Company'].strip() for c in contacts if c['Company'].strip() != '']
 11 for i in range(len(companies)):
 12     for transform in transforms:
 13         companies[i] = companies[i].replace(*transform)
 14 #fields=['Company', 'Freq']
 15 #pt = PrettyTable(fields=fields)
 16 #pt.set_field_align('Company', 'l')
 17 fdist = nltk.FreqDist(companies)
 18 #[pt.add_row([company, freq]) for (company, freq) in fdist.items() if freq > 1] 
 19 #[print("["+company+","+freq+"]") for (company, freq) in fdist.items() if freq > 1] 
 20 [print company for (company, freq) in fdist.items() if freq > 1]
 21 #pt.printt()
~                                           

3 个解决方案

#1


11  

No statements can appear in Python expressions. print is one kind of statement in Python 2, and list comprehensions are one kind of expression. Impossible. Neither can you, for example, put a global statement in an index expression.

Python表达式中不会出现任何语句。 print是Python 2中的一种语句,列表推导是一种表达。不可能。例如,您也不能在索引表达式中放置全局语句。

Note that in Python 2, you can put

请注意,在Python 2中,您可以放置

from __future__ import print_function

to have print() treated as a function instead (as it is in Python 3).

将print()视为函数(就像在Python 3中一样)。

#2


8  

It's not a print statement in Python 3, it's a function.

它不是Python 3中的print语句,它是一个函数。

>>> sys.version
'3.4.0a4+ (default:8af2dc11464f, Nov 12 2013, 22:38:21) \n[GCC 4.7.3]'
>>> [print(i) for i in range(4)]
0
1
2
3

and returns:

并返回:

[None, None, None, None]

And as Tim Peters said, no statements can be in comprehensions or generator expressions.

正如蒂姆·彼得斯所说的那样,没有任何陈述可以用于理解或生成器表达。

#3


3  

The other answer is: Don't do this. Use a for loop. There's no need to materialize a list of None's in memory. The print function returns None, and the printing is a mere side-effect from the perspective of functional programming. If you need the printing, use a for-loop, since there's no need to materialize a list in memory. If you need the list of None's, use None, not print(i), since this would make your program IO bound.

另一个答案是:不要这样做。使用for循环。没有必要在内存中实现None的列表。打印功能返回None,从功能编程的角度来看,打印只是一个副作用。如果需要打印,请使用for循环,因为不需要在内存中实现列表。如果你需要None的列表,请使用None,而不是print(i),因为这会使你的程序IO绑定。

If you need both, do them so that you can easily remove the one you don't need when you are done.

如果您需要两者,请执行它们,以便在完成后可以轻松删除不需要的那个。

#1


11  

No statements can appear in Python expressions. print is one kind of statement in Python 2, and list comprehensions are one kind of expression. Impossible. Neither can you, for example, put a global statement in an index expression.

Python表达式中不会出现任何语句。 print是Python 2中的一种语句,列表推导是一种表达。不可能。例如,您也不能在索引表达式中放置全局语句。

Note that in Python 2, you can put

请注意,在Python 2中,您可以放置

from __future__ import print_function

to have print() treated as a function instead (as it is in Python 3).

将print()视为函数(就像在Python 3中一样)。

#2


8  

It's not a print statement in Python 3, it's a function.

它不是Python 3中的print语句,它是一个函数。

>>> sys.version
'3.4.0a4+ (default:8af2dc11464f, Nov 12 2013, 22:38:21) \n[GCC 4.7.3]'
>>> [print(i) for i in range(4)]
0
1
2
3

and returns:

并返回:

[None, None, None, None]

And as Tim Peters said, no statements can be in comprehensions or generator expressions.

正如蒂姆·彼得斯所说的那样,没有任何陈述可以用于理解或生成器表达。

#3


3  

The other answer is: Don't do this. Use a for loop. There's no need to materialize a list of None's in memory. The print function returns None, and the printing is a mere side-effect from the perspective of functional programming. If you need the printing, use a for-loop, since there's no need to materialize a list in memory. If you need the list of None's, use None, not print(i), since this would make your program IO bound.

另一个答案是:不要这样做。使用for循环。没有必要在内存中实现None的列表。打印功能返回None,从功能编程的角度来看,打印只是一个副作用。如果需要打印,请使用for循环,因为不需要在内存中实现列表。如果你需要None的列表,请使用None,而不是print(i),因为这会使你的程序IO绑定。

If you need both, do them so that you can easily remove the one you don't need when you are done.

如果您需要两者,请执行它们,以便在完成后可以轻松删除不需要的那个。