4136

Math operations from a list [duplicate]

Question:

This question already has an answer here:

<ul><li> <a href="/questions/43101611/string-in-list-into-a-function" dir="ltr" rel="nofollow">String in list, into a function</a> <span class="question-originals-answer-count"> 5 answers </span> </li> </ul>

I imported myList from a .txt file (I converted the numbers to integers)

from math import * myList = [100, 'sin', 0, 1] x = pi

How would I go about calling the sin function for my given value of x?

myList[1](pi)

I hoped this would simply return sin(pi), but it does not, because it is just the string 'sin(x)'

Answer1:

Do not store functions as string literals, store the actual functions.

>>> from math import sin, exp, log >>> funcs = [sin, exp, log] >>> x = 0 >>> funcs[0](x) 0.0 >>> funcs[1](x) 1.0 >>> funcs[2](2.71) 0.9969486348916096

However, if you don't plan to do something more involved with that list of functions, you can just call them directly.

Recommend

  • Python sin and cosine giving incorrect values
  • AngularJS - ngBind and Bootstrap Switch
  • python struct.pack(): pack multiple datas in a list or a tuple
  • How can I use a custom function within an expression using the eval dataframe method?
  • Two columns in subquery in where clause
  • How do I generate a Sine Sweep in Java (Android)
  • Is a .txt file created in VB different than one I'd randomly create?
  • I don't get what's the difference between format() and … (python)
  • How to Divide an array on c#?
  • Correctly Importing Apache Commons Math Package
  • How to search a CSV file with php by checking if a date falls between 2 ranges
  • init_seg and warning C4073 from library code?
  • iOS Localization Doesn't Work with More Than 63 Files
  • Efficient & Pythonic way of finding all possible sublists of a list in given range and the minim
  • why calling cd shell command through system() or execvp() from a child process won't work?
  • JavaScript IE rotation transform maths
  • How to create a 2D image by rotating 1D vector of numbers around its center element?
  • C# fibonacci function returning errors
  • Default parameter as generic type
  • D3 get axis values on zoom event
  • ActiveRecord query for a count of new users by day
  • QLineEdit password safety
  • Splitting given String into two variables - php
  • NetLogo BehaviorSpace - Measure runs using reporters
  • Is my CUDA kernel really runs on device or is being mistekenly executed by host in emulation?
  • Sending data from AppleScript to FileMaker records
  • Running a C# exe file
  • Symfony2: How to get request parameter
  • Build own AppleScript numerical error handling
  • Google cloud sdk not working when python points python3
  • Why winpcap requires both .lib and .dll to run?
  • Return words with double consecutive letters
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • Python: how to group similar lists together in a list of lists?
  • Buffer size for converting unsigned long to string
  • Load html files in TinyMce
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • coudnt use logback because of log4j
  • python draw pie shapes with colour filled
  • Busy indicator not showing up in wpf window [duplicate]