i need help with this function for split a list of filesname with add for each file the site
#!/usr/bin/python3
site = 'http://example.com/'
files = ['Steve.php','Jane.php','Sara.php','Mary.php','Jack.php','Bob.php']
def chunks(l, n):
for i in range(0, len(l), n):
yield l[i:i n]
result = list(chunks(files, 2))
print (result)
>> [['Steve.php', 'Jane.php'], ['Sara.php', 'Mary.php'], ['Jack.php', 'Bob.php']]
but i need like here
1) ',' and not ', ' ( without space )
and
2) >> [['http://example.com/Steve.php', 'http://example.com/Jane.php'], ['http://example.com/Sara.php', 'http://example.com/Mary.php'], ['http://example.com/Jack.php', 'http://example.com/Bob.php']]
how i can edit this function ?
Thanks
CodePudding user response:
Thanks so much for ur help, working fine, but still i dont know how to fix this:
['http://example.com/Steve.php', 'http://example.com/Jane.php']
['http://example.com/Steve.php','http://example.com/Jane.php'] without space
CodePudding user response:
I'm not certain but perhaps this is what you want:
site = 'http://example.com/'
files = ['Steve.php','Jane.php','Sara.php','Mary.php','Jack.php','Bob.php']
output = [[site files[i], site files[i 1]] for i in range(0, len(files), 2)]
print(repr(output).replace(' ', ''))
Output:
[['http://example.com/Steve.php','http://example.com/Jane.php'],['http://example.com/Sara.php','http://example.com/Mary.php'],['http://example.com/Jack.php','http://example.com/Bob.php']]
Note:
This will fail if the files list has an odd number of elements
CodePudding user response:
site = 'http://example.com/'
files = ['Steve.php','Jane.php','Sara.php','Mary.php','Jack.php','Bob.php']
files = [site file for file in files]
files = [files[i:i 2] for i in range(len(files)) if i%2 == 0]
The code above should do the job! I don't know what you mean by the requirement
',' and not ', '
That is simply how python outputs the file.
