【问题标题】:Error: TypeError: can't multiply sequence by non-int of type 'list'错误:TypeError:不能将序列乘以“列表”类型的非整数
【发布时间】:2018-11-30 07:13:56
【问题描述】:

我刚开始使用 python 并针对此错误。有谁知道为什么以及是否将我的数据保存为整数或如何做到这一点?

Traceback (most recent call last):
  File "C:\Users\Gabriela Lammoglia\AppData\Local\Programs\Python\Python37-32\open.py", line 34, in <module>
    latt2=mult*lattice

代码:

from csv import reader
from collections import defaultdict
from ast import literal_eval
from pprint import pprint

data = defaultdict(list)
with open('crystal.csv') as f:
    csv_reader = reader(f)

    current_header = None
    for line in csv_reader:

        # We found a header
        if len(line) == 1 and all(item.isalpha() or item.isspace() for item in line[0]):
            current_header = line[0]

        # Otherwise normal line with ints and floats
        else:
            data[current_header].append(list(map(literal_eval, line)))

pprint(data)

mult=data['multiplicty']
lattice=data['lattice parameters']
occup=data['occupancy']
sites=data['atom sites']

print(mult)
print(lattice)
print(occup)
print(sites)

import numpy as np
latt2=mult*lattice

【问题讨论】:

  • 你的预期输出是什么?
  • mullattice 都是 list 对象。因此,当您尝试将两个list 对象相乘时,将得到TypeError: can't multiply sequence by non-int of type 'list'。在你的 python 解释器中尝试[] * [] :)。
  • 当我从 csv 中提取数据时,我不知道我得到了一个列表。我试图理想地将两个向量相乘并得到一个向量作为输出。有没有办法将列表转换为整数向量?

标签: python error-handling int


【解决方案1】:
latt2=int(mult)*lattice

您需要将 mult 值设为整数。现在你尝试多个字符串和列表,这是个坏主意。

【讨论】:

  • 我试过这个并且得到了这个错误:类型错误:int() 参数必须是一个字符串、一个类似字节的对象或一个数字,而不是“列表”。我刚开始使用 Python 有没有办法将我的列表转换为整数?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-11-15
  • 2010-12-30
  • 2012-10-09
  • 1970-01-01
  • 1970-01-01
  • 2018-04-20
  • 2019-04-12
相关资源
最近更新 更多