是的,我知道这个主题已经被覆盖过了(这里,这里,这里,这里),但据我所知,除了一个之外,所有解决方案都在这样的列表中失败:
L = [[[1, 2, 3], [4, 5]], 6]
Run Code Online (Sandbox Code Playgroud)
期望的输出是什么
[1, 2, 3, 4, 5, 6]
Run Code Online (Sandbox Code Playgroud)
或者甚至更好,一个迭代器.我看到的唯一适用于任意嵌套的解决方案可以在这个问题中找到:
def flatten(x):
result = []
for el in x:
if hasattr(el, "__iter__") and not isinstance(el, basestring):
result.extend(flatten(el))
else:
result.append(el)
return result
flatten(L)
Run Code Online (Sandbox Code Playgroud)
这是最好的型号吗?我忽略了什么吗?任何问题?
我有一个这样的列表列表.
documents = [['Human machine interface for lab abc computer applications','4'],
['A survey of user opinion of computer system response time','3'],
['The EPS user interface management system','2']]
Run Code Online (Sandbox Code Playgroud)
现在我需要遍历上面的列表并输出一个字符串列表,如下所示(没有原始列表中的数字)
documents = ['Human machine interface for lab abc computer applications',
'A survey of user opinion of computer system response time',
'The EPS user interface management system']
Run Code Online (Sandbox Code Playgroud) 我正在使用Python进行网络流量监控项目.不熟悉Python,所以我在这里寻求帮助.
简而言之,我正在检查流量和流量,我这样写:
for iter in ('in','out'):
netdata = myhttp()
print data
Run Code Online (Sandbox Code Playgroud)
netdata是一个由嵌套列表组成的列表,其格式如下:
[ [t1,f1], [t2,f2], ...]
Run Code Online (Sandbox Code Playgroud)
这里t代表的是时刻,f是流动.但是我只想在这个时刻保留这些f进行内外,我想知道如何获得有效的代码.
经过一些搜索,我认为我需要使用创建流量列表(2个元素),然后使用zip函数同时迭代这两个列表,但是我很难写出正确的列表.由于我的netdata是一个很长的列表,效率也非常重要.
如果有任何令人困惑的事情,请告诉我,我会尽力澄清.感谢帮助
我有列表列表,我需要通过 Python 遍历每个字符串,删除空格(条)并将列表保存到新列表中。
例如原始列表: org = [ [' a ','b '],['c ',' d '],['e ',' f'] ]
期待新列表: new = [ ['a','b'],['c','d'],['e','f'] ]
我从下面的代码开始,但不知道如何将剥离的对象添加到新的列表列表中。new.append(item) - 创建没有内部列表的简单列表。
new = []
for items in org:
for item in items:
item= item.strip()
new.append(item)
我正在尝试对列表进行排序。我如何打印或迭代每个列表中的第一个和最后一个元素?下面的非工作代码:
from numpy import *
xs=[[1.,2.,3.],[4.,5.,6.],[7.,8.,9.]]
for i in xs:
for j in xs[i]:
print(xs[1],xs[-1])
Run Code Online (Sandbox Code Playgroud)
如果需要则回溯错误:
runfile('/Users/Alex/untitled9.py', wdir='/Users/Alex')
Traceback (most recent call last):
File "<ipython-input-14-8a28382c7f81>", line 1, in <module>
runfile('/Users/Alex/untitled9.py', wdir='/Users/Alex')
File "/anaconda/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 880, in runfile
execfile(filename, namespace)
File "/anaconda/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "/Users/Alex/untitled9.py", line 13, in <module>
for j in xs[i]:
TypeError: list indices must be integers or slices, not list
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个简单的函数来重新格式化列表中的项目然后打印它们
这对于列表来说很简单:
my_list = [1/3,7/11,2/5] #generate list of floats
formatted_list = ['%.2f' % elem for elem in my_list] #format to 2 d.p
print(formatted_list) #print re-formatted list
Run Code Online (Sandbox Code Playgroud)
但我有一个嵌套列表列表.要遍历这些项目,我需要做一些像这个答案.然后,这让我写出来
my_list = [(1/3,1/4,1/5), (2/3,2/4,2/5), 3/3]
formatted_list = ['%.2f' % elem for elem in traverse(my_list)] #loop over unpacked list
Run Code Online (Sandbox Code Playgroud)
然而,这样做的主要缺点是它不再保留结构(列表中的元组).
那是:
print(formatted_list)
Run Code Online (Sandbox Code Playgroud)
回报
0.33, 0.25, 0.2, 0.66, 0.5, 0.4, 1.0
#I want (0.33, 0.25, 0.2), (0.66, 0.5, 0.4), 1.0
Run Code Online (Sandbox Code Playgroud)
有没有一种方法可以重新格式化,或者更一般地在任意深度嵌套列表中迭代项目,以返回与原始列表具有相同结构的列表?
假设我有一份清单清单.子列表本身可以包含子列表.将所有子列表的所有元素转换为特定类型的有效方法是什么?
让我们说它像这样凌乱:
a = [
1,
2,
3,
[
"a",
"b"
],
[
10,
20,
[
"hello",
"world"
]
],
4,
5,
"hi",
"there"
]
Run Code Online (Sandbox Code Playgroud)
我的想法是将类似的内容转换为:
a = [
"1",
"2",
"3",
[
"a",
"b"
],
[
"10",
"20",
[
"hello",
"world"
]
],
"4",
"5",
"hi",
"there"
]
Run Code Online (Sandbox Code Playgroud)
请注意,我正在寻找处理任意深度的子列表的方法.我有一种感觉,可以使用发电机,但我不知道如何处理这个问题.
python ×7
list ×4
python-3.x ×2
flatten ×1
generator ×1
nested-lists ×1
optimization ×1
sublist ×1
types ×1