Compare 2 lists and output the similar matches [closed] - python

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 months ago.
Improve this question
I have 2 Python lists :
prefixList = ["12","9"]
files = ["12-a.csv","12-b.csv","9-t.txt","8-a.txt"]
and want to create a new list with a list of files that start with the prefix list, so the output will be:
fileOutput = ["12-a.csv","12-b.csv","9-t.txt"]

You can use regex and find number and search number in prefixList.
prefixList = ["12","9"]
files = ["12-a.csv","12-b.csv","9-t.txt","8-a.txt"]
new_files = [file
for file in files
if(re.search(r'\d+', file).group(0) in prefixList)]
print(new_files)
Output:
['12-a.csv', '12-b.csv', '9-t.txt']

Related

How do I get all first elements of python dictionary? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I used dictionaries the first time and I can't figure out how to get all first elements of a dictionary. The picture shows an example of my problem. I want to get the brand names, not "brand0, brand1" etc.
thisdict = {
"brand0": ("Ford", "green_car"),
"brand1": ("Audi", "yellow_car"),
"brand2": ("Porsche", "red_car")
}
You can use several aproaches to this problem but the easiest is probably this
firstItems = [value[0] for value in thisdict.values()]
this works the same as
firstItems = []
for value in thisdict.values():
firstItems.append(value[0])

how remove sub path from csv file [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
in 'id_path' in CSV file i want remove subpath from it such as
dataframe of csv file
i want remove all path before the image file name
./input/skin-cancer-malignant-vs-benign/data/test/benign/454.jpg
./input/skin-cancer-malignant-vs-benign/data/test/benign/90.jpg
./input/skin-cancer-malignant-vs-benign/data/test/benign/147.jpg
./input/skin-cancer-malignant-vs-benign/data/test/malignant/771.jpg
./input/skin-cancer-malignant-vs-benign/data/test/malignant/208.jpg
./input/skin-cancer-malignant-vs-benign/data/test/malignant/1383.jpg
./input/skin-cancer-malignant-vs-benign/data/test/malignant/1354.jpg
the output should be
454.jpg
90.jpg
147.jpg
771.jpg
208.jpg
1383.jpg
1354.jpg
rsplit() splits the data from the right side of the string and 1 is way of saying python to stop after first split.
txt = "./input/skin-cancer-malignant-vs-benign/data/test/benign/454.jpg"
x = txt.rsplit("/",1)
#your answer
print(x[1])
on your dataframe you could do something like:
train_df['id_path'] = train_df['id_path'].apply(lambda x: x.rsplit('/',1)[1])
Using str.replace:
df["filename"] = df["path"].str.replace(r'^.*/', '')
We could also use str.extract here:
df["filename"] = df["path"].str.extract(r'([^/]+\.\S+$)')

Delete all item contains word with regex [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
List = ['aleksandre', 'shopify-ecommerce', 'php-ecommerce', 'html-code', 'css-code', 'sultan', 'november']
New List = ['aleksandre', 'sultan', 'november']
How I can delete item contains 'ecommerce' and 'code' from list?
I try to delete with regex but i cant.
try this code:
List = ['aleksandre', 'shopify-ecommerce', 'php-ecommerce', 'html-code', 'css-code',
'sultan', 'november']
a = [ x for x in List if "ecommerce" not in x and "code" not in x]
print(a)
output is:
['aleksandre', 'sultan', 'november']

Trying to exctract a char from a list [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
So I have a list of chars like
nodeList = ['A','C','E','G']
and I want to extract the A. So I found the list[number] method for extracting from a list. But when I put
node = nodeList[0]
I get an error saying that "'dict_keys' object does not support indexing." So how can I work around this? Thanks.
As stated in the comments, nodeList is not actually a list, but a dict_keys object. Before trying to index it, you may simply convert it to a list:
nodeList = list(nodeList)
node = nodeList[0]

Read 2d array and list result in Python [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have following 2D array
name_list = [['Orange', '5'],['Mango','6'],['Banana','3']]
I want to get each fruit name alone with its count and print it using a python code. So how do I read above array to extract the data (inside for loop)
I need print out as
Name:Orange<br/>
Count:5<br/>
Name:Mango<br/>
Count:6<br/>
Name:Banana<br/>
Count:3<br/>
You can unpack your list like this:
for name, amount in name_list:
print("Name:{}".format(name))
print("Count:{}".format(amount))
Try this:
name_list = [['Orange', '5'],['Mango','6'],['Banana','3']]
for item in name_list:
print("Name: {}".format(item[0]))
print("Count: {}".format(item[1]))

Categories

Resources