按值作为索引删除列表中的元素

时间:2017-07-12 13:19:04

标签: python list indexing list-comprehension

我有一个列表,其中的时间序列中的天数不是从零开始的:

days = [2,3,4,5,...]

我有另一个列表,从第0天开始的所有日子都是云分钟:

weather = [0,120,150,60,120,30,300,...]

我想迭代days,如果weather中的相应索引大于某个值,则将其删除。

我试过

downtime = 100
days_new = [x for i, x in enumerate(days) if weather[i] < downtime]

然后应该导致:

days_new = [3,5,...]

因为删除的索引(2,4)在列表weather中的值大于100。

但它正在基于days索引而不是其作为索引的值来删除它们。即这只适用于我的列表从0开始,而不是任何大于0的整数。我该如何解决这个问题?

4 个答案:

答案 0 :(得分:4)

您的要求假定daysenumerate的所有项目都是{em>可订阅,在这种情况下,您不需要weather。使用days中的索引直接索引days_new = [x for x in days if weather[x] < downtime]

for(auto const& point : centroids_1)
  distance_vector.push_back(std::accumulate(begin(centroids_1), end(centroids_1), 0.0,
    [&](auto res, auto const& point2) { return res + norm(point - point2); }
  ));

答案 1 :(得分:1)

如果天气从days开始是天气指数的天气:

downtime = 100
new_days = [day for day in days if weather[day] <= downtime]

答案 2 :(得分:1)

你可以试试这个

# Array of spare numbers that meet your criteria
$firstAvailableSpare

# criteria
$currentStatus = "Spare"
$userBusinessUnit = "Support"
$userLocation = "WA"


$csv |`
    ForEach-Object {
        $LineUri += $_.LineUri
        $Status += $_.Status
        $BusinessUnit +=$_.BusinessUnit
        $Location +=$_.Location
    $currentStatus = "Spare"
    $userBusinessUnit = "Support"
    $userLocation = "WA"


    if ( ($_.Status -eq $currentStatus) -And ($_.BusinessUnit -eq $userBusinessUnit) -And ($_.Location -eq $userLocation) ) { 
        $firstAvailableSpare = $_ # Assign the first
        $_.Status = "Used"

        continue # break the loop so we don't get additional hits

    }
}

$csv | Export-Csv -Path $newpath -NoTypeInformation
$firstAvailableSpare # <-- your first available spare
$firstAvailableSpare.LineUri # <-- the URI you want

输出,供您输入:

[x for x in days if weather[x] <= 100]

答案 3 :(得分:0)

您可以尝试这种方式:

days = [2,3,4,5,...]
weather = [0,120,150,60,120,30,300,...]
downtime = 100
days_new = []
for x in days:
    if weather[x] < downtime:
        days_new.append(x)

# output :
days_new = [3, 5,...]