我有一个这样的清单:
[['zai4'], [' '], ['tui1'], ['jin4'], [' '], ['shi2'], ['pin3'], [' '], ['an1'], ['quan2'], [' '], ['xin4'], ['xi1'], [' ']]
我怎么能把它转换成这种:
zai4 tui1 jin4 shi2 pin3 an1 quan2 xin4 xi1
谢谢。
我认为我的问题的独特部分是如何从中提取内容 子列表到字符串。
答案 0 :(得分:0)
使用session_unset($_SESSION['session_name']);
和list comprehension
str.join
<强>输出:强>
a = [['zai4'], [' '], ['tui1'], ['jin4'], [' '], ['shi2'], ['pin3'], [' '], ['an1'], ['quan2'], [' '], ['xin4'], ['xi1'], [' ']]
print(" ".join(i[0] for i in a).replace(" ", ""))
答案 1 :(得分:0)
尝试这种方法:
$ ./bin/asciiencrypt3
mode: EnCRypt
msg : hello
Enter a number that is 5 digits long: 11111
res : ifmmp
输出:
data=[['zai4'], [' '], ['tui1'], ['jin4'], [' '], ['shi2'], ['pin3'], [' '], ['an1'], ['quan2'], [' '], ['xin4'], ['xi1'], [' ']]
print([i[0] for i in data if i[0].isalnum()])
如果你想要没有列表,那么:
['zai4', 'tui1', 'jin4', 'shi2', 'pin3', 'an1', 'quan2', 'xin4', 'xi1']
输出:
print(" ".join([i[0] for i in data if i[0].isalnum()]))
答案 2 :(得分:0)
您可以使用解包:
s = [['zai4'], [' '], ['tui1'], ['jin4'], [' '], ['shi2'], ['pin3'], [' '], ['an1'], ['quan2'], [' '], ['xin4'], ['xi1'], [' ']]
new_s = ' '.join(a for [a] in s if a != ' ')
输出:
'zai4 tui1 jin4 shi2 pin3 an1 quan2 xin4 xi1'