python - 从相同索引位置的多个列表中删除元素
问题描述
我的程序有许多相互依赖的列表。我需要的是,当我按索引从主列表(称为 LIST_5)中删除一个元素时,它将删除所有其他列表上的相应索引。
我只删除了主列表(LIST_5)中的元素后被卡住了,我不知道如何对其他列表做同样的事情。
LIST_1 = ['english', 'english', 'french', 'english', 'english', 'english']
LIST_2 = ['documentation', 'music', 'position', 'social media', 'microeconomics', 'financial equations']
LIST_3 = ['ea_12', 'ea_12', 'ea_12', 'ea_12', 'ea_12', 'ea_12']
LIST_4 = ['141031', '141032', '141033', '141034', '141035', '141036']
LIST_5 = ['allowed', 'allowed', 'rejected', 'allowed', 'rejected', 'allowed']
for L5 in LIST_5:
if 'allowed' in L5:
pass
else:
LIST_5.remove(L5)
print(LIST_5)
# OUTPUT:
# ['allowed', 'allowed', 'allowed', 'allowed']
# How to apply the removal process for all other lists?
所以它的输出将是:
# LIST_1 = ['english', 'english', 'english', 'english']
# LIST_2 = ['documentation', 'music', 'social media', 'financial equations']
# LIST_3 = ['ea_12', 'ea_12', 'ea_12', 'ea_12']
# LIST_4 = ['141031', '141032', '141034', '141036']
# LIST_5 = ['allowed', 'allowed', 'allowed', 'allowed']
解决方案
您希望以一种方式压缩它们,使每个单词与来自其他列表中相同索引的其他单词配对。 zip
这样做,您可以为每个新列表使用索引 4 来保留或删除。然后就是将它们重塑回原始形式的问题list(zip(*...)))
LIST_1 = ['english', 'english', 'french', 'english', 'english', 'english']
LIST_2 = ['documentation', 'music', 'position', 'social media', 'microeconomics', 'financial equations']
LIST_3 = ['ea_12', 'ea_12', 'ea_12', 'ea_12', 'ea_12', 'ea_12']
LIST_4 = ['141031', '141032', '141033', '141034', '141035', '141036']
LIST_5 = ['allowed', 'allowed', 'rejected', 'allowed', 'rejected', 'allowed']
编辑:
要解释这里发生的事情,请考虑内部拉链
tuple(zip(LIST_1,LIST_2,LIST_3,LIST_4,LIST_5))
它正在按列重塑数据
(('english', 'documentation', 'ea_12', '141031', 'allowed'),
('english', 'music', 'ea_12', '141032', 'allowed'),
('french', 'position', 'ea_12', '141033', 'rejected'),
('english', 'social media', 'ea_12', '141034', 'allowed'),
('english', 'microeconomics', 'ea_12', '141035', 'rejected'),
('english', 'financial equations', 'ea_12', '141036', 'allowed'))
这里每组中的最后一个值位于索引 4 处,并且被允许/拒绝,因此在列表理解中,我们仅保留索引 4 处的单词为“允许”的元素
您可以在此处看到该行为,您获得的是通过条件的列的列表:
[x for x in tuple(zip(LIST_1,LIST_2,LIST_3,LIST_4,LIST_5)) if x[4]=='allowed']
输出
[('english', 'documentation', 'ea_12', '141031', 'allowed'),
('english', 'music', 'ea_12', '141032', 'allowed'),
('english', 'social media', 'ea_12', '141034', 'allowed'),
('english', 'financial equations', 'ea_12', '141036', 'allowed')]
现在我们可以使用*
来解包元组列表,并将zip
它们改回逐行,基本上颠倒第一个zip
. 最后,您可以将它们分配回其原始值。
整个代码。
LIST_1, LIST_2, LIST_3, LIST_4, LIST_5 = list(zip(*[x for x in tuple(zip(LIST_1,LIST_2,LIST_3,LIST_4,LIST_5)) if x[4]=='allowed']))
推荐阅读
- angular - Angular - 指令 - 使用“null”与“”(空字符串)触发@Input setter
- ubuntu - 当我尝试在目录中跟随时,不允许进行数字海洋存储操作
- c++ - 无法调试此代码的分段错误
- c# - Unity:如何使用新输入系统处理 GetButton 情况?
- c++ - Visual Studio - CMake - Mingw64 配置
- python - Gitlab CI搭建django postgres服务
- css - 在 Angular Material 中显示锚标记内的元素一个在另一个之下
- c# - 如何在 Xamarin 中使用具有 android/IOS 特定视图的控制模板
- javascript - 如果 IF 语句为真,如何只运行一次函数?
- openshift - OpenShift import docker image from JFrog Artifactory 未授权错误