我正在尝试使用zip()和itertools.zip_longest()函数。我需要帮助理解他们,也许修复我正在得到的错误。有了zip(),代码就会运行,但它并不能获得其中一个列表中的所有条目。使用itertools.zip_longest(),我得到了一个我无法理解的错误。
这是我的代码(通过Google运行):
!wget https://raw.githubusercontent.com/muriloasouza/Deep-Learning/master/Input.txt
!wget https://raw.githubusercontent.com/muriloasouza/Deep-Learning/master/Labels.txt
!wget https://raw.githubusercontent.com/muriloasouza/Deep-Learning/master/Input_Train.txt
!wget https://raw.githubusercontent.com/muriloasouza/Deep-Learning/master/Labels_Train.txt
!wget https://raw.githubusercontent.com/muriloasouza/Deep-Learning/master/Input_Valid.txt
!wget https://raw.githubusercontent.com/muriloasouza/Deep-Learning/master/Labels_Valid.txt
batch_size = 3
df_input_train=pd.read_csv('./Input_Train.txt',usecols =['Wind_MWh','Actual_Load_MWh'],chunksize = 24*batch_size, iterator=True)
df_target_train=pd.read_csv('./Labels_Train.txt',usecols =['Potencia_Media_do_Vento_(MW)','Desvio_Padrao_Vento_(MW)','FCSPV_(Fracao_de_Carga_Suprida_pela_Potencia_do_Vento)'],chunksize = batch_size, iterator=True)
df_input_valid=pd.read_csv('./Input_Valid.txt',usecols =['Wind_MWh','Actual_Load_MWh'],chunksize = 24*batch_size, iterator=True)
df_target_valid=pd.read_csv('./Labels_Valid.txt',usecols =['Potencia_Media_do_Vento_(MW)','Desvio_Padrao_Vento_(MW)','FCSPV_(Fracao_de_Carga_Suprida_pela_Potencia_do_Vento)'],chunksize = batch_size, iterator=True)
c= 0
for chunk, chunk2, chunk3, chunk4 in itertools.zip_longest(df_input_train,df_target_train,df_input_valid,df_target_valid):
c = c+1
X_train = chunk.values
X_valid = chunk3.values
X_train = np.resize(X_train,(batch_size,24,2,1))
X_valid = np.resize(X_valid,(batch_size,24,2,1))
Y_train = chunk2.values
Y_valid = chunk4.values
print(X_train)
print(c)而错误是:
17 c = c+1
18 X_train = chunk.values
---> 19 X_valid = chunk3.values
20 X_train = np.resize(X_train,(batch_size,24,2,1))
21 X_valid = np.resize(X_valid,(batch_size,24,2,1))
AttributeError: 'NoneType' object has no attribute 'values'1: zip()迭代传递的最小列表(这就是它在其中一个列表中缺少一些条目的原因)?
2: zip_longest()迭代更大的?
3:,为什么我会犯这个错误?
4:存在吗?我可以在每个列表中迭代它的长度吗?
发布于 2019-11-30 20:10:29
zip结合了多个迭代器。
举个简单的例子,运行:
for x in zip(range(10),range(5)):
print(x)这只打印5次,因为zip结束于最短的迭代器。
zip_longest()将遍历所有条目,如果其中一个迭代器提前运行,它将被None替换。
import itertools
for x in itertools.zip_longest(range(10),range(5)):
print(x)1.是的,zip()迭代到最短迭代器的长度。
2.是的,zip_longest()迭代到最长迭代器的长度。
3.是因为chunk3比最长的迭代器短,所以在那个迭代中,它的值是None。没有一个没有values属性。
4.,现在您将它们都迭代到一起。问您是否只能对每个长度进行迭代是没有意义的,因为它们有不同的长度,而且您正在一起迭代。要么您遗漏了一些值,要么您有一些迭代的None值。如果您不需要将这些值放在一起,则可以查看类似迭代工具链之类的内容。
发布于 2019-11-30 20:06:42
,不管怎样,我可以在每个列表中迭代它的长度吗?
来自itertools.zip_longest文档字符串:
当较短的可迭代性耗尽时,
将替换
fillvalue。fillvalue默认为None,也可以由关键字参数指定。
>>> for a, b in itertools.zip_longest([1, 2, 3, 4], "abcdefghijk"):
print(a, b)
1 a
2 b
3 c
4 d
None e
None f
None g
None h
None i
None j
None k使用显式fillvalue
for a, b in itertools.zip_longest([1, 2, 3, 4], "abcdefghijk", fillvalue="YAY"):
print(a, b)
1 a
2 b
3 c
4 d
YAY e
YAY f
YAY g
YAY h
YAY i
YAY j
YAY k当某一项丢失时,您将不得不决定应该发生什么。
https://stackoverflow.com/questions/59119751
复制相似问题