【问题标题】:How to remove quotes from list of strings and store it as list again..?如何从字符串列表中删除引号并将其再次存储为列表..?
【发布时间】:2021-05-26 05:14:26
【问题描述】:

我必须在 for 循环中调用该函数。所有这些函数都作为带引号的字符串存储在列表中。我需要删除这些引号并将值再次存储在列表中。

需要做什么:

  1. 从 DB 中获取函数列表
  2. 从字符串列表中删除单引号/双引号
  3. 将这些字符串存储在列表中
  4. 循环列表执行函数

Python

fruits = ['apple','mango','orange']
print(type(fruits))
func = '[%s]'%','.join(map(str,fruits))
print(func) ## [apple,mango,orange]
print(type(func))

def apple():
  print("In apple")
def mango():
   print("In mango")
def orange():
   print("In orange")

n = len(func)
func_it = itertools.cycle(func)
for i in range(n):
   next(func_it)()

输出

<class 'list'>
[apple,mango,orange]
<class 'str'>

从字符串中删除引号后,其数据类型将更改为 . 有没有办法从字符串列表中删除引号并将这些值再次存储为列表?

【问题讨论】:

  • 我认为不可能,因为 list 从字符串中删除引号...

标签: python string list double-quotes single-quotes


【解决方案1】:

你不能这样调用函数。从字符串中删除引号不会将其变成函数。您正在将func 构造为'[apple, mango, orange]',这是一个字符串。当你迭代它时,你会得到字符串的每个字符。即你得到[a 等。每个都是一个字符串,你不能调用字符串。你基本上是在做'['() 这是没有意义的。

请记住 - 在 Python 中 - 函数是一流的对象。如果你想列出函数,只需将这些函数的引用放在一个列表中:

import itertools

def apple():
  print("In apple")
def mango():
   print("In mango")
def orange():
   print("In orange")

func = [apple, mango, orange]  # list of functions
n = len(func)
func_it = itertools.cycle(func)
for i in range(n):
    x = next(func_it)
    print(type(x))  # check the type
    x()

结果:

<class 'function'>
In apple
<class 'function'>
In mango
<class 'function'>
In orange

所以如果你想从你的字符串 '[apple, mango, orange]' 构造这个列表,你需要 eval 它:

s = '[apple, mango, orange]'
func = eval(s)
print(func)

结果:

[<function apple at 0x000001FB9E7CF040>, <function mango at 0x000001FB9ECB7940>, <function orange at 0x000001FB9ECB7DC0>]

但是,如果可能,您应该始终尽量避免使用eval

【讨论】:

    【解决方案2】:

    您可以使用内置的 python exec() 函数将任何字符串作为代码执行。

    #!/usr/bin/env python3
    
    fruits = ['apple','mango','orange']
    
    def apple():
      print("In apple")
    def mango():
       print("In mango")
    def orange():
       print("In orange")
    
    for func in fruits:
        exec(func + '()')  
    

    输出

    In apple
    In mango
    In orange
    

    【讨论】:

      【解决方案3】:

      根据您的代码,我猜您想根据字符串调用函数?我建议使用字典

      import itertools
      fruits = ['apple','mango','orange']
      def apple():
        print("In apple")
      def mango():
         print("In mango")
      def orange():
         print("In orange")
      funcs = {'apple':apple()}
      funcs['apple']
      

      出来

      In apple
      

      【讨论】:

        【解决方案4】:

        您可以使用globals() 使用名称获取函数对象,然后您可以使用该对象

        func = [globals()[fun] for fun in fruits]
        func_it = itertools.cycle(func)
        for i in range(len(func)):
           next(func_it)()
        

        输出:

        In apple
        In mango
        In orange
        

        【讨论】:

          【解决方案5】:

          这里,hehe = eval(func) 行创建了一个名称列表,稍后将其作为函数调用,这是可能的,因为结尾括号“()”不是必需的,至少在 Python 3.9 中是这样。

          import itertools
          
          def apple():
            print("In apple")
          def mango():
             print("In mango")
          def orange():
             print("In orange")
          
          fruits = ['apple','mango','orange']
          print(type(fruits))
          func = '[%s]'%','.join(map(str,fruits))
          print(func) ## [apple,mango,orange]
          hehe = eval(func)
          print(type(hehe))
          
          n = len(hehe)
          func_it = itertools.cycle(hehe)
          for i in range(n):
             next(func_it)()
          

          输出:

          <class 'list'>
          [apple,mango,orange]
          <class 'list'>
          In apple
          In mango
          In orange
          
          

          【讨论】:

            猜你喜欢
            • 2023-04-04
            • 2018-01-15
            • 1970-01-01
            • 2019-04-24
            • 2014-07-30
            • 1970-01-01
            • 1970-01-01
            • 2016-12-08
            • 1970-01-01
            相关资源
            最近更新 更多