List of lists middle element extraction in python [duplicate] - python

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']

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

combining undefined lists to one list [duplicate]

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)

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']
>>>

Nested list in pairs from list [duplicate]

This question already has answers here:
Split by comma and strip whitespace in Python
(10 answers)
Closed 4 years ago.
Input list example = ['listen, silent', 'dog, fog', 'colour, simple']
how do I return a nested list from the example in pairs, to look like this:
[[word1,word2], [word3,word4]...etc]
please, thank you
I have tried list comprehension,
my_list1 = [i[1] for i in my_list]
my_list2 = [i[0] for i in my_list]
but it took out only the first letter instead of word... hoping for it to look like;
[listen, silent],[dog, fog]...etc
You can split each word in the list using , as a separator:
l = ['listen, silent', 'dog, fog', 'colour, simple']
l = [elem.split(', ') for elem in l]
print(l)
Output:
[['listen', 'silent'], ['dog', 'fog'], ['colour', 'simple']]

python remove alphanum and numbers elements in a list [duplicate]

This question already has answers here:
Remove all items in Python list containing a number [duplicate]
(2 answers)
Remove strings from a list that contains numbers in python [duplicate]
(6 answers)
Check if a string contains a number
(20 answers)
Closed 4 years ago.
How do I remove alphanum and numbers elements in a list? Below code is not removing, what am I doing wrong here? After research in other stackoverflow, they are removing characters but not the elements itself.
ls = ['1a', 'b3', '1.45','apples','oranges','mangoes']
cleaned = [x for x in ls if x is not x.isalnum() or x is not x.isdigit()]
cleaned
result = re.sub(r'[^a-zA-Z]', "", ls)
print(result) #expected string or bytes-like object
output should be:
['apples','oranges','mangoes']
enter code here
Try this:
ls = ['1a', 'b3', '1.45','apples','oranges','mangoes']
[l for l in ls if l.isalpha()]
Output:
['apples', 'oranges', 'mangoes']
try this:-
ls = ['1a', 'b3', '1.45','apples','oranges','mangoes']
l = []
for i in ls:
if not bool(re.search(r'\d', i)):
l.append(i)
print(l)
I'd do it like this:
newList = []
for x in ls:
if x.isalpha():
newList.append(x)
print(newList)
It works for me. It only adds the element to the new list if they don't contain a number.

Categories

Resources