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

Posted

技术标签:

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

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

需要做什么:

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

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 从字符串中删除引号... 【参考方案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

【讨论】:

以上是关于如何从字符串列表中删除引号并将其再次存储为列表..?的主要内容,如果未能解决你的问题,请参考以下文章

如何从 Firebase 存储中下载图像并将其存储在颤动的列表中 [关闭]

节点已插入,但输入的第一个节点已从列表中删除

在列表中搜索字符串并将其存储在 Python 中

如何从 Firebase Firestore 获取数据并将其存储在列表中以在颤振小部件中使用?

如何从 JSON 字符串中删除括号和引号

将字符串列表转换为一个字符串[重复]