I have a below mentioned list:
a= ['1234,5678\n','90123,45678\n']
The expected output I'm working towards is this:
op = [['1234','5678'],['90123','45678']]
Basically a list of lists with individual elements referring to a particular column.
Using the below mentioned code i get the following output:
a = ['1234,5678\n','90123,45678\n']
new_list = []
for element in a:
#remove new lines
new_list.append(element.splitlines())
print(new_list)
output:[['1234,5678'], ['90123,45678']]
Any direction regarding this would be much appreciated.
Check this:
a= ['1234,5678\n','90123,45678\n']
a = ['1234,5678\n','90123,45678\n']
new_list = []
for element in a:
#remove new lines
new_list.append(element.strip("\n").split(","))
print(new_list)
Try this:
a = [i.strip("\n").split(",") for i in a]
Since the strings in your input list appears to follow the CSV format, you can use csv.reader to parse them:
import csv
list(csv.reader(a))
This returns:
[['1234', '5678'], ['90123', '45678']]
Related
Have got a set() like
item = {'Apple','Lemon'}
and a string flow='Mango'
need to combine both to form a list like below
result = ['Mango','Apple','Lemon']
tried the code result = [flow ,item] which doesn't work out.
Help me through this. Thanks!
You can unpack the set into a new list that includes flow:
result = [flow, *item]
Add item to the set, conver the set to a list:
item.add(flow)
list(item)
# ['Apple', 'Mango', 'Lemon']
Or convert the set to a list and concatenate it with a list of flow:
[flow] + list(item)
# ['Mango', 'Apple', 'Lemon']
The second approach preserves the original set.
i grabed some data and stuff with my crawler and now i have to analyse them.
I got a list with different links of some pictures, but i just want to save the ones without "/Thumbnails"
pictures = ['Media/Shop/922180cruv.jpg', 'Media/Shop/922180cruvdet.jpg', 'Media/Shop/Thumbnails/922180cruvx320x240.jpg', 'Media/Shop/Thumbnails/922180cruvdetx320x240.jpg']
Is there a way to say "if string got /Thumbnails", remove all from the list?.
I got a csv with ~25000 list in separate columns.
Already tried with remove() and indexing, but my lists are different, so the index for the first list wont fit to the second list for example
Use filtering list comprehension:
pictures = ['Media/Shop/922180cruv.jpg', 'Media/Shop/922180cruvdet.jpg', 'Media/Shop/Thumbnails/922180cruvx320x240.jpg', 'Media/Shop/Thumbnails/922180cruvdetx320x240.jpg']
pictures = [p for p in pictures if not "/Thumbnails/" in p]
print(pictures)
Prints:
['Media/Shop/922180cruv.jpg', 'Media/Shop/922180cruvdet.jpg']
You can user split and join feature of python as below.
pictures = ['Media/Shop/922180cruv.jpg', 'Media/Shop/922180cruvdet.jpg',
'Media/Shop/Thumbnails/922180cruvx320x240.jpg',
'Media/Shop/Thumbnails/922180cruvdetx320x240.jpg']
pictures2= []
for pic in pictures:
if "Thumbnails" in pic:
pic_list = pic.split("Thumbnails/")
pic = "".join(pic_list)
print(pic)
pictures2.append(pic)
else:
pictures2.append(pic)
print(pictures2)
output
['Media/Shop/922180cruv.jpg', 'Media/Shop/922180cruvdet.jpg', 'Media/Shop/922180cruvx320x240.jpg', 'Media/Shop/922180cruvdetx320x240.jpg']
You can iterate through the list and check if each string contains the substring /Thumbnail
result = []
pictures = ['Media/Shop/922180cruv.jpg', 'Media/Shop/922180cruvdet.jpg',
'Media/Shop/Thumbnails/922180cruvx320x240.jpg',
'Media/Shop/Thumbnails/922180cruvdetx320x240.jpg']
for picture in pictures:
if picture.find('/Thumbnails') == -1:
result.append(picture)
Output:
['Media/Shop/922180cruv.jpg', 'Media/Shop/922180cruvdet.jpg']
Demo: https://onlinegdb.com/rkAo1EL4w
I have an example situation where I have a list as follows:
test = ['a-nyc','a-chi','b-sf','c-dal','a-phx','c-la']
the items in this list are naturally ordered in some way, and the objective is to keep the first encountered value for each prefix, e.g. the desired result is a list as follows:
['a-nyc', 'b-sf', 'c-dal']
is there a handy way of doing this?
looks like this can be done this way:
newl = []
prel = []
for i in range(len(test)):
if test[i].split('-')[0] not in prel:
newl.append(test[i])
else:
pass
prel.append(test[i].split('-')[0])
but not sure if there is a more pythonic solution
Yes, you can try like following also:
test = ['a-nyc','a-chi','b-sf','c-dal','a-phx','c-la']
prefix = []
newlist = []
for i in test:
if i.split('-')[0] not in prefix:
prefix.append(i.split('-')[0])
newlist.append(i)
print(newlist)
In this, if any query then let me know.
Thank you.
I'm totally new to python. I have this code:
import requests
won = 'https://api.pipedrive.com/v1/deals?status=won&start=0&api_token=xxxx'
json_data = requests.get(won).json()
deal_name = json_data ['data'][0]['title']
print(deal_name)
It prints the first title for me, but I would like it to loop through all titles in the json. But I can't figure out how. Can anyone guide me in the right direction?
You want to read up on dictionaries and lists. It seems like your json_data["data"] contains a list, so:
Seeing you wrote this:
deal_name = json_data ['data'][0]['title']
print(deal_name)
What you are looking for is:
for i in range(len(json_data["data"])):
print(json_data["data"][i]["title"])
Print it with a for loop
1. for item in json_data['data']: will take each element in the list json_data['data']
2. Then we print the title property of the object using the line print(item['title'])
Code:
import requests
won = 'https://api.pipedrive.com/v1/deals?status=won&start=0&api_token=xxxx'
json_data = requests.get(won).json()
for item in json_data['data']:
print(item['title'])
If you are ok with printing the titles as a list you can use List Comprehensions, Please refer the link in references to learn more.
print([x['title'] for x in json_data['data']])
References:
Python Loops
Python Lists
Python Comprehensions
I'm new to programming and I'm having some trouble with my code. I need to remove the first element in a list without using any of the mentioned methods. What I have so far:
user_string = str(input("Enter a one-character string"))
user_string = user_string.lower()
user_list = [1,2,3]
def choiceN(user_list): #goes to this function if the user enters n as user_string
temp_list = []
for i in range(1, len(user_list)):
temp_list.append(user_list[i])
return temp_list
I don't see where this is going wrong. As I'm new to programming, any explanations would also be greatly appreciated. Thank you.
Make a copy of the list for only the 2nd element to last element and delete the first list.