How do I convert string to list in Python? [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
How do I convert def stringParametre(x) x to a list so that if x is hello then it will become a list like ["H","E","l","l","O"] . Capitals don't matter .

Note that you do not have to convert to a list if all you want to do is to iterate over the characters of the string:
for c in "hello":
# do something with c
works

Building on #idjaw's comment:
def stringParametre(x):
return list(x)
Of course this will have an error if x is not a string (or other sequence type).

list(x)
OR
mylist = []
for c in x:
mylist.append(c)

Related

Turn a string of a list to an integer in Python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I have an output of a = "[1,2,3]", how do I convert it to an array a = [1,2,3] from the string in Python?
Thanks!
Try this:
import json
result = json.loads("[1,2,3]")
print(result)
Use ast and then ast.literal_eval()
import ast
print(ast.literal_eval(x))
You can do it without importing stuff
lst = []
for index in range(len(a)):
if a[index].isdigit():
lst.append(int(a[index]))
Then lst will be a list containing all the integers in string a. And if you want to keep the variable name "a", you can
a = lst [::]

How to match a string to pattern "Foo-Bar", where Bar can be any element of a list? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a list of strings L.
I need to check, whether a string is either directly an element of L or is in this format: "foo-element_of_L"
Is there a better way to do this in python than adding "foo-X" to L for all X in L?
I would do two lookups:
if x in L or f'foo-{x}' in L:
which may be significantly faster than
if any(x == y or f'foo-{x}' == y for x in L):
which is essentially what you were proposing.

Sorting a list in python based on the last number in each string entry [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have a list in python of the following form:
myList = ['r0x94', 'r0x21', 'r0x51']
I want to sort it based on the last number in each string entry of the list such that:
sorted_myList = ['r0x21', 'r0x51', 'r0x94']
The last number is not hex, rather it is decimal. How to do it?
>>> my_list = ['r0x94', 'r0x21', 'r0x51']
>>> sorted(my_list, key=lambda x: int(x.rpartition('x')[-1]))
['r0x21', 'r0x51', 'r0x94']

Getting values from a dict in 2nd field [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
For example, I have this:
alphabetValues = {"a":1,"b":2,"c":3,"d":4,"e":5,"f":6,"g":7...
Is it possible if instead of having:
print(alphabetValues["c"])
To having something that would get "e" if I searched for 5 in a dict.
"e":5
Thanks in advance.
As suggested by jonrsharpe, you need to reverse your dictionnary :
alphabetValues = {"a":1,"b":2,"c":3,"d":4,"e":5,"f":6,"g":7}
revalpha={v:k for k,v in alphabetValues.iteritems()}
>>> revalpha[5]
'e'
Why not set up an alphabet list?
alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
print(alphabet[0]) #will print out 'a'
print(alphabet[25]) #will print out 'z'
Note that all values are 1 less than expected.

How do i check whether my list contains any symbols or numbers on python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am making a code breaker game on python and need to make a if statement that checks whether the list contains any symbols or numbers.
So it would tell me that:
list=[hello, bonjour, 4 , hola]`
contains a number. And:
list2=[hello, bonjour, hola]
does not contain a number
Just use:
>>> test = 'abc123%def'
>>> any(not x.isalpha() for x in test)
True
>>> test2 = 'abc'
>>> any(not x.isalpha() for x in test2)
False

Categories

Resources