can i insert of my list with List Comprehensions - python

I want to make an insert on the List Comprehensions.
I can do it?
t = ['test','tes']
x = ['1','2','3','4','5']
t.insert([0,i]for i in x)

If the result you're looking for is something like this
['test', 'tes', [0, 'test'], [0, 'tes']]
The code is below:
t = ['test','tes']
x = ['1','2','3','4','5']
t.extend([[0,i]for i in t])
print t

You can do it in a list comprehension but it ends up doing extra work. Just do it in a for loop:
for thing in x:
t.insert(0, thing)

t = ['test','tes']
x = ['1','2','3','4','5']
out = t + x
your out put will be ['tes','test','1','2','3','4','5','6']
or something alone those lines. python allows you to concatenate strings

The ListItemLabel accepts only strings. I want to give it more than one element in a list, I use in ListItemLabel front str, but it does not do anything.
I do not know if it's the way you think
Python:
result_true = ListProperty([])
extra = self.viewedit.adapter.data
for edit in extra:
if edit['active'] == True:
app.result_true.append(edit['text'])
#app.result_true = (edit['text'])
print app.result_true
Kivy:
ListItemLabel:
text:str(ctx.edit)
Return:
['\xce\xbb\xce\xb9\xce\xb1\xcf\x83\xcf\x84\xce\xae
\xce\xbd\xcf\x84\xce\xbf\xce\xbc\xce\xac\xcf\x84\xce\xb1',
'\xcf\x83\xce\xbf\xcf\x85\xcf\x83\xce\xac\xce\xbc\xce\xb9',
'\xcf\x84\xcf\x85\xcf\x81\xce\xaf']

Related

List comprehension if/else and for iteration order

I have a CSV file that contains book chapters either as single chapters or chapter ranges delimited by commas, e.g. 1,2,4-6,12.
Given this input '1,2,4-6,12', I want a list ['1','2','4','5','6','12'] as output
Something along the lines of:
chps=[str(x) for x in chp_range(entry)) if '-' in entry else entry for entry in chapters.split(',') ]
which doesn't work.
Function chp_range('4-6') returns a range(4,6) object.
I've tried a lot of variations, but still haven't been able to get the order of conditionals and iteration right. How can I get this code to work?
If it has to be a one-liner, this should work:
>>> [str(x) for c in chapters.split(",") for x in range(int(c.split("-")[0]), int(c.split("-")[-1])+1)]
['1', '2', '4', '5', '6', '12']
You can't conditionally nest your comprehensions, so your chp_range function is of little value when used in a comprehension.
Don't try to use list comprehension for the sake of, only if it's actually easier / more readable:
lst = []
for x in s.split(','):
if '-' in x:
start, end = x.split('-')
lst.extend([str(i) for i in range(int(start), int(end)+1)])
else:
lst.append(x)
What you were trying to do:
chps = [str(x)
for entry in chapters.split(',')
for x in (chp_range(entry) if '-' in entry else [entry])]
Try it online!
from itertools import chain
def chp_range(entry):
x, y = map(int, entry.split('-'))
return map(str, range(x, y+1))
chps = [
chp_range(entry) if '-' in entry else entry for entry in chapters.split(',')]
list(chain(*chps))
YOu can use extend for range as follows:
string = '1,2,4-6,12'
string = string.split(',')
chapters = []
for i in string:
if '-' in i:
a,b = i.split('-')
chapters.extend(range(int(a),int(b)+1))
else:
chapters.append(int(i))
print(chapters)
Not a one-liner but if you are interested in a recursive solution. Please check it up.
a = '1,2,4-6,12'
inp = [num if '-' not in num else [str(i) for i in range(int(num[0]),int(num[-1])+1)] for num in a.split(',') ]
ans = []
def flatten(inp,ans):
while inp:
tmp = inp.pop()
if type(tmp) == str:
ans.append(tmp)
else:
flatten(tmp,ans)
return
flatten(inp,ans)
ans[::-1]

Remove file name duplicates in a list

I have a list l:
l = ['Abc.xlsx', 'Wqe.csv', 'Abc.csv', 'Xyz.xlsx']
In this list, I need to remove duplicates without considering the extension. The expected output is below.
l = ['Wqe.csv', 'Abc.csv', 'Xyz.xlsx']
I tried:
l = list(set(x.split('.')[0] for x in l))
But getting only unique filenames without extension
How could I achieve it?
You can use a dictionary comprehension that uses the name part as key and the full file name as the value, exploiting the fact that dict keys must be unique:
>>> list({x.split(".")[0]: x for x in l}.values())
['Abc.csv', 'Wqe.csv', 'Xyz.xlsx']
If the file names can be in more sophisticated formats (such as with directory names, or in the foo.bar.xls format) you should use os.path.splitext:
>>> import os
>>> list({os.path.splitext(x)[0]: x for x in l}.values())
['Abc.csv', 'Wqe.csv', 'Xyz.xlsx']
If the order of the end result doesn't matter, we could split each item on the period. We'll regard the first item in the list as the key and then keep the item if the key is unique.
oldList = l
setKeys = set()
l = []
for item in oldList:
itemKey = item.split(".")[0]
if itemKey in setKeys:
pass
else:
setKeys.add(itemKey)
l.append(item)
Try this
l = ['Abc.xlsx', 'Wqe.csv', 'Abc.csv', 'Xyz.xlsx']
for x in l:
name = x.split('.')[0]
find = 0
for index,d in enumerate(l, start=0):
txt = d.split('.')[0]
if name == txt:
find += 1
if find > 1:
l.pop(index)
print(l)
#Selcuk Definitely the best solution, unfortunately I don't have enough reputation to vote you answer.
But I would rather use el[:el.rfind('.')] as my dictionary key than os.path.splitext(x)[0] in order to handle the case where we have sophisticated formats in the name. that will give something like this:
list({x[:x.rfind('.')]: x for x in l}.values())

How to change list of string to list of elements (numbers)?

When I use:
with open("test.txt") as file
read = csv.reader(file)
for i in read:
print(i)
and I have got something like that:
['[0', ' 0', ' 1', ' 0]']
and I need:
[0, 0, 1, 0]
Any advises please?
ad = ['[0', ' 0', ' 1', ' 0]']
for i in range(len(ad)):
ad[i] = int(ad[i].replace("[", "").replace("]", ""))
print ad
[0, 0, 1, 0]
Normally, the easiest solution would be a list comprehension where new = [int(a) for a in old]. However, in your case, the first and last elements of your list actually have brackets inside of them too.
Instead, you need to do something like:
new = [int("".join(filter(str.isdigit, a))) for a in old]
This is a pretty big one liner so lets break it down.
The list comprehension iterates through each element in your list (I called it old) and names it a
A is passed into the filter command with the function str.isdigit. This basically removes any character that isn't a digit. The issue with this, is that it returns an iterator and not a simple value.
To fix the iterator problem, I wrapped the filter command with a "".join() command to convert it to a simple string value. This string will only have the number.
Finally, we can wrap that entire thing with the int command which will transform your value into an int.
If you don't like the one-liner, it can also be done this way:
new = []
for a in old:
filtered = filter(str.isdigit, a)
num_str = "".join(filtered)
num.append(int(num_str))
It's the same thing but a bit more verbose.
def parseToInt(s):
...: return int((s.replace('[', '')).replace(']', ''))
list(map(lambda x: parseToINt(x), a))

Put a symbol between every letter in a list of strings

How would I put ! after every character in a list
listOne = ["hello","world"]
How do I turn that into:
["h!e!l!l!o","w!o!r!l!d"]
Attempt:
def turn(List):
return [i for i in (list(lambda x: "%s!" % x,listOne))]
turn(listOne)
Returns:
['hello!',"world!"]
Is their another way to do this besides:
def turn(List):
x = ""
for word in words:
for letter in word:
x += "%s!" % letter
return x
turn(listOne)
I'm not a big fan of doing things like that however I do realize that may be more pythonic than what I'm trying to do which is make it as few lines as possible so. Is this possible?
You can easily achieve this with the str.join() method, and list comprehension:
>>> listOne = ['!'.join(i) for i in listOne]
>>> listOne
Output
['h!e!l!l!o', 'w!o!r!l!d']
Alternatively, as abarnert suggested, you can use the bulit-in map function.
>>> listOne = list(map('!'.join, listOne))
>>> listOne
['h!e!l!l!o', 'w!o!r!l!d']
Hope this helps!
listOne = ["hello","world"]
listTwo = ['!'.join([x for x in word]) for word in listOne]
How about this?
["!".join(s) for s in ["hello", "world"]]
Or more specific:
def turn(l):
return ["!".join(s) for s in l]
Edit: Removed wrapping of the string in list() as str.join takes every iterable
object (those that implement __iter__()), and, thus strings as well. Courtesy to #alKid.

Removing character in list of strings

If I have a list of strings such as:
[("aaaa8"),("bb8"),("ccc8"),("dddddd8")...]
What should I do in order to get rid of all the 8s in each string? I tried using strip or replace in a for loop but it doesn't work like it would in a normal string (that not in a list). Does anyone have a suggestion?
Try this:
lst = [("aaaa8"),("bb8"),("ccc8"),("dddddd8")]
print([s.strip('8') for s in lst]) # remove the 8 from the string borders
print([s.replace('8', '') for s in lst]) # remove all the 8s
Beside using loop and for comprehension, you could also use map
lst = [("aaaa8"),("bb8"),("ccc8"),("dddddd8")]
mylst = map(lambda each:each.strip("8"), lst)
print mylst
A faster way is to join the list, replace 8 and split the new string:
mylist = [("aaaa8"),("bb8"),("ccc8"),("dddddd8")]
mylist = ' '.join(mylist).replace('8','').split()
print mylist
mylist = [("aaaa8"),("bb8"),("ccc8"),("dddddd8")]
print mylist
j=0
for i in mylist:
mylist[j]=i.rstrip("8")
j+=1
print mylist
Here's a short one-liner using regular expressions:
print [re.compile(r"8").sub("", m) for m in mylist]
If we separate the regex operations and improve the namings:
pattern = re.compile(r"8") # Create the regular expression to match
res = [pattern.sub("", match) for match in mylist] # Remove match on each element
print res
lst = [("aaaa8"),("bb8"),("ccc8"),("dddddd8")...]
msg = filter(lambda x : x != "8", lst)
print msg
EDIT:
For anyone who came across this post, just for understanding the above removes any elements from the list which are equal to 8.
Supposing we use the above example the first element ("aaaaa8") would not be equal to 8 and so it would be dropped.
To make this (kinda work?) with how the intent of the question was we could perform something similar to this
msg = filter(lambda x: x != "8", map(lambda y: list(y), lst))
I am not in an interpreter at the moment so of course mileage may vary, we may have to index so we do list(y[0]) would be the only modification to the above for this explanation purposes.
What this does is split each element of list up into an array of characters so ("aaaa8") would become ["a", "a", "a", "a", "8"].
This would result in a data type that looks like this
msg = [["a", "a", "a", "a"], ["b", "b"]...]
So finally to wrap that up we would have to map it to bring them all back into the same type roughly
msg = list(map(lambda q: ''.join(q), filter(lambda x: x != "8", map(lambda y: list(y[0]), lst))))
I would absolutely not recommend it, but if you were really wanting to play with map and filter, that would be how I think you could do it with a single line.

Categories

Resources