如何将字符串和URL分成列表中的不同列表

时间:2019-04-23 13:27:33

标签: python python-3.x

问候。我是python初学者。我想将包含字符串和URL的列表拆分为不同的列表。我已经尝试了一些解决方案,但仍然无法解决。谢谢你。

我曾经为列表应用名为URLExtract的程序包,但它没有返回任何值。

print(results)

[['1.', 'Oases | Define Oases at Dictionary.com\n  https://www.dictionary.com/browse/oases'],  
['2.', 'oases - Yahoo Dictionary\n  https://dictionary.yahoo.com/dictionary?p=oases'], 
['3.', 'OASES Cambridge Dictionary\n  https://dictionary.cambridge.org/zht/%E8%A9%9E%E5%85%B8/%E8%8B%B1%E8%AA%9E/oases'], 
['4.', 'Oases - Wikipedia\n  https://en.wikipedia.org/wiki/Oasis'], 
['5.', 'Oases definition and meaning | Collins English Dictionary\n  https://www.collinsdictionary.com/dictionary/english/oases'], 
['6.', 'OASES - YouTube\n  https://www.youtube.com/watch?v=K5gJVgIZgYQ'], 
['7.', 'Oases - definition of oases by The Free Dictionary\n  https://www.thefreedictionary.com/oases'], 
['8.', 'Oases – Correct Spelling – Grammarist\n  https://grammarist.com/spelling/oases/'], 
['9.', 'Online Tutoring Management & Scheduling Software | Oases\n  https://oasesonline.com/'], 
['10.', 'OASES – Log in\n  https://oases.wageningenacademic.com/']]

预期产量

print(intro)
1. Oases | Define Oases at Dictionary.com
2. oases - Yahoo Dictionary
3. OASES Cambridge Dictionary
4. Oases - Wikipedia
...
10. OASES – Log in

print(urls)
https://www.dictionary.com/browse/oases
https://dictionary.yahoo.com/dictionary?p=oases
https://dictionary.cambridge.org/zht/%E8%A9%9E%E5%85%B8/%E8%8B%B1%E8%AA%9E/oases
https://en.wikipedia.org/wiki/Oasis
...
https://oases.wageningenacademic.com/

4 个答案:

答案 0 :(得分:0)

在您的情况下,您可以使用finally

例如:

str.splitlines()

输出:

data = [['1.', 'Oases | Define Oases at Dictionary.com\n  https://www.dictionary.com/browse/oases'],  
['2.', 'oases - Yahoo Dictionary\n  https://dictionary.yahoo.com/dictionary?p=oases'], 
['3.', 'OASES Cambridge Dictionary\n  https://dictionary.cambridge.org/zht/%E8%A9%9E%E5%85%B8/%E8%8B%B1%E8%AA%9E/oases'], 
['4.', 'Oases - Wikipedia\n  https://en.wikipedia.org/wiki/Oasis'], 
['5.', 'Oases definition and meaning | Collins English Dictionary\n  https://www.collinsdictionary.com/dictionary/english/oases'], 
['6.', 'OASES - YouTube\n  https://www.youtube.com/watch?v=K5gJVgIZgYQ'], 
['7.', 'Oases - definition of oases by The Free Dictionary\n  https://www.thefreedictionary.com/oases'], 
['8.', 'Oases – Correct Spelling – Grammarist\n  https://grammarist.com/spelling/oases/'], 
['9.', 'Online Tutoring Management & Scheduling Software | Oases\n  https://oasesonline.com/'], 
['10.', 'OASES – Log in\n  https://oases.wageningenacademic.com/']]

intros = []
urls = []
for i in data:
    intro, url = i[1].splitlines()
    intros.append(intro.strip())
    urls.append(url.strip())

print(intros)
print(urls)

答案 1 :(得分:0)

您可以通过以下操作来实现此目的:遍历列表,然后拆分行(索引为1,仅包括所需列表的一部分):

intro = []
urls = []
for entry in results:
  intro_res, url_res = entry[1].split('\n')
  intro.append(intro_res)
  urls.append(url_res.lstrip()) # We lstrip here because you have whitespace

输出:

['Oases | Define Oases at Dictionary.com',
 'oases - Yahoo Dictionary',
 'OASES Cambridge Dictionary',
 'Oases - Wikipedia',
 'Oases definition and meaning | Collins English Dictionary',
 'OASES - YouTube',
 'Oases - definition of oases by The Free Dictionary',
 'Oases - Correct Spelling - Grammarist',
 'Online Tutoring Management & Scheduling Software | Oases',
 'OASES - Log in']

['https://www.dictionary.com/browse/oases',
 'https://dictionary.yahoo.com/dictionary?p=oases',
 'https://dictionary.cambridge.org/zht/%E8%A9%9E%E5%85%B8/%E8%8B%B1%E8%AA%9E/oases',
 'https://en.wikipedia.org/wiki/Oasis',
 'https://www.collinsdictionary.com/dictionary/english/oases',
 'https://www.youtube.com/watch?v=K5gJVgIZgYQ',
 'https://www.thefreedictionary.com/oases',
 'https://grammarist.com/spelling/oases/',
 'https://oasesonline.com/',
 'https://oases.wageningenacademic.com/']

答案 2 :(得分:0)

>>> data = [
['1.', 'Oases | Define Oases at Dictionary.com\n  https://www.dictionary.com/browse/oases'],  
['2.', 'oases - Yahoo Dictionary\n  https://dictionary.yahoo.com/dictionary?p=oases'], 
['3.', 'OASES Cambridge Dictionary\n  https://dictionary.cambridge.org/zht/%E8%A9%9E%E5%85%B8/%E8%8B%B1%E8%AA%9E/oases'], 
['4.', 'Oases - Wikipedia\n  https://en.wikipedia.org/wiki/Oasis'], 
['5.', 'Oases definition and meaning | Collins English Dictionary\n  https://www.collinsdictionary.com/dictionary/english/oases'], 
['6.', 'OASES - YouTube\n  https://www.youtube.com/watch?v=K5gJVgIZgYQ'], 
['7.', 'Oases - definition of oases by The Free Dictionary\n  https://www.thefreedictionary.com/oases'], 
['8.', 'Oases – Correct Spelling – Grammarist\n  https://grammarist.com/spelling/oases/'], 
['9.', 'Online Tutoring Management & Scheduling Software | Oases\n  https://oasesonline.com/'], 
['10.', 'OASES – Log in\n  https://oases.wageningenacademic.com/']]
>>>
>>> intro = [i[1].splitlines()[0] for i in data]
>>> urls = [i[1].splitlines()[1].strip() for i in data]
>>>
>>> print(intro)
['Oases | Define Oases at Dictionary.com',
 'oases - Yahoo Dictionary',
 'OASES Cambridge Dictionary',
 'Oases - Wikipedia',
 'Oases definition and meaning | Collins English Dictionary',
 'OASES - YouTube',
 'Oases - definition of oases by The Free Dictionary',
 'Oases – Correct Spelling – Grammarist',
 'Online Tutoring Management & Scheduling Software | Oases',
 'OASES – Log in']
>>>
>>> print(urls)
['https://www.dictionary.com/browse/oases',
 'https://dictionary.yahoo.com/dictionary?p=oases',
 'https://dictionary.cambridge.org/zht/%E8%A9%9E%E5%85%B8/%E8%8B%B1%E8%AA%9E/oases',
 'https://en.wikipedia.org/wiki/Oasis',
 'https://www.collinsdictionary.com/dictionary/english/oases',
 'https://www.youtube.com/watch?v=K5gJVgIZgYQ',
 'https://www.thefreedictionary.com/oases',
 'https://grammarist.com/spelling/oases/',
 'https://oasesonline.com/',
 'https://oases.wageningenacademic.com/']

我使用List Comprehensions完成了此操作。

答案 3 :(得分:-1)

使用splitlines拆分URL及其相关信息应该可以在此处解决问题:

info = []
urls = []
for entry in data: 
    url_info = entry[1].splitlines()    # Split info and URL
    info.append('{} {}'.format(entry[0], url_info[0]))      # Create a new list of info  
    urls.append(url_info[1].strip())    # Create a new list of urls

print(info)
print(urls)