What does the "x for x in" syntax mean? [duplicate] - python

This question already has answers here:
Explanation of how nested list comprehension works?
(11 answers)
Closed 5 years ago.
What actually happens when this code is executed:
text = "word1anotherword23nextone456lastone333"
numbers = [x for x in text if x.isdigit()]
print(numbers)
I understand, that [] makes a list, .isdigit() checks for True or False if an element of string (text) is a number. However I am unsure about other steps, especially: what does that "x" do in front of for loop?
I know what the output is (below), but how is it done?
Output: ['1', '2', '3', '4', '5', '6', '3', '3', '3']

This is just standard Python list comprehension. It's a different way of writing a longer for loop. You're looping over all the characters in your string and putting them in the list if the character is a digit.
See this for more info on list comprehension.

Related

Hwo to convert array into Comma Saperated string in python? [duplicate]

This question already has answers here:
How to convert integers in list to string in python
(6 answers)
Closed 2 years ago.
I Have following Array in python,
[4,5,6]
i want my output as ['4','5','6']
How to Do that?
Use map to convert list of integer to list string:
arr = [4,5,6]
arr = list(map(str,arr))
arr
Result :
['4', '5', '6']
I suppose you want your integers represented as strings, so i suggest a list comprehension:
alpha = [4,5,6]
alpha = [str(i) for i in alpha]
print(alpha)
Output:
['4', '5', '6']

Generating a list of consecutive numbers as strings [duplicate]

This question already has answers here:
Generate string of numbers python
(2 answers)
Closed 2 years ago.
I would like to generate a list of numbers such as this:
['1','2','3','4','5','6']
but all I can seem to get is:
[1, 2, 3, 4, 5, 6]
using this code:
values = list(range(1,7))
Is there some easy way to generate a list of consecutive numbers, that are within apostrophes, since I want the code to treat them like a string.
I have tried:
values = str(list(range(1,7)))
but that just gave me the same result as above.
>>> [str(i) for i in range(1,7)]
['1', '2', '3', '4', '5', '6']
The comprehension proposed by #CoryKramer has you covered, but given your attempts, you might be looking for:
>>> list(map(str, range(1, 7)))
['1', '2', '3', '4', '5', '6']
Try the following:
list(map(str, range(1,7)))

Searching a string of free text for integers and return a list of tuples [duplicate]

This question already has answers here:
How to get integer values from a string in Python?
(11 answers)
Find all numbers in a string in Python 3 [duplicate]
(2 answers)
extract digits in a simple way from a python string [duplicate]
(5 answers)
Closed 3 years ago.
I'm trying to search a list of strings for integers and return a list of tuples of the searched integers.
text = ['10 KM, REDUCING TO 6KM, IN PASSING SHOWERS', '10 KM, REDUCING TO 4000 M, IN PASSING SHOWERS', '2500M IN RAIN']
I've tried something like the following:
text_num = []
for i in text:
for x in i:
if x.isdigit() == True:
text_num.append(x)
which returns:
text_num = ['1', '0', '6', '1', '0', '4', '0', '0', '0', '2', '5', '0', '0']
Ideally, I'd like the output to be something like.
text_num = [(10, 6), (10, 4000), (2500)]
You can use regex
import re
[[int(num) for num in re.findall(r'[0-9]+', string)]
for string in text]
outputs
[[10, 6], [10, 4000], [2500]]

How to transfer range() output into string [duplicate]

This question already has answers here:
Generate string of numbers python
(2 answers)
Closed 4 years ago.
How to create a list of string using range() built-in function in python?
range(1,7) produces a range object equivilant to [1,2,3,4,5,6]
Desired list: ['1','2','3','4','5','6']
Use a cast to str() & list comprehension as per the following:
string_list = [str(x) for x in range(1, 7)]
With the map function:
list(map(str,range(7)))
Or the new f-strings:
>>> [f'{i}' for i in range(7)]
['0', '1', '2', '3', '4', '5', '6']
Use string formatting & list comprehension as per the following
string_list = ["{}".format(x) for x in range(1,7)]

How to prepend all list elements into another list [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Merge two lists in python?
How can I prepend list elements into another list?
A = ['1', '2']
B = ['3', '4']
A.append(B)
print A
returns
['1', '2', ['3', '4']]
How can I make it
['1', '2', '3', '4']?
A.extend(B)
or
A += B
This text added to let me post this answer.
list.extend, for example, in your case, A.extend(B).
this can also be done as
for s in B:
A.append(B)
of course A.extend(B) does the same work but using append we need to add in the above f

Categories