I have a list that contains duplicates, some are exactly the same while others have just a prefix at the beginning.
For example the following entries refer to the same entry:
'user01', 'aa-user01', 'xyz-user01'
I succeeded to remove the entries that are exactly the same but not the ones with a prefix:
list = ['user01','user01', 'aa-user01','user02','user02', 'user03', 'xyz-user02']
list2 = []
for i in range(0, len(list)):
if list[i] not in list2:
list2.append(list[i])
print(list2)
I get this:
['user01', 'aa-user01', 'user02', 'user03', 'xyz-user02']
The resulat that i want:
['user01', 'user02', 'user03']
The simplest fix would only consider the token after the "-":
for x in list:
user = x.rsplit("-", 1)[-1]
if user not in list2:
list2.append(user)
Note: you should not shadow built-in names like list. Also, contains-checks are expensive for lists, so if you have lots of data, you should consider using a set or dict.
s = set(x.rsplit("-", 1)[-1] for x in lst)
Docs:
str.rsplit
for i in range(0, len(list)):
if list[i][list[i].find("user"):] not in list2:
list2.append(list[i])
list1 = ['user01','user01', 'aa-user01','user02','user02', 'user03', 'xyz-user02']
list2 = []
for i in range(0, len(list1)):
if list1[i] not in list2:
list2.append(list1[i])
list2 = list(dict.fromkeys(list2))
print(list2)
print(list2)
One way to do this would be to first remove the prefix using a list comprehension;
Remove prefix
list = ['user01','user01', 'aa-user01','user02','user02', 'user03', 'xyz-user02']
list = [i.split('-', 1)[-1] for i in l]
list2 = []
for i in range(0, len(list)):
if list[i] not in list2:
list2.append(list[i])
print(list2)
You can go with a short list comprehension, including:
lower() -> Ignore case typing (remove if not needed)
split() -> Removes the prefix
set() -> Removes duplicates
Your result will be set([x.lower().split('-', 1)[-1] for x in test_list]) while test_list is your list.
my_list = ['user01','user01', 'aa-user01','user02','user02', 'user03', 'xyz-user02']
new_list = [j.split('-')[1] if len(j.split('-'))==2 else j for j in my_list]
['user01', 'user01', 'user01', 'user02', 'user02', 'user03', 'user02']#value of new_list
set(new_list)#output unique values
{'user01', 'user02', 'user03'}
Related
This question already has answers here:
How to add an integer to each element in a list?
(12 answers)
Closed last month.
I would like to get such a result [2,4,5,6,7], how can do that? Now i get this: [7]
list1 = [1,3,4,5,6]
for i in range(len(list1)):
l = []
# list1[i] = list1[i] + 1
s = list1[i] + 1
l.append(s)
print(l)
you can use list comprehension
list1 = [1,3,4,5,6]
newlist = [x+1 for x in list1]
print(newlist)
If you want to use a loop, you need to put l = [] ahead of it - otherwise you're reintializing it as an empty list at each loop iteration,
list1 = [1,3,4,5,6]
l = []
for el in list1:
l.append(el + 1)
print(l)
Notice that this code directly loops through elements of list1. This is a preferred way for lists and any other iterables. I recommend you use it whenever practical/possible.
As an alternative to list comprehensions and loops, you can also use a map with a lambda,
list1 = [1,3,4,5,6]
new_list = list(map(lambda x: x+1, list1))
print(new_list)
Note, however, that in most situations, including this one, list comprehension is a better choice. For reasons and details, look up this post.
This will work:
list1 = [1,3,4,5,6]
for i in range(len(list1)):
list1[i]+=1
print(list1)
I have a list of like below, some of them are prefixed with "abc_" and some of them are not.
What would be the effective way to prefix the ones that do not have the prefix?
(Basically, I need all of them to have the prefix "abc_")
my_list = ['abc_apple','abc_orange','cherry','abc_berry','banana']
Required output:
my_list = ['abc_apple','abc_orange','abc_cherry','abc_berry','abc_banana']
Is it Possible to do it using list comprehension?
Don't name lists with a Python Keyword. list is a keyword in Python. You can use list comprehension to do it using .startswith():
list1 = ['abc_apple','abc_orange','cherry','abc_berry','banana']
list1 = ['abc_'+i if not i.startswith('abc_') else i for i in list1]
print(list1)
Output:
['abc_apple', 'abc_orange', 'abc_cherry', 'abc_berry', 'abc_banana']
Just following if/else in a list comprehension you could do something like this:
my_list = ['abc_apple','abc_orange','cherry','abc_berry','banana']
my_list = [f"abc_{word}" if not word.startswith("abc_") else word for word in my_list]
print(my_list)
Output:
['abc_apple', 'abc_orange', 'abc_cherry', 'abc_berry', 'abc_banana']
list = ['abc_apple','abc_orange','cherry','abc_berry','banana']
for i in range(len(list)):
if 'abc_' in list[i]:
pass
else:
list[i] = 'abc_' + list[i]
list
output:
['abc_apple', 'abc_orange', 'abc_cherry', 'abc_berry', 'abc_banana']
OR
list = ['abc_apple','abc_orange','cherry','abc_berry','banana']
for i in range(len(list)):
if 'abc_' not in list[i]:
list[i] = 'abc_' + list[i]
list
OR Better answer
list = ['abc_apple','abc_orange','cherry','abc_berry','banana']
for i in range(len(list)):
if list[i].startswith('abc_'):
pass
else:
list[i] = 'abc_' + list[i]
list
Try method map to make an iterator that computes the function using arguments from each of the iterables.
>>> lst = ['abc_apple','abc_orange','cherry','abc_berry','banana']
>>> result = list(map(lambda x:x if x.startswith('abc_') else 'abc_'+x, lst))
>>>
>>> result
['abc_apple', 'abc_orange', 'abc_cherry', 'abc_berry', 'abc_banana']
list1 = ['a','b','c','d']
list2 = [1,0,1,0]
Given two lists like the above, I would like to obtain a third list whose values are ['a','c']
In other words, I'd like the target list to be the values from list1 where the corresponding element in list2 is 1.
As noted in the comments:
[i for i, j in zip(list1, list2) if j] would work.
Alternatively, if you were looking for something not so advanced:
list3 = []
for i in range(len(list1)):
if list2[i] == 1:
list3.append(list1[i])
Use enumerate function on second list to include index, which can be used for the first list.
[list1[i] for i, item in enumerate(list2) if item]
Generators can be nice if you have very long lists:
def filterfunc(a, b, keyvalue=1):
return (x for i, x in enumerate(a) if b[i] == keyvalue)
To get the whole sequence:
list(filterfunc(a, b))
I have two text files that I have turned into lists. List1 has lines that look like this:
'U|blah|USAA032812134||blah|blah|25|USAA032812134|blah|A||4||blah|2019-05-28 12:54:59|blah|123456||blah'
list2 has lines that look like this:
['smuspid\n', 'USAA032367605\n', 'USAA032367776\n', 'USAA044754265\n', 'USAA044754267\n']
I want to return every line in list1 that has a match in list2. I've tried using regex for this:
found = []
check = re.compile('|'.join(list2))
for elem in list1:
if check.match(elem):
found.append(elem)
but my code above is returning an empty list. Any suggestions?
I guess you can do that without a regular expression:
Method 1
list1 = ['U|blah|USAA032812134||blah|blah|25|USAA032812134|blah|A||4||blah|2019-05-28 12:54:59|blah|123456||blah']
list2 = ['|USAA032812134', '|USAA0328121304', '|USAA032999812134']
found = []
for i in list1:
for j in list2:
if j in i:
found.append(j)
print(found)
Output 1
['|USAA032812134']
Method 2 using List Comprehension
list1 = ['U|blah|USAA032812134||blah|blah|25|USAA032812134|blah|A||4||blah|2019-05-28 12:54:59|blah|123456||blah']
list2 = ['|USAA032812134', '|USAA0328121304', '|USAA032999812134', 'blah']
print([j for i in list1 for j in list2 if j in i])
Output 2
['|USAA032812134', 'blah']
Method 3: strip() for new lines
You can simply strip() and append() to your found list:
list1 = ['U|blah|USAA032812134||blah|blah|25|USAA032812134|blah|A||4||blah|2019-05-28 12:54:59|blah|123456||blah']
list2 = ['smuspid\n', 'USAA032812134\n', 'USAA032367605\n', 'USAA032367776\n',
'USAA044754265\n', 'USAA044754267\n']
found = []
for i in list1:
for j in list2:
if j.strip() in i:
found.append(j.strip())
print(found)
Output 3
['USAA032812134']
For reference this is my code:
list1 = [('10.180.13.101', '10.50.60.30', 'STCMGMTUNIX01')]
list2 = [('0.0.0.0', 'STCMGMTUNIX01')]
for i in list1:
for j in list2:
for k in j:
print (k)
if k.upper() in i:
matching_app.add(j)
for i in matching_app:
print (i)
When I run it, it does not match. This list can contain two or three variables and I need it to add it to the matching_app set if ANY value from list2 = ANY value from list1. It does not work unless the tuples are of equal length.
Any direction to how to resolve this logic error will be appreciated.
You can solve this in a few different ways. Here are two approaches:
Looping:
list1 = [('10.180.13.101', '10.50.60.30', 'STCMGMTUNIX01')]
list2 = [('0.0.0.0', 'STCMGMTUNIX01')]
matches = []
for i in list1[0]:
if i in list2[0]:
matches.append(i)
print(matches)
#['STCMGMTUNIX01']
List Comp with a set
merged = list(list1[0] + list2[0])
matches2 = set([i for i in merged if merged.count(i) > 1])
print(matches2)
#{'STCMGMTUNIX01'}
I'm not clear of what you want to do. You have two lists, each containing exactly one tuple. There also seems to be one missing comma in the first tuple.
For finding an item from a list in another list you can:
list1 = ['10.180.13.101', '10.50.60.30', 'STCMGMTUNIX01']
list2 = ['0.0.0.0', 'STCMGMTUNIX01']
for item in list2:
if item.upper() in list1: # Check if item is in list
print(item, 'found in', list1)
Works the same way with tuples.