Python 从没有列表理解的字符串列表创建列表

Python 从没有列表理解的字符串列表创建列表,python,list,Python,List,我有一份清单m: m=[‘苹果’、‘柠檬’、‘橘子’] 我希望输出如下: ['Apple']、['Lemon']、['Orange'] 如何在不使用列表理解和循环的情况下做到这一点 在我的方法中,我不明白使用什么变量。有什么想法吗 def lists(m): count=0` while count < len(m): len(m) count += 1 return #separat

我有一份清单m:

m=[‘苹果’、‘柠檬’、‘橘子’]

我希望输出如下:

['Apple']、['Lemon']、['Orange']

如何在不使用列表理解和循环的情况下做到这一点

在我的方法中,我不明白使用什么变量。有什么想法吗

def lists(m):    
    count=0`
    while count < len(m):            
        len(m)
        count += 1
        
    return #separated lists?

print(lists(m))
def列表(m):
计数=0`
当计数
既然您要求避免列表理解,您可以尝试以下方法:(顺便说一句,
只是一种
循环!)

m=['Apple'、'Lemon'、'Orange']
从输入导入列表开始
def拆分列表(lst:list[str]):
res=[]
对于lst中的项目:
res.append([项目])
返回res
打印(拆分列表(m))

这里有一种传统的
while
循环方法。有关其工作原理的详细信息,请参见评论

m = ['Apple', 'Lemon', 'Orange']

def list_to_lists(lst):
    i = 0

    # this will be the result list, containing lists of strings
    res = []

    while i < len(lst):

        # [lst[i]] creates a new list containing the i'th element of lst
        # res.append will append this to the result list
        res.append([lst[i]])

        i += 1

    # return the result list
    return res

lists = list_to_lists(m)
print(lists)
m=['Apple'、'Lemon'、'Orange']
def列表到def列表(lst):
i=0
#这将是结果列表,包含字符串列表
res=[]
而i

自从我真正编写了
while
循环以来,已经有相当长的时间了。请注意,
for
循环对于这类事情通常更整洁-参见@daniel hao的示例。

没有列表理解,大概没有“for cycles”(可能取决于您的意思),并根据要求生成一个
元组

>>> *map(lambda s: [s], m),
(['Apple'], ['Lemon'], ['Orange'])

您可以使用递归和参数解包

def makeLists(first,*rest): 
    return [[first]] + (makeLists(*rest) if rest else [])

m = ['Apple', 'Lemon', 'Orange']

print(makeLists(*m))

# [['Apple'], ['Lemon'], ['Orange']]
您也可以在不解包参数的情况下进行:

def makeLists(strings): 
    return ([strings[:1]] + makeLists(strings[1:])) if strings else []

m = ['Apple', 'Lemon', 'Orange']

print(makeLists(m))

# [['Apple'], ['Lemon'], ['Orange']]

您是否希望在不使用for循环的列表理解或不使用list comprehension或for循环的情况下解决问题?(您对生成器理解还满意吗?@l3via我对此表示怀疑,OP似乎采用了传统的
,而
循环方法,因为有这样一个奇怪的特定请求,例如“无清单理解“,最好能解释一下你为什么要这样做,这样答案就可以解释你的具体推理。你创建了一个不使用的
count
变量,这有点奇怪。如果不打算使用索引,那么使用
enumerate
也有点奇怪。为什么不为lst中的项目
?另一方面,OP要求不要对
循环使用
。你完全正确。我的坏左从OP的代码。现在纠正它。