简体   繁体   English

你如何区分numpy数组和Python的内置对象

[英]how do you distinguish numpy arrays from Python's built-in objects

PEP8 has naming conventions for eg functions (lowercase), classes (CamelCase) and constants (uppercase). PEP8具有例如函数(小写),类(CamelCase)和常量(大写)的命名约定。

It seems to me that distinguishing between numpy arrays and built-ins such as lists is probably more important as the same operators such as "+" actually mean something totally different. 在我看来,区分numpy数组和内置命令如列表可能更重要,因为像“+”这样的运算符实际上意味着完全不同的东西。

Does anyone have any naming conventions to help with this? 有没有人有任何命名约定来帮助解决这个问题?

您可以为numpy数组使用前缀np_ ,从而将它们与其他变量区分开来。

numpy arrays and lists should occupy similar syntactic roles in your code and as such I wouldn't try to distinguish between them by naming conventions. numpy数组和列表应该在代码中占用类似的语法角色,因此我不会尝试通过命名约定来区分它们。 Since everything in python is an object the usual naming conventions are there not to help distinguish type so much as usage. 由于python中的所有内容都是一个对象,因此通常的命名约定无助于区分类型和使用。 Data, whether represented in a list or a numpy.ndarray has the same usage. 无论是在列表中还是以numpy.ndarray表示的数据都具有相同的用法。

I agree that it's awkward that eg. 我同意,例如,这很尴尬。 + means different things for lists and arrays. +表示列表和数组的不同之处。 I implicitly deal with this by never putting anything like numerical data in a list but rather always in an array. 我通过永远不会将数字数据放在列表中而是总是放在数组中来隐式处理这个问题。 That way I know if I want to concatenate blocks of data I should be using numpy.hstack. 这样我知道我是否想要连接数据块我应该使用numpy.hstack。 That said, there are definitely cases where I want to build up a list through concatenation and turn it into a numpy array when I'm done. 也就是说,有些情况下我想通过连接建立一个列表,并在我完成后将其转换为一个numpy数组。 In those cases the code block is usually short enough that it's clear what's going on. 在这些情况下,代码块通常足够短,以便清楚发生了什么。 Some comments in the code never hurt. 代码中的一些注释从未受到伤害。

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

相关问题 你如何研究python的内置方法的实现? - How do you investigate python's implementation of built-in methods? 是否有内置的 numpy 来执行此操作? - Is there a numpy built-in to do this? 如何通过使用内置函数来节省计算时间,并避免在python中为大型numpy数组循环for循环? - How to save computational time by using built-in functions and avoid looping over for loops for large numpy arrays in python? 如何从 numpy 数组列表中“删除”一个 numpy 数组? - How do you 'remove' a numpy array from a list of numpy arrays? 如何在 python 和 numpy arrays 中进行 SIMD 处理? - How do you do SIMD processing in python with numpy arrays? 您如何查看内置变量以检查您的答案是否正确? Python - How do you see built-in variables to check if your answer is correct? Python 如何不使用内置函数或切片在python中查找字符串的子字符串 - How do you find a substring of a string in python using no built-in function or slicing 我如何在python中实现内置对象和创建类之间的操作? - how do i implement a operation between built-in objects and created classes the other way around in python? 如何在python中快速求和大型numpy数组? - How do you quickly sum large numpy arrays in python? 从内置类型派生时如何设置“the”值? - how do you set "the" value when deriving from built-in type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM