combining undefined lists to one list [duplicate] - python

This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed 3 years ago.
I'm scraping a website and the needed output is a list of floats.
when scraping I'm getting back lists of the the floats in str.
after converting them to floats I want to combine them to one list so i can iterate over it and write it to csv.
for statname in data['athletes']:
l = list(statname['categories'][1]['totals'][10:12])
ast = (l[0])
nast = []
nast.append(ast)
a = list(nast)
sas = list(map(float, a))
print(sas)
result:
[8.8] [6.3] [6.2] [7.6] [3.0][3.8]
needed:
[8.8, 6.3, 6.2, 7.6...]

This should work:
sas = []
for statname in data['athletes']:
l = list(statname['categories'][1]['totals'][10:12])
ast = (l[0])
nast = []
nast.append(ast)
a = list(nast)
sas = sas + list(map(float, a))
print(sas)

Try using:
nast = []
for statname in data['athletes']:
l = list(statname['categories'][1]['totals'][10:12])[0]
nast.append(l)
sas = list(map(float, nast))
print(sas)

Related

list of lists coordinates to a list of coordinates with space for SVG file [duplicate]

This question already has answers here:
How to concatenate (join) items in a list to a single string
(11 answers)
Apply function to each element of a list
(4 answers)
Closed 3 months ago.
I have a list
my_list = [[200.0, 10.0], [250.0, 190.0], [160.0, 210.0]]
I want get the list of these coordinate with space between them
req_list = "200,10 250,190 160,210"
to write these in SVG format for polygons.
I tried replacing "[]" with " " but replace doesn't work for an array
my_list.replace("[", " ")
You can iterate through the list and append them into an empty string defined, for example:
req_list = ""
for cor in my_list:
req_list += '{},{} '.format(int(cor[0]),int(cor[1]))
print(req_list[:-1])
Prints:
200,10 250,190 160,210
Indexed till -1 is to ignore the last white space.
You can use str.join to the sublists:
my_list = [[200.0, 10.0], [250.0, 190.0], [160.0, 210.0]]
req_list = " ".join(",".join(f"{int(v)}" for v in l) for l in my_list)
print(req_list)
Prints:
200,10 250,190 160,210

How do i append from one list to each item in another list [duplicate]

This question already has answers here:
How to get the cartesian product of multiple lists
(17 answers)
Closed 3 years ago.
I am new to python and am trying to create a new list from 2 other lists by appending each item in the list.
for number in num:
for names in name:
print(number+names)
num = [1,2,3,4,5]
name = ['Tom','Bob','Dave']
new_desired_list = [1Tom,1Bob,1Dave,2Tom,2Bob,2Data,3Tom,3Bob,3Dave..etc]
Seems like you want the cartesian product of both lists. For that you have itertools.product. In order to join the strings you could use string formatting:
from itertools import product
[f'{i}{j}' for i,j in product(num, name)]
# ['{}{}'.format(i,j) for i,j in product(num, name)] # for Python 3.6<
# ['1Tom', '1Bob', '1Dave', '2Tom', '2Bob'...
You could try appending a list ;)
l = []
numbers = [1,2,3,4,5]
names = ['Tom','Bob','Dave']
for number in numbers:
for name in names:
l.append(str(number) + str(name))
print(l)
Use list comprehension:
new_list = [str(i)+x for i in num for x in name]
Example:
>>> num = [1,2,3,4,5]
>>> name = ['Tom','Bob','Dave']
>>>
>>> new_list = [str(i)+x for i in num for x in name]
>>> new_list
['1Tom', '1Bob', '1Dave', '2Tom', '2Bob', '2Dave', '3Tom', '3Bob', '3Dave', '4Tom', '4Bob', '4Dave', '5Tom', '5Bob', '5Dave']
>>>

How to Interleave two strings in Python when their lengths are not equal? [duplicate]

This question already has answers here:
Is there a zip-like function that pads to longest length?
(8 answers)
Closed 3 years ago.
I know a way to interleave two strings using Python but it works only if their lengths are equal:
u = 'Abcd'
l = 'Wxyz'
res = "".join(i + j for i, j in zip(u, l))
print(res)
This will give me the correct Output:AWbxcydz
But if the strings are u = 'Utkarsh' and l = 'Jain', the same method does not give the correct answer. Can someone suggest a way to do so?
Use zip_longest from itertools.
from itertools import zip_longest
u = 'Abcdefgh'
l = 'Wxyz'
res = "".join(i + j for i, j in zip_longest(u, l, fillvalue=''))
print(res)

List of lists middle element extraction in python [duplicate]

This question already has answers here:
How to extract the n-th elements from a list of tuples
(8 answers)
Closed 4 years ago.
I have a list of lists as follows
[('trojan', 'virus', 0.4731800100841465), ('elb', 'Ebola', 0.3722390506633956)]
How to extract only the middle element i.e. 'virus' and 'Ebola' ?
You can use a comprehnesion list
your_list = [('trojan', 'virus', 0.4731800100841465), ('elb', 'Ebola', 0.3722390506633956)]
l = [x[1] for x in your_list]
output:
['virus', 'Ebola']
You have to find middle index
data = [
('trojan', 'virus', 0.4731800100841465),
('elb', 'Ebola', 0.3722390506633956)
]
middle_data = []
for inside_list in data:
middle = len(inside_list)/2
middle_data.append(inside_list[middle])
print(middle_data)
Output: ['virus', 'Ebola']

List Comprehension - Python [duplicate]

This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed 5 years ago.
I'm trying iterate over lists in Python, but can't do it in an economic and pythonic way.
I can do in one dimension list:
api_time = ['hour', 'time']
text = 'What time is it?'
request = [r for r in api_time if r in text]
This outputs 'time', which is correct.
But when I start using two dimension list, I can't do it in one sentence.
api_time = ['hour', 'time']
api_weather = ['clime', 'rain']
apis = [api_time, api_weather]
How can I check this in one sentence?
You can use a double list comprehension:
>>> api_time = ['hour', 'time']
>>> api_weather = ['clime', 'rain']
>>>
>>> apis = [api_time, api_weather]
>>> text = 'What time is it?'
>>> [r for api in apis for r in api if r in text]
['time']
Or a simple list addition instead of nested lists:
>>> [r for r in api_time + api_weather if r in text]
['time']

Categories

Resources