简体   繁体   English

如何从字符串列表中删除引号并将其再次存储为列表..?

[英]How to remove quotes from list of strings and store it as list again..?

I have to call the function in a for loop.我必须在 for 循环中调用 function。 All those functions are stored in a list as a string with quotes.所有这些函数都作为带引号的字符串存储在列表中。 I need to remove those quotes and store the values again in a list.我需要删除这些引号并将值再次存储在列表中。

What needs to be done:需要做什么:

  1. Fetch the function list from DB从 DB 中获取 function 列表
  2. Remove single/double quotes from a list of strings从字符串列表中删除单/双引号
  3. Store those strings in a list将这些字符串存储在列表中
  4. Loop the list to execute functions循环列表执行函数

Python 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)()

Output Output

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

After removing the quotes from the strings, Its data type is getting changed to.从字符串中删除引号后,其数据类型将更改为。 Is there any way to remove quotes from a list of strings and store those values as a list again?有没有办法从字符串列表中删除引号并将这些值再次存储为列表?

You can't call functions like that.你不能调用这样的函数。 Removing quotes from a string won't turn it into a function.从字符串中删除引号不会将其变成 function。 You are constructing func to be '[apple, mango, orange]' which is a string.您正在将func构造为'[apple, mango, orange]' ,它是一个字符串。 When you iterate over that you get each of the characters of the string.当你迭代它时,你会得到字符串的每个字符。 ie you get [ , a etc. Each is a string & you can't call strings.即你得到[a等。每个都是一个字符串,你不能调用字符串。 You're basically doing '['() which is meaningless.你基本上是在做'['()这是没有意义的。

Remember - in Python - functions are first-class objects.请记住 - 在 Python 中 - 函数是一流的对象。 If you want to list over functions just put references to those functions in a list:如果要列出函数,只需将这些函数的引用放在列表中:

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()

Which results in:结果是:

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

So if you want to construct this list from your string '[apple, mango, orange]' you need to eval it:所以如果你想从你的字符串'[apple, mango, orange]'构造这个列表,你需要eval它:

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

Which results in:结果是:

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

However if possible you should always try to avoid using eval但是,如果可能的话,您应该始终尽量避免使用eval

Based on your code i'm guessing you want to call a function based on a string?根据您的代码,我猜您想根据字符串调用 function 吗? I suggest using a dictionary我建议使用字典

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']

out出去

In apple

You can use globals() to get the function object using name then you can use that object您可以使用globals()使用名称获取 function object 然后您可以使用该 object

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

Output: Output:

In apple
In mango
In orange

You can use the built in python exec() function that will execute any string as code.您可以使用内置的 python exec() function 将任何字符串作为代码执行。

#!/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 + '()')  

Output Output

In apple
In mango
In orange

Here, the line hehe = eval(func) creates a list of the names which are later taken as function calls and it's possible because the ending parentheses "()" are not necessary, atleast in Python 3.9.在这里,行hehe = eval(func)创建了一个名称列表,稍后将其作为 function 调用,这是可能的,因为结束括号“()”不是必需的,至少在 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)()

output: output:

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

    fruits = '[apple, mango, orange]'
    import json
    fruits= json.loads(fruits)

output: fruits = [apple, mango, orange] output: fruits = [apple, mango, orange]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM