好的,所以我有两个列表,它们可以是从一个值长到20的任何位置,但它们总是具有相同的数量。
例如
alphabet = ['a', 'b', 'c', 'd', 'e']
numbers = ['1', '2', '3', '4', '5']
现在我的目标是创建一个遍历两个列表的for循环,并将每个列表中的相应值相互添加。所以..
['a1', 'b2', 'c3', 'd4', '5e']
再举一个例子。
names = ['john', 'harry', 'joe']
IDs = ['100', '200', '300']
output: ['john100', 'harry200', 'joe300']
有人能指出我正确的方向吗?
答案 0 :(得分:3)
您可以使用from selenium import webdriver
driver = webdriver.Firefox()
driver.get('http://www.website.com')
source = driver.page_source
location = source.find('<p class="info"')
source = source[location+16:] #adjust the +16 to maybe +15 or w/e depending on the exact source page you get
location_second = source.find('viewers on') #assuming there is a space between the actual number of viewers and the
source = int(source[:location_second-1]) #adjust the -1 to maybe -2 or w/e depending on the exact source page you get
if source > x: # replace x with whatever number is your minimum viewers
driver.find_element_by_class_name('js-profile-link') #might need to use x-path if you have multiple instances of the same class name
和zip
:
join
至于第二个例子:
[''.join(p) for p in zip(alphabet, numbers)]
# ['a1', 'b2', 'c3', 'd4', 'e5']
答案 1 :(得分:1)
作为@Psidom单线程解决方案的替代方案,您可以使用zip()
:
>>>[i+j for i, j in zip(alphabet, numbers)]
>>>['a1', 'b2', 'c3', 'd4', 'e5']
或者如果你完全使用常规for循环:
res = []
for i, j in zip(alphabet, numbers):
res.append(i+j)
你也可以把它变得更通用,并把它放在一个函数中:
# method one
def concancate_elements(list1, list2):
return [i+j, for i, j in zip(alphabet, numbers)]
# method two
def concancate_elements(list1, list2):
res = []
for i, j in zip(alphabet, numbers):
res.append(i+j)
return res