我正在使用Python Interactive Shell(Windows XP下的ActiveState ActivePython 2.6.4).我创建了一个能够实现我想要的功能.但是,我已经清除了屏幕,因此我无法返回查看功能定义.它也是一个多线函数,因此重新显示行的向上箭头具有最小值.反正有没有返回函数的实际代码?dir()显示" code "和"func_code"属性,但我不知道它们是否包含我需要的内容.
我正在处理一个CSV文件并计算第4列的唯一值.到目前为止,我已经用这三种方式编码了.一个使用"if key in dictionary",第二个使用KeyError,第三个使用"DefaultDictionary".例如(其中x [3]是文件中的值,"a"是字典):
第一种方式:
if x[3] in a:
a[x[3]] += 1
else:
a[x[3]] = 1
Run Code Online (Sandbox Code Playgroud)
第二种方式:
try:
b[x[3]] += 1
except KeyError:
b[x[3]] = 1
Run Code Online (Sandbox Code Playgroud)
第三种方式:
from collections import defaultdict
c = defaultdict(int)
c[x[3]] += 1
Run Code Online (Sandbox Code Playgroud)
我的问题是:哪种方式更有效......更干净......更好......等等还是有更好的方法.这两种方式都有效,并给出相同的答案,但我认为我会将蜂巢思维作为一个学习案例.
谢谢 -
我在Windows XP下运行ActiveState的ActivePython 2.6.5.12和PostgreSQL 9.0 Beta 1.
如果我创建一个带有大写首字母的表(即Books),当我运行select语句时,psycopg2会返回"Programming Error:relation"书籍"不存在"错误消息:execute("SELECT * FROM Books").如果我运行,则会返回相同的错误:execute("SELECT * FROM books").但是,如果我将表更改为小写的第一个名称(即书籍),则上述任一语句都可以.
表名是否应该具有小写的名字?这是设置或功能还是错误?我错过了一些明显的东西吗
我有一个 JSON 格式的传入有效负载,我将其中的一些对象输出到 CSV 文件。有效载荷也有一个数组:
"Chargebacks": [
{
"CostCenterCode": "123ABC",
"AllocationPercentage": 100
},
{
"CostCenterCode": "456DEF",
"AllocationPercentage": 100
}
]
Run Code Online (Sandbox Code Playgroud)
我需要 CSV 文件包含:
<other headers from the objects>,Cost Center Code 1, Allocation Percentage 1, Cost Center Code 2, Allocation Percentage 2
<other object values>,123ABC,100,456DEF,100
Run Code Online (Sandbox Code Playgroud)
我的第一次尝试是创建两个变量来保存标题列表和值列表:
%dw 2.0
output application/csv
var x = payload.Item.CatalogAttributes.Chargebacks map (chargeBack, index) ->
{
"header": "Cost Center Code " ++ index+1 ++ ", Allocation Percentage "++ index+1,
"costCenterCode": chargeBack.CostCenterCode ++ "," ++ chargeBack.AllocationPercentage,
}
var foo …Run Code Online (Sandbox Code Playgroud) python ×3
dataweave ×1
dictionary ×1
mule ×1
mulesoft ×1
postgresql ×1
shell ×1
sql ×1
unique-key ×1