Matching of element in List [duplicate] - python

This question already has answers here:
Check if multiple strings exist in another string
(17 answers)
Closed last month.
How can I check a string for substrings contained in a list, like in Check if a string contains an element from a list (of strings), but in Python?

Try this test:
any(substring in string for substring in substring_list)
It will return True if any of the substrings in substring_list is contained in string.
Note that there is a Python analogue of Marc Gravell's answer in the linked question:
from itertools import imap
any(imap(string.__contains__, substring_list))
In Python 3, you can use map directly instead:
any(map(string.__contains__, substring_list))
Probably the above version using a generator expression is more clear though.

Related

Can I use values from an array to complete an if statement? [duplicate]

This question already has answers here:
How do I do a case-insensitive string comparison?
(15 answers)
Closed last year.
import time
while True:
npc=input("Josuke\n\nJotaro Kujo (Part 4)\n\nChoose NPC to talk to.")
if npc=="Josuke" or npc=="josuke":
confirm=input("[Press E to interact.]")
elif npc=="jp4" or npc=="JP4" or npc=="Jp4
Within this code you can see that there are 2 NPCs to interact to. Because there are many ways of addressing the name Jotaro Kujo (Part 4), the if statement has many "or"s, but I want to be able to condense it. Could I use an array to be able to put the possibilities in and then have the if statement identify if the value is within the array? (I haven't completed the code yet, but the problem doesn't require the full code to be completed.)
Yes, you can do it easily using the in operator to check if a particular string is in the list.
as an example:
lst = ["bemwa", "mike", "charles"]
if "bemwa" in lst:
print("found")
And if all the possibilities you want to cover are related to case-insensitivity you can simply convert the input to lower-case or upper-case and compare it with only one possibility.

How to split string before a character then have python create a list from it [duplicate]

This question already has answers here:
How can I split and parse a string in Python? [duplicate]
(3 answers)
Closed 1 year ago.
I am trying to make python take a string, separate the characters before another character eg: "10001001010Q1002000293Q100292Q". I want to separate the string before each Q and have python create either a list or another string. I cannot figure this out for the life of me.
You can do this using the split function, give "Q" as a parameter to the split function then you can slice the list to only get the numbers before Q.
num = "10001001010Q1002000293Q100292Q"
print(num.split("Q")[:-1])
Split() function: https://www.w3schools.com/python/ref_string_split.asp
Slicing: https://www.w3schools.com/python/python_strings_slicing.asp
The syntax is str.split("separator").
str = str.split("Q")
Then output will be ['10001001010', '1002000293', '100292', ''].
If you don't need the last empty element then you can write as:
str = str.split("Q")[:-1]

Python converting a list of strings to unicode [duplicate]

This question already has answers here:
How to detect string byte encoding?
(4 answers)
What is the difference between encode/decode?
(7 answers)
Closed last month.
I have a list of strings that originally was a list of unicode element. And so, some string contains some character accent in unicode formate.
list=['Citt\xe0','Veszpr\xe9m','Publicit\xe0']
I need to get a new list that looks like this:
new_list=[u'Citt\xe0',u'Veszpr\xe9m',u'Publicit\xe0']
Each element of the new_list has to carry both the u and the accent.
Is there a way to do it iterating on each element?
new_list=[unicode(repr(word)) for word in old_list]
>>> print new_list
[u"'Citt\\xe0'", u"'Hello'", u"'Publicit\\xe0'"]
Is that what you want?

How can i combine all the contents of a List to one value Python [duplicate]

This question already has answers here:
Converting a list to a string [duplicate]
(8 answers)
Closed 6 years ago.
If I were to get the input of someone and put it into a list. How would I combine this into one big string.
user_input = input()
listed = list(user_input)
I am having trouble with this since the contents are unknown. Is there anyway to make it one big string again(combining all the contents of the list). Is there anything I can import into my code to do this for me
To join a list together, you can use the join method. Simply use it as a method on whatever string you want to have placed between each entry in the list:
>>> ls = ['Hello,','world!']
>>> ' '.join(ls)
'Hello, world!'

Convert string of list to python list [duplicate]

This question already has answers here:
Converting a string to a tuple in python
(3 answers)
Closed 8 years ago.
I downloaded a csv table from a database using SQL. One of the fields has values like so:
'[0.1234,0.0,0.0]'
I want to convert this string to a python list to get the first value. I only know how to convert strings to ints and floats... is there any way to de-string this object? The table I got from SQL is from a web-based viewer, I'm not getting it from my command line.
You could take the substring from index 1 to index -1 and then split it using the comma as a delimiter. In python
array = variable[1:-1].split(',')
should work.
If you're sure it is always valid list syntax, you could use"
myList = eval('[0.1234,0.0,0.0]')
Or if the value itself has quotes ' in it, you can slice those off
value = "'[0.1234,0.0,0.0]'"
myList = eval(value[1:-1])
Then to get the first value you just
myList[0]

Categories

Resources