Home > Net >  How to resolve - TypeError: can't multiply sequence by non-int of type 'float'
How to resolve - TypeError: can't multiply sequence by non-int of type 'float'

Time:11-26

Temp_ = [274,275,276,277,278,279,280,281,282,283,284,285,286,287,288,289,290,291,292,293,294,295,296,297,298,299,300,301,302,303,304,305,306,307,308,309,310,311,312,313,314,315,316,317,318,319,320,321,322,323,324,325,326,327,328,329,330,331,332,333,334,335,336,337,338,339,340,341,342,343,344,345,346,347,348,349,350,351,352,353,354,355,356,357,358,359,360,361,362,363,364,365,366,367,368,369,370,371,372,373,374,375,376,377,378,379,380,381,382,383,384,385,386,387,388,389,390,391,392,393,394,395,396,397,398,399,400,401,402,403,404,405,406,407,408,409,410,411,412,413,414,415,416,417,418,419,420,421,422,423,424,425,426,427,428,429,430,431,432,433,434,435,436,437,438,439,440,441,442,443,444,445,446,447,448,449,450,451,452,453,454,455,456,457,458,459,460,461,462,463,464,465,466,467,468,469,470,471,472,473,474,475,476,477,478,479,480,481,482,483,484,485,486,487,488,489,490,491,492,493,494,495,496,497,498,499,500,501,502,503,504,505,506,507,508,509,510,511,512,513,514,515,516,517,518,519,520,521,522,523]

import math as mt
Debye_A = 3.3690e-01 - 6.3210e-04*Temp_   (9.1425/Temp_) - (1.3514e-02*mt.log(Temp_))   (2.2609e-03/(Temp_-263))   (1.9212e-06*(Temp_**2))   (45.2586/(680-Temp_))

I tried to do this and I get this error, how do I g

CodePudding user response:

Use numpy to convert Temp_ list as a numpy array and replace math.log by np.log

import numpy as np

Temp_ = np.array(Temp_)

Debye_A = 3.3690e-01 - 6.3210e-04*Temp_   (9.1425/Temp_) - (1.3514e-02*np.log(Temp_)) \
            (2.2609e-03/(Temp_-263))   (1.9212e-06*(Temp_**2))   (45.2586/(680-Temp_))

Output:

>>> Debye_A
array([0.37713151, 0.3776417 , 0.37816079, 0.37868821, 0.37922355,
       0.3797665 , 0.38031681, 0.38087432, 0.38143886, 0.38201033,
       0.38258863, 0.38317368, 0.38376543, 0.38436382, 0.3849688 ,
       0.38558036, 0.38619844, 0.38682303, 0.38745412, 0.38809167,
       0.38873568, 0.38938613, 0.39004302, 0.39070633, 0.39137606,
       0.39205221, 0.39273477, 0.39342374, 0.39411911, 0.39482089,
       0.39552908, 0.39624368, 0.39696468, 0.3976921 , 0.39842594,
       0.39916619, 0.39991286, 0.40066596, 0.4014255 , 0.40219147,
       0.40296389, 0.40374276, 0.40452808, 0.40531987, 0.40611813,
       0.40692287, 0.4077341 , 0.40855183, 0.40937606, 0.41020681,
       0.41104408, 0.41188789, 0.41273824, 0.41359515, 0.41445862,
       0.41532867, 0.41620532, 0.41708856, 0.41797842, 0.4188749 ,
       0.41977803, 0.4206878 , 0.42160425, 0.42252737, 0.42345718,
       0.4243937 , 0.42533695, 0.42628693, 0.42724366, 0.42820717,
       0.42917745, 0.43015454, 0.43113845, 0.43212919, 0.43312678,
       0.43413123, 0.43514258, 0.43616083, 0.437186  , 0.43821812,
       0.4392572 , 0.44030326, 0.44135632, 0.4424164 , 0.44348352,
       0.44455771, 0.44563898, 0.44672735, 0.44782286, 0.44892551,
       0.45003534, 0.45115236, 0.45227661, 0.4534081 , 0.45454685,
       0.4556929 , 0.45684627, 0.45800699, 0.45917507, 0.46035055,
       0.46153346, 0.46272381, 0.46392164, 0.46512699, 0.46633986,
       0.4675603 , 0.46878834, 0.470024  , 0.47126732, 0.47251832,
       0.47377704, 0.47504351, 0.47631776, 0.47759983, 0.47888974,
       0.48018754, 0.48149326, 0.48280693, 0.48412859, 0.48545828,
       0.48679602, 0.48814187, 0.48949586, 0.49085802, 0.49222839,
       0.49360703, 0.49499396, 0.49638923, 0.49779287, 0.49920494,
       0.50062548, 0.50205452, 0.50349212, 0.50493832, 0.50639316,
       0.5078567 , 0.50932898, 0.51081004, 0.51229994, 0.51379873,
       0.51530646, 0.51682318, 0.51834894, 0.51988379, 0.5214278 ,
       0.52298101, 0.52454347, 0.52611526, 0.52769642, 0.52928701,
       0.5308871 , 0.53249674, 0.534116  , 0.53574494, 0.53738361,
       0.5390321 , 0.54069045, 0.54235875, 0.54403705, 0.54572543,
       0.54742396, 0.54913271, 0.55085175, 0.55258115, 0.554321  ,
       0.55607137, 0.55783233, 0.55960397, 0.56138636, 0.56317959,
       0.56498375, 0.56679891, 0.56862516, 0.5704626 , 0.57231131,
       0.57417138, 0.57604291, 0.57792598, 0.57982071, 0.58172718,
       0.58364549, 0.58557575, 0.58751805, 0.58947252, 0.59143924,
       0.59341833, 0.59540991, 0.59741408, 0.59943097, 0.60146068,
       0.60350334, 0.60555908, 0.607628  , 0.60971025, 0.61180596,
       0.61391524, 0.61603824, 0.6181751 , 0.62032595, 0.62249094,
       0.62467021, 0.62686391, 0.62907218, 0.6312952 , 0.6335331 ,
       0.63578606, 0.63805423, 0.64033778, 0.64263689, 0.64495172,
       0.64728245, 0.64962927, 0.65199235, 0.65437189, 0.65676808,
       0.65918112, 0.66161121, 0.66405854, 0.66652334, 0.66900581,
       0.67150618, 0.67402466, 0.67656149, 0.67911689, 0.68169112,
       0.6842844 , 0.68689699, 0.68952915, 0.69218113, 0.69485321,
       0.69754564, 0.70025872, 0.70299273, 0.70574796, 0.7085247 ,
       0.71132327, 0.71414398, 0.71698714, 0.71985309, 0.72274216,
       0.72565469, 0.72859104, 0.73155157, 0.73453665, 0.73754666,
       0.74058198, 0.74364302, 0.74673017, 0.74984387, 0.75298454])

CodePudding user response:

To perform mathematical operations on every element of an array, you should use numpy. If you want to do it in a regular Python list, you have to loop or use an explicit mapping / comprehension on the list elements:

import math as mt

def f(t):
    return 3.3690e-01 - 6.3210e-04*t   (9.1425/t) - (1.3514e-02*mt.log(t)) \
           (2.2609e-03/(t-263))   (1.9212e-06*(t**2))   (45.2586/(680-t))

Debye_A = [ f(t) for t in Temp_ ]

# or 

Debye_A = list(map(f,Temp_))

print(Debye_A)
[0.37713150689152897, 0.3776416978945515, 0.3781607869090762,
 0.37868820860326485, 0.37922354847328565, ...
  • Related