text sublistas_jupyter_notebook.ipynb
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了text sublistas_jupyter_notebook.ipynb相关的知识,希望对你有一定的参考价值。
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Qual é a maneira mais Pythônica ou ter um código fácil de entender e que resolva:\n",
"\n",
"Tenho uma lista com sub-listas:\n",
"\n",
"```python\n",
"items = [['a','b'], ['c','d'], ['e', None]]\n",
"```\n",
"\n",
"\n",
"Quero como resultado remover as sub-listas:\n",
"\n",
"```python\n",
"resultado = ['a','b', 'c','d', 'e', None]\n",
"```\n",
"\n",
"IMPORTANTE:\n",
"- a lista de items será pequena, logo performance não é problema!\n",
"- \"Readability counts\" The Zen of Python\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"\n",
"items = [['a','b'], ['c','d'], ['e', None]]\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Tentativa 1\n",
"=======\n",
"Usar 'List Comprehensions' com 1 for dentro de outro é pythônico? É fácil de entender?"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['a', 'b', 'c', 'd', 'e', None]\n"
]
}
],
"source": [
"\n",
"tentativa1 = [item for sublist in items for item in sublist]\n",
"print(tentativa1)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Tentativa 2\n",
"===========\n",
"Usar itertools"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['a', 'b', 'c', 'd', 'e', None]\n",
"['a', 'b', 'c', 'd', 'e', None]\n"
]
}
],
"source": [
"\n",
"from itertools import chain\n",
"tentativa2 = list(chain(*items))\n",
"print(tentativa2)\n",
"\n",
"# ou\n",
"\n",
"tentativa2 = list(chain.from_iterable(items))\n",
"print(tentativa2)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Tentativa 3\n",
"===========\n",
"Usar a Built-in `sum(iterable [, start])` do Python. \n",
"Parece simples, não precisa de import. Mas este [] como segundo parametro?"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['a', 'b', 'c', 'd', 'e', None]\n"
]
}
],
"source": [
"\n",
"tentativa3 = sum(items, [])\n",
"print(tentativa3)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Tentativa 4\n",
"===========\n",
"Programação funcional?\n",
"uma vez que `['a', 'b'] + ['c', 'd']` tem como resultado `['a', 'b', 'c', 'd']`\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['a', 'b', 'c', 'd', 'e', None]\n"
]
}
],
"source": [
"\n",
"from functools import reduce\n",
"tentativa4 = reduce(lambda x, y: x+y, items)\n",
"print(tentativa4)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"Tem mais formas? \n",
"```\n",
"\"There should be one-- and preferably only one --obvious way to do it.\"\n",
"```\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
以上是关于text sublistas_jupyter_notebook.ipynb的主要内容,如果未能解决你的问题,请参考以下文章
VB中 如何复制Text1.text的字体到剪贴板? 如何剪切?
text-decoration:[ text-decoration-line ] || [ text-decoration-style ] || [ text-decoration-color ]
javascript或css:如何隐藏标签内的文本中的任何数字,后跟点前缀“1.text”,“2.text”...“30.text”