简体   繁体   English

从旧列表中删除时将第 n 个元素移动到新列表

[英]Move nth element to new list while removing from old list

Other posts did not fully answer my particular problem.其他帖子没有完全回答我的特定问题。 Im trying to move every 3rd (2nth?) element into a new list but somehow it does not seem to consistently do that.我试图将每个第 3 个(第 2 个?)元素移动到一个新列表中,但不知何故,它似乎并没有始终如一地这样做。 I tried to understand it with 0, 1, 2,... unfortunately it seems to me that it does not move it that way.我试图用 0, 1, 2,... 来理解它,不幸的是,在我看来,它并没有那样移动它。 Here is my code:这是我的代码:

test = ['2 – 4\nZimmer', '47,86 – 189,23 m²\nFläche', 'Preis auf Anfrage', '3 – 3.5\nZimmer', '63,43 – 158,34 m²\nFläche', 'ab 550.000 €', '2 – 4\nZimmer', '65,5 – 121,86 m²\nFläche', 'ab 530.000 €', '1 – 4\nZimmer', '31,99 – 78,26 m²\nFläche', 'ab 140.000,52 €', '2 – 4\nZimmer', '36,8 – 101,94 m²\nFläche', 'ab 225.722,2 €', '1 – 3\nZimmer', '44,49 – 92,75 m²\nFläche', 'ab 278.000 €', '2 – 4\nZimmer', '47,19 – 99,35 m²\nFläche', 'ab 284.900 €', '3 – 3\nZimmer', '80,91 – 95,91 m²\nFläche', 'ab 579.000 €', '1 – 2\nZimmer', '38,19 – 45,79 m²\nFläche', 'ab 252.800,09 €', '2 – 3\nZimmer', '34,77 – 74,26 m²\nFläche', 'ab 247.000 €', '2 – 6\nZimmer', '63,35 – 475,18 m²\nFläche', 'ab 790.000 €', '2 – 4\nZimmer', '46,4 – 219,38 m²\nFläche', 'ab 414.760 €', '2 – 3\nZimmer', '57,86 – 76,84 m²\nFläche', 'ab 499.000 €', '1 – 4.5\nZimmer', '33,56 – 112,72 m²\nFläche', 'ab 173.400 €', '3 – 7\nZimmer', '92,05 – 215,9 m²\nFläche', 'ab 1.265.000 €', '1 – 3\nZimmer', '33,35 – 75,89 m²\nFläche', 'ab 231.500 €', '1 – 4\nZimmer', '37,9 – 90,33 m²\nFläche', 'ab 241.400 €', '1 – 5\nZimmer', '30,06 – 107,13 m²\nFläche', 'ab 199.900 €', '2 – 3\nZimmer', '42,41 – 46,04 m²\nFläche', 'ab 229.193,39 €', '3 – 3\nZimmer', '69,07 – 69,07 m²\nFläche', 'Preis auf Anfrage', '3 – 5\nZimmer', '68,55 – 218,16 m²\nFläche', 'ab 608.800 €',]
newl = test[::2]
del test[::2]

print(test)
print(newl)

Also tried this:也试过这个:

new = [2::2]

Looks better but it still takes one extra element which I do not need.看起来更好,但它仍然需要一个我不需要的额外元素。

Overall I'll have to split the "test" list into 3 different Lists pos 0, 1 and 2总的来说,我必须将“测试”列表分成 3 个不同的列表,分别为 0、1 和 2

Any suggestions / Solutions?有什么建议/解决方案吗? I appreciate any help and directions.我感谢任何帮助和指示。

Found the solution!找到了解决方案!

new = [2::3]

This takes now every 3rd element, I seemingly misunderstood the spacing part.现在需要每 3 个元素,我似乎误解了间距部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM