GVKun编程网logo

根据元素的某些方面,如何将Python列表分为两个列表(python把一个列表分成2个列表)

49

此处将为大家介绍关于根据元素的某些方面,如何将Python列表分为两个列表的详细内容,并且为您解答有关python把一个列表分成2个列表的相关问题,此外,我们还将为您介绍关于Dart:根据条件将列表分

此处将为大家介绍关于根据元素的某些方面,如何将Python列表分为两个列表的详细内容,并且为您解答有关python把一个列表分成2个列表的相关问题,此外,我们还将为您介绍关于Dart:根据条件将列表分为两个子列表、Java:将一个列表分为两个子列表?、python – 列表元素的最大总和,每个列表元素由(至少)k个元素分隔、python – 如何将两个列表的元素添加到一个列表中?extend的有用信息。

本文目录一览:

根据元素的某些方面,如何将Python列表分为两个列表(python把一个列表分成2个列表)

根据元素的某些方面,如何将Python列表分为两个列表(python把一个列表分成2个列表)

我有一个这样的清单:

[[8, "Plot", "Sunday"], [1, "unPlot", "Monday"], [12, "Plot", "Monday"], [10, "Plot", "Tuesday"], [4, "unPlot", "Tuesday"], [14, "Plot", "Wednesday"], [6, "unPlot", "Wednesday"], [1, "unPlot", "Thursday"], [19, "Plot", "Thursday"], [28, "Plot", "Friday"], [10, "unPlot", "Friday"], [3, "unPlot", "Saturday"]]

我想根据PlotunPlot值将其分为两个列表,结果是:

list1=[[8, "Plot", "Sunday"], [12, "Plot", "Monday"], ...]list2=[[1, "unPlot", "Monday"], [4, "unPlot", "Tuesday"], ...]

答案1

小编典典

尝试基本的列表理解:

>>> [ x for x in l if x[1] == "Plot" ][[8, ''Plot'', ''Sunday''], [12, ''Plot'', ''Monday''], [10, ''Plot'', ''Tuesday''], [14, ''Plot'', ''Wednesday''], [19, ''Plot'', ''Thursday''], [28, ''Plot'', ''Friday'']]>>> [ x for x in l if x[1] == "unPlot" ][[1, ''unPlot'', ''Monday''], [4, ''unPlot'', ''Tuesday''], [6, ''unPlot'', ''Wednesday''], [1, ''unPlot'', ''Thursday''], [10, ''unPlot'', ''Friday''], [3, ''unPlot'', ''Saturday'']]

或者,filter如果您喜欢函数式编程,也可以:

>>> filter(lambda x: x[1] == "Plot", l)[[8, ''Plot'', ''Sunday''], [12, ''Plot'', ''Monday''], [10, ''Plot'', ''Tuesday''], [14, ''Plot'', ''Wednesday''], [19, ''Plot'', ''Thursday''], [28, ''Plot'', ''Friday'']]>>> filter(lambda x: x[1] == "unPlot", l)[[1, ''unPlot'', ''Monday''], [4, ''unPlot'', ''Tuesday''], [6, ''unPlot'', ''Wednesday''], [1, ''unPlot'', ''Thursday''], [10, ''unPlot'', ''Friday''], [3, ''unPlot'', ''Saturday'']]

我个人发现列表理解要清晰得多。当然,这是最“ pythonic”的方式。

Dart:根据条件将列表分为两个子列表

Dart:根据条件将列表分为两个子列表

快速解决方案:

var s_list = [1,2,3,4,5,6,7,8];

s_list.where( (el) => el % 2 == 0 ).toList();
s_list.where( (el) => el % 2 != 0 ).toList();
,

如果您想做很多事情,最好在您的项目中创建一个扩展方法,该方法可以执行您想要的操作。我提出了以下设计,该设计应该以通用且有效的方式工作:

void main() {
  final s_list = [1,8];
  final match = s_list.splitMatch((element) => element % 2 == 0);

  print(match.matched);   // [2,8]
  print(match.unmatched); // [1,7]
}

extension SplitMatch<T> on List<T> {
  ListMatch<T> splitMatch(bool Function(T element) matchFunction) {
    final listMatch = ListMatch<T>();

    for (final element in this) {
      if (matchFunction(element)) {
        listMatch.matched.add(element);
      } else {
        listMatch.unmatched.add(element);
      }
    }

    return listMatch;
  }
}

class ListMatch<T> {
  List<T> matched = <T>[];
  List<T> unmatched = <T>[];
}

Java:将一个列表分为两个子列表?

Java:将一个列表分为两个子列表?

在Java中将列表拆分为两个子列表的最简单,最标准和/或最有效的方法是什么?可以更改原始列表,因此无需复制。方法签名可以是

/** Split a list into two sublists. The original list will be modified to * have size i and will contain exactly the same elements at indices 0  * through i-1 as it had originally; the returned list will have size  * len-i (where len is the size of the original list before the call)  * and will have the same elements at indices 0 through len-(i+1) as  * the original list had at indices i through len-1. */<T> List<T> split(List<T> list, int i);

[EDIT]
List.subList返回原始列表上的视图,如果修改了原始视图,该视图将无效。因此,除非它也放弃了原始参考文献,否则split无法使用subList(或者,如Marc
Novakowski的回答所述,使用subList但立即复制结果)。

答案1

小编典典

快速半伪代码:

List sub=one.subList(...);List two=new XxxList(sub);sub.clear(); // since sub is backed by one, this removes all sub-list items from one

它使用标准的List实现方法,并避免了所有循环运行。clear()方法还将removeRange()对大多数列表使用内部方法,并且效率更高。

python – 列表元素的最大总和,每个列表元素由(至少)k个元素分隔

python – 列表元素的最大总和,每个列表元素由(至少)k个元素分隔

给出一个数字列表,找到时间复杂度为o(n)且空间复杂度为o(1)的非相邻元素的最大总和,我可以使用:

sum1= 0
sum2= list[0]

for i in range(1,len(list)):
    num= sum1
    sum1= sum2+ list[i]
    sum2= max(num,sum2)

print(max(sum2,sum1))

只有当k = 1 [求和数之间只有一个元素]时,这个代码才有效,如何通过动态编程改变k值来改善它.其中k是求和数之间的元素数.
例如:

list = [5,6,4,1,2] k = 1
答案= 11#5 4 2

list = [5,2] k = 2
答案= 8#6 2

list = [5,3,10,2] k = 1
答案= 15#5 10

解决方法

这是Ami Tavory描述的算法的快速实现(据我所知).它适用于任何序列,但如果列表全部为负数,则最大总和将为0(空子序列的总和).

import collections

def max_sum_separated_by_k(iterable,k):
    best = collections.deque([0]*(k+1),k+1)
    for item in iterable:
        best.appendleft(max(item + best[-1],best[0]))
    return best[0]

这使用O(k)空间和O(N)时间.所有deque操作,包括向一端附加值(并从另一端隐式删除一个以保持长度限制)和从末尾读取,都是O(1).

如果您希望算法返回最大子序列(而不仅仅是其总和),您可以将deque的初始化更改为以空列表而不是0开始,然后追加max([item] best [-1],最佳[0],key = sum)在循环体中.但这样效率会相当低,因为它会在整个地方添加O(N)操作.

python – 如何将两个列表的元素添加到一个列表中?extend

python – 如何将两个列表的元素添加到一个列表中?extend

例如,我有一个这样的列表:

list1 = [good, bad, tall, big]
list2 = [boy, girl, guy, man]

我想做一个这样的列表:

list3 = [goodboy, badgirl, tallguy, bigman]

我尝试过这样的东西:

list3=[]
list3 = list1 + list2

但这只会包含list1的值

所以我用于:

list3 = []
for a in list1:
    for b in list2:
        c = a + b
        list3.append(c)

但会导致列表太多(在这种情况下,4 * 4 = 16)

我该怎么办?任何帮助将是非常好的!

您可以使用列表推导与zip:

list3 = [a + b for a, b in zip(list1, list2)]

zip通过组合您提供的迭代元素来生成元组列表.所以在你的情况下,它会从list1和list2中返回成对的元素,直到首先被用尽.

还可以使用 extend

>>> x = [1,2,3]
>>> y = [4,5,6]
>>> x + y
[1, 2, 3, 4, 5, 6]
>>> x.extend(y)
>>> x
[1, 2, 3, 4, 5, 6] 

参考网址    https://cloud.tencent.com/developer/ask/35138 

今天关于根据元素的某些方面,如何将Python列表分为两个列表python把一个列表分成2个列表的讲解已经结束,谢谢您的阅读,如果想了解更多关于Dart:根据条件将列表分为两个子列表、Java:将一个列表分为两个子列表?、python – 列表元素的最大总和,每个列表元素由(至少)k个元素分隔、python – 如何将两个列表的元素添加到一个列表中?extend的相关知识,请在本站搜索。

本文标签: