简体   繁体   English

如何在 python 中将多行列表排序为单行列表

[英]How to sort a multi-line list to a single-line list in python

How would you take an output from the concatenation of two lists and put it into one list.您如何从两个列表的串联中取出 output 并将其放入一个列表中。 For example-例如-

example1 = ["hello", "banana", "skit"]   
example2 = ["rodeo", "pine", "sky", "feel"]   
joined_list = example1 + example2

But the outcome is like this-但结果是这样的——

[ "hello" ,   
  "banana" ,   
  "skit" ,   
  "rodeo" ,   
  "pine" ,   
  "sky" ,   
  "feel" ]

I need it to be in a one line list, so the output should look like-我需要它在一行列表中,所以 output 应该看起来像 -

["hello", "banana", "skit", "rodeo", "pine", "sky", "feel"]

If someone also knows how to sort this list based off highest string length to lowest, so it ends up looking like-如果有人也知道如何根据最高字符串长度对这个列表进行排序,那么它最终看起来像 -

["banana", "hello", "rodeo", "feel", "pine", "skit", "sky"]

Any help into properly formatting this would be much appreciated!任何帮助正确格式化这将不胜感激!

use sort , on joined_list在joined_list上使用sort

example1 = ["hello", "banana", "skit"]   
example2 = ["rodeo", "pine", "sky", "feel"]   
joined_list = example1 + example2
joined_list.sort(key=len, reverse=True)
print(joined_list)

output output

["banana", "hello", "rodeo", "feel", "pine", "skit", "sky"]

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

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