为什么在相同长度下,len函数在某些迭代中返回2?

时间:2019-05-11 07:10:06

标签: python

from datetime import date

score_list = [
{ 2:date(2018,10,20), 5:date(2018,11,8), 0:date(2018,12,25),},
{ 3:date(2018,10,20), 1:date(2018,11,8), 4:date(2018,12,25),},
{ 0:date(2018,10,20), 1:date(2018,11,8), 0:date(2018,12,25),},
{ 2:date(2018,10,20), 7:date(2018,11,8), 3:date(2018,12,25),},
{ 0:date(2018,10,20), 0:date(2018,11,8), 6:date(2018,12,25),},
]

total = []
ave_total = []
ite=0

for i in score_list:
    sum = 0
    for key in i:
        sum+= key
    total.append(sum) 

print (total)

for j in total:
    **t = len(score_list[ite])
    ave = j/t
    print (t)**
    print (ite)
    ite += 1
    ave_total.append(ave) 
    print (ave)

print (ave_total)

在尝试使用print调试出了什么问题时,我注意到有时t都返回2,而它们的长度都相同(3)。

1 个答案:

答案 0 :(得分:3)

字典中的键是唯一的,您别无选择。因此,例如:

sudo sqoop import \
--connect 'jdbc:sqlserver://1.1.1.1\test_server;database=Training' \
--username Training_user --password Training_user \
--table BigDataTest -m 1 \
--check-column lastmodified \
--merge-key id \
--incremental lastmodified \
--compression-codec=snappy \
--as-parquetfile \
--target-dir /user/hive/warehouse \
--hive-table bigDataTest \
--last-value '2019-05-06 15:07:49.917'

仅提供 2 键,因为重复. . . 19/05/09 11:00:50 INFO tool.ImportTool: Final destination exists, will run merge job. 19/05/09 11:00:50 ERROR tool.ImportTool: Import failed: java.io.IOException: Could not load jar /tmp/sqoop-root/compile/e913f7c459cf4e1cdb8a8d5802f1dac2/codegen_BigDataTest.jar into JVM. (Could not find class BigDataTest.) at org.apache.sqoop.util.ClassLoaderStack.addJarFile(ClassLoaderStack.java:92) at com.cloudera.sqoop.util.ClassLoaderStack.addJarFile(ClassLoaderStack.java:36) at org.apache.sqoop.tool.ImportTool.loadJars(ImportTool.java:120) at org.apache.sqoop.tool.ImportTool.lastModifiedMerge(ImportTool.java:456) at org.apache.sqoop.tool.ImportTool.importTable(ImportTool.java:522) at org.apache.sqoop.tool.ImportTool.run(ImportTool.java:621) at org.apache.sqoop.Sqoop.run(Sqoop.java:147) at org.apache.hadoop.util.ToolRunner.run(ToolRunner.java:70) at org.apache.sqoop.Sqoop.runSqoop(Sqoop.java:183) at org.apache.sqoop.Sqoop.runTool(Sqoop.java:234) at org.apache.sqoop.Sqoop.runTool(Sqoop.java:243) at org.apache.sqoop.Sqoop.main(Sqoop.java:252) Caused by: java.lang.ClassNotFoundException: BigDataTest at java.net.URLClassLoader$1.run(URLClassLoader.java:366) at java.net.URLClassLoader$1.run(URLClassLoader.java:355) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:354) at java.lang.ClassLoader.loadClass(ClassLoader.java:425) at java.net.FactoryURLClassLoader.loadClass(URLClassLoader.java:789) at java.lang.ClassLoader.loadClass(ClassLoader.java:358) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:270) at org.apache.sqoop.util.ClassLoaderStack.addJarFile(ClassLoaderStack.java:88) ... 11 more

编辑: 您需要重组数据,因为您将按照现在定义的方式丢失所有重复项。有几种解决方法,这是一种:

{ 0:date(2018,10,20), 1:date(2018,11,8), 0:date(2018,12,25),},

这使具有重复值的键使用0

这意味着您简单的from datetime import date score_list = [ { 2:date(2018,10,20), 5:date(2018,11,8), 0:date(2018,12,25),}, { 3:date(2018,10,20), 1:date(2018,11,8), 4:date(2018,12,25),}, #{ 0:date(2018,10,20), 1:date(2018,11,8), 0:date(2018,12,25),}, { 0:[date(2018,10,20), date(2018,12,25)], 1:date(2018,11,8), }, { 2:date(2018,10,20), 7:date(2018,11,8), 3:date(2018,12,25),}, #{ 0:date(2018,10,20), 0:date(2018,11,8), 6:date(2018,12,25),}, { 0:[date(2018,10,20), date(2018,11,8)], 6:date(2018,12,25),}, ] 不再足够:

list

如果您将每个值都创建为列表,则可以简化此操作。