首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >python将数组拆分为2个数组

python将数组拆分为2个数组
EN

Stack Overflow用户
提问于 2019-01-09 18:18:26
回答 1查看 74关注 0票数 2

我试着把这个数组中的信息分开。它有标题和链接

第一部分是标题,最后是网址。

代码语言:javascript
复制
 u'6 Essential Tips on How to Become a Full Stack Developer', 
 u'https://hackernoon.com/6-essential-tips-on-how-to-become-a-full- 
   stack-developer-1d10965aaead'

 u'6 Essential Tips on How to Become a Full Stack Developer', 
 u'https://hackernoon.com/6-essential-tips-on-how-to-become-a-full- 
   stack-developer-1d10965aaead'

 u'What is a Full-Stack Developer? - Codeup', 
 u'https://codeup.com/what-is-a-full-stack-developer/'

 u'A Guide to Becoming a Full-Stack Developer in 2017 \u2013 
   Coderbyte ...',  
 u'https://medium.com/coderbyte/a-guide-to- 
   becoming-a-full-stack-developer-in-2017-5c3c08a1600c'

我想要能够推动标题在一个列表,以及链接在一个列表。而不是把所有的东西都列在一张清单上

这是我目前的代码

main.py

代码语言:javascript
复制
from gsearch.googlesearch import search
from orderedset import OrderedSet
import re
import csv

results = search('Full Stack Developer')  # returns 10 or less 
          results

myAraay = list()

for x in results:
   owl = re.sub('[\(\)\{\}<>]', '', str(x))
   myAraay.append(owl)


newArray = "\n".join(map(str, myAraay))

print(newArray)

更新的Main.py (现在无法连接'str‘和'int’对象)

代码语言:javascript
复制
from gsearch.googlesearch import search
from orderedset import OrderedSet
import re
import csv

results = search('Full Stack Developer')  # returns 10 or less results

myAraay = list()

for x in results:
    owl = re.sub('[\(\)\{\}<>]', '', str(x))
    myAraay.append(owl)



newArray = "\n".join(map(str, myAraay))

theDict = {}                                     #keep a dictionary for title:link
for idx, i in enumerate(newArray):           #loop through list
    if idx % 2 == 0:                             #the title goes first...
        dict[i] = dict[i+1]                      #add title and link to dictionary
    else:                                        #link comes second
         continue                                #skip it, go to next title
print(theDict)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-09 18:41:34

假设列表是格式化的,所以链接总是继承标题.

代码语言:javascript
复制
theDict = {}                                     #keep a dictionary for title:link
for idx, i in enumerate(OriginalList):           #loop through list
    if idx % 2 == 0:                             #the title goes first...
        dict[i] = dict[i+1]                      #add title and link to dictionary
    else:                                        #link comes second
         continue                                #skip it, go to next title

看上去就像

代码语言:javascript
复制
{"How to be Cool" : "http://www.cool.org/cool"}

如果你想要列表而不是字典,那也是一样的.

代码语言:javascript
复制
articles = []                                    #keep a lists
for idx, i in enumerate(OriginalList):           #loop through list
    if idx % 2 == 0:                             #the title goes first...
        articles.append([i,i+1])                   #add title and link to list
    else:                                        #link comes second
         continue                                #skip it, go to next title

所以这里看起来是:

代码语言:javascript
复制
[["How to be Cool", "http://www.cool.org/cool"]]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54116120

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档