我有两个长名单.我基本上想从这个列表中删除与condtion不匹配的元素.例如,
list_1=['a', 'b', 'c', 'd']
list_2=['1', 'e', '1', 'e']
Run Code Online (Sandbox Code Playgroud)
列表一和二对应.现在我想删除列表中与我的条件不匹配的某些元素.我必须确保从列表2中删除相应的元素,并且顺序不会搞砸.
所以我创建了一个遍历列表1的for循环,并存储了必须删除的元素的所有索引.
让我们说:
index_list = ['1', '3']
Run Code Online (Sandbox Code Playgroud)
基本上,我需要确保从列表1中删除b和d以及从列表2中删除e和e.我该怎么做?
我试过了:
del (list_1 [i] for i in index_list)]
del (list_2 [i] for i in index_list)]
Run Code Online (Sandbox Code Playgroud)
但我得到一个错误,索引必须是一个列表,而不是列表.我也尝试过:
list_1.remove[i]
list_2.remove[i]
Run Code Online (Sandbox Code Playgroud)
但这也不起作用.我尝试创建另一个循环:
for e, in (list_1):
for i, in (index_list):
if e == i:
del list_1(i)
for j, in (list_2):
for i, in (index_list):
if j == i:
del list_2(i)
Run Code Online (Sandbox Code Playgroud)
但这也不起作用.它给了我一个错误,即e和j不是全局名称.
作为一个简单的例子:
list1 = ['a', 'b', 'c']
list2 = ['a', 'stack', 'overflow']
for i in list1 and list2:
print i
Run Code Online (Sandbox Code Playgroud)
这将打印出所有元素list2.为什么是这样?我怎样才能打印两个列表中的元素?
是否可以告诉 For 循环从特定位置开始?
>>> languages = ["C", "C++", "Perl", "Python"]
>>> for x in languages:
... print(x)
...
C
C++
Perl
Python
>>>
Run Code Online (Sandbox Code Playgroud)
如何让脚本从“Perl”开始?我不需要它来循环。
我试图找到v的索引,但它总是给我:
'numpy.ndarray' object has no attribute 'index'
我已经尝试过:
TypeError:slice索引必须为整数或None或具有__index__方法。怎么解决呢?
如何在数组中找到数组的索引。
在Python中给定包含该项目的列表的情况下查找项目的索引
他们都没有回答我的问题
v = np.random.randn(10)
print(v)
maximum = np.max(v)
minimum = np.min(v)
print(maximum, minimum)
v.index(ma, mi)
Run Code Online (Sandbox Code Playgroud) 我的任务
我正在尝试使用正则表达式查找出现在字符串中的单词的位置
码
import re
# A random string
mystr = "there not what is jake can do for you ask what you play do for spare jake".upper()
match = re.search(r"[^a-zA-Z](jake)[^a-zA-Z]", mystr)
print match.start(1)
Run Code Online (Sandbox Code Playgroud)
输出量
18
Run Code Online (Sandbox Code Playgroud)
预期产量
我希望我的输出包含字符串的位置jake:
5, 17
Run Code Online (Sandbox Code Playgroud)
编辑:为了澄清,我正在尝试确定单词的位置。我相信我所做的就是找到索引,并且不确定如何使其按预期工作
基本上我是多么喜欢它,是:
另一篇文章的例子:
some_list = ['abc-123', 'def-456', 'ghi-789', 'abc-456']
for any("abc" in s for s in some_list):
#s isn't the index, right?
Run Code Online (Sandbox Code Playgroud)
那么,问题基本上是,我该怎么做才能做到.对不起,我的格式和英语很差,等等.
例:
我有一个字符串 "La die la Blablabla and flowers are red"
我有一个阵列TheArray,['Blablabla', 'thisonewillnotbefound', 'flowers', 'thisonenoteither', 'red']
我需要一个遍历数组中每个项目的循环,每当它找到一个存在于其中的项目时,它将被附加到一个全新的列表中.
这是一个非常简单的问题,但我似乎无法理解为什么我没有得到它.
def listindex():
li = ['a', 'e', 'a', 'd', 'b', 'a', 'e']
for x in li:
if x == 'a':
print(li.index(x))
Run Code Online (Sandbox Code Playgroud)
结果:
0
0
0
Run Code Online (Sandbox Code Playgroud)
预期结果:
0
2
5
Run Code Online (Sandbox Code Playgroud)
虽然它迭代了所有项目我只得到第一项索引,为什么呢?即使它非常简单,也建议.
我有两个清单.第一个列表具有特定长度,第二个列表具有不稳定长度.我想从两个列表中分配项目.我用colour_index = sel_pos%(len(colours)).sel_pos是不稳定列表中项目的位置.如何每次都找到这个位置?
考虑以下元素列表。
h = [38, 203, 1, 45, 39, 10, 34, 90, 10, 2, 100, 1]
如果将其放入基于数组的堆中,它将以以下方式显示。
import heapq
heapq.heapify(h)
# now we have a heap that looks like this
# [1, 2, 1, 10, 39, 10, 34, 90, 45, 203, 100, 38]
Run Code Online (Sandbox Code Playgroud)
找出39这个堆中位置的最佳方法是什么?
找出的一种方法是从堆中弹出项目,直到它返回 39,这样如果我们跟踪我们从堆中弹出项目的次数,我们就知道它的位置。然而,这不是很有效,因为我们修改了堆本身。
有没有更好的方法来解决问题?
我写了一个if声明如下:
if word in vocab:
print word
Run Code Online (Sandbox Code Playgroud)
但是,我也想知道匹配的word索引vocab.
有没有办法在Python中可以做到这一点?
我只是提出了一个vocab.index(word)用于获取索引的解决方案,但这种方式通过数组两次(一个在if语句中,一个调用index()).我想知道应该有更多的效率方法.
谢谢.