Below is the code:
var1 =[]
var5 =[]
var6 = []
var7 = []
var2 = [1, 2, 3, 4, 5,6]
length = len(var2) - 1 # To use it for loop range
var3 = ['a', 'b', 'c', 'd', 'e', 'f']
var4 = ['active','active','active','active','active','active']
for i in (0, length):
with open("listsfile.txt") as f:
for line in f:
if len(line.split(" ")) >= 4: # if strings more than or equal to 4 in line
var5 = line.split(" ")
var5 = filter(None, var5) # filter if any empty element
if len(var5) >=3: # if list has more than or equal to 3 elements
cmp1 = str(var2[i])
cmp2 = str(var5[0])
print cmp1, cmp2
if cmp1 == cmp2: # if first string in a line is matched with list var2 then enter
print "xyz"
var6 = var5[0], var5[1], var5[2]
var7 = var2[i],var3[i],var4[i]
if var6 == var7:
print "Matched"
else:
print "Not Matched"
In above code i am not able to run 6 times in for loop "for i in (0, length):"
Actually I have to run var2 list length times(from 0 it should begin). But it is running only one time.
Please help.
For python 3 "range" seems to do the trick. Also: var5 is now a filter, needs to be converted to list using list().
var1 =[]
var5 =[]
var6 = []
var7 = []
var2 = [1, 2, 3, 4, 5,6]
length = len(var2) - 1 # To use it for loop range
var3 = ['a', 'b', 'c', 'd', 'e', 'f']
var4 = ['active','active','active','active','active','active']
for i in range(0, length):
with open("listsfile.txt") as f:
for line in f:
if len(line.split(" ")) >= 4: # if strings more than or equal to 4 in line
var5 = line.split(" ")
var5 = list(filter(None, var5)) # filter if any empty element
if len(var5) >=3: # if list has more than or equal to 3 elements
cmp1 = str(var2[i])
cmp2 = str(var5[0])
print (cmp1, cmp2)
if cmp1 == cmp2: # if first string in a line is matched with list var2 then enter
print ("xyz")
var6 = var5[0], var5[1], var5[2]
var7 = var2[i],var3[i],var4[i]
if var6 == var7:
print ("Matched")
else:
print ("Not Matched")
Related
I have a list that will sometimes hold a single value, sometimes 2 or 3 values. I am assigning each of the values to a variable. If there is no value for the assigned position of the list, I want it to be a empty variable. Is there any way I can make this more efficient?
split = line.split(',')
try:
var1 = split[0]
except IndexError:
var1 = None
try:
var2 = split[1]
except IndexError:
var2 = None
try:
var3 = split[2]
except IndexError:
var3 = None
split = line.split(',')
variables = [None, None, None]
for i in range(len(split)):
variables[i] = split[i]
var1, var2, var3 = variables
Edit (after #ekhumoro):
split = line.split(',')
variables = [None, None, None]
variables[:len(split)] = split
var1, var2, var3 = variables
You can use generator expression (or list comprehension) with conditional check to see if the index exists in the list. If exists, return element at that index, else return None. Here I am limiting my generator expression to return n (which is 3 in your case) values, and you can unpack and save these values in n variables.
For example:
my_list = [1] # List with just one element
n = 3 # Number of variables in which you want to fill the values
var1, var2, var3 = (my_list[i] if i < len(my_list) else None for i in range(n))
print(var1, var2, var3)
# prints: (1, None, None)
Refer Generator Expressions and List Comprehensions for more details.
Depending on what you're actually trying to accomplish*, it might be better to not use individual variables. For example, you could use dict.get(), which returns None if the given key is not in the dict.
for line in 'a,b,c', 'd,e', 'f':
split = line.split(',')
d = {i: v for i, v in enumerate(split)}
print(d, ' -> ', d.get(0), d.get(1), d.get(2))
Output:
{0: 'a', 1: 'b', 2: 'c'} -> a b c
{0: 'd', 1: 'e'} -> d e None
{0: 'f'} -> f None None
You could also consider using unpacking:
for line in 'a,b,c', 'd,e', 'f':
var1, *others = line.split(',')
print(var1, others)
Output:
a ['b', 'c']
d ['e']
f []
By the way, note that split[0] will always exist, so I'm not sure why you're expecting an IndexError. For example:
>>> ''.split(',')
['']
* See some related ideas at How do I create variable variables? and more broadly, how to avoid the XY problem
This question already has answers here:
Removing duplicates in lists
(56 answers)
Closed 4 months ago.
So I'm trying to make this program that will ask the user for input and store the values in an array / list.
Then when a blank line is entered it will tell the user how many of those values are unique.
I'm building this for real life reasons and not as a problem set.
enter: happy
enter: rofl
enter: happy
enter: mpg8
enter: Cpp
enter: Cpp
enter:
There are 4 unique words!
My code is as follows:
# ask for input
ipta = raw_input("Word: ")
# create list
uniquewords = []
counter = 0
uniquewords.append(ipta)
a = 0 # loop thingy
# while loop to ask for input and append in list
while ipta:
ipta = raw_input("Word: ")
new_words.append(input1)
counter = counter + 1
for p in uniquewords:
..and that's about all I've gotten so far.
I'm not sure how to count the unique number of words in a list?
If someone can post the solution so I can learn from it, or at least show me how it would be great, thanks!
In addition, use collections.Counter to refactor your code:
from collections import Counter
words = ['a', 'b', 'c', 'a']
Counter(words).keys() # equals to list(set(words))
Counter(words).values() # counts the elements' frequency
Output:
['a', 'c', 'b']
[2, 1, 1]
You can use a set to remove duplicates, and then the len function to count the elements in the set:
len(set(new_words))
values, counts = np.unique(words, return_counts=True)
More Detail
import numpy as np
words = ['b', 'a', 'a', 'c', 'c', 'c']
values, counts = np.unique(words, return_counts=True)
The function numpy.unique returns sorted unique elements of the input list together with their counts:
['a', 'b', 'c']
[2, 1, 3]
Use a set:
words = ['a', 'b', 'c', 'a']
unique_words = set(words) # == set(['a', 'b', 'c'])
unique_word_count = len(unique_words) # == 3
Armed with this, your solution could be as simple as:
words = []
ipta = raw_input("Word: ")
while ipta:
words.append(ipta)
ipta = raw_input("Word: ")
unique_word_count = len(set(words))
print "There are %d unique words!" % unique_word_count
aa="XXYYYSBAA"
bb=dict(zip(list(aa),[list(aa).count(i) for i in list(aa)]))
print(bb)
# output:
# {'X': 2, 'Y': 3, 'S': 1, 'B': 1, 'A': 2}
For ndarray there is a numpy method called unique:
np.unique(array_name)
Examples:
>>> np.unique([1, 1, 2, 2, 3, 3])
array([1, 2, 3])
>>> a = np.array([[1, 1], [2, 3]])
>>> np.unique(a)
array([1, 2, 3])
For a Series there is a function call value_counts():
Series_name.value_counts()
If you would like to have a histogram of unique values here's oneliner
import numpy as np
unique_labels, unique_counts = np.unique(labels_list, return_counts=True)
labels_histogram = dict(zip(unique_labels, unique_counts))
How about:
import pandas as pd
#List with all words
words=[]
#Code for adding words
words.append('test')
#When Input equals blank:
pd.Series(words).nunique()
It returns how many unique values are in a list
You can use get method:
lst = ['a', 'b', 'c', 'c', 'c', 'd', 'd']
dictionary = {}
for item in lst:
dictionary[item] = dictionary.get(item, 0) + 1
print(dictionary)
Output:
{'a': 1, 'b': 1, 'c': 3, 'd': 2}
ipta = raw_input("Word: ") ## asks for input
words = [] ## creates list
unique_words = set(words)
Although a set is the easiest way, you could also use a dict and use some_dict.has(key) to populate a dictionary with only unique keys and values.
Assuming you have already populated words[] with input from the user, create a dict mapping the unique words in the list to a number:
word_map = {}
i = 1
for j in range(len(words)):
if not word_map.has_key(words[j]):
word_map[words[j]] = i
i += 1
num_unique_words = len(new_map) # or num_unique_words = i, however you prefer
Other method by using pandas
import pandas as pd
LIST = ["a","a","c","a","a","v","d"]
counts,values = pd.Series(LIST).value_counts().values, pd.Series(LIST).value_counts().index
df_results = pd.DataFrame(list(zip(values,counts)),columns=["value","count"])
You can then export results in any format you want
The following should work. The lambda function filter out the duplicated words.
inputs=[]
input = raw_input("Word: ").strip()
while input:
inputs.append(input)
input = raw_input("Word: ").strip()
uniques=reduce(lambda x,y: ((y in x) and x) or x+[y], inputs, [])
print 'There are', len(uniques), 'unique words'
I'd use a set myself, but here's yet another way:
uniquewords = []
while True:
ipta = raw_input("Word: ")
if ipta == "":
break
if not ipta in uniquewords:
uniquewords.append(ipta)
print "There are", len(uniquewords), "unique words!"
ipta = raw_input("Word: ") ## asks for input
words = [] ## creates list
while ipta: ## while loop to ask for input and append in list
words.append(ipta)
ipta = raw_input("Word: ")
words.append(ipta)
#Create a set, sets do not have repeats
unique_words = set(words)
print "There are " + str(len(unique_words)) + " unique words!"
This is my own version
def unique_elements():
elem_list = []
dict_unique_word = {}
for i in range(5):# say you want to check for unique words from five given words
word_input = input('enter element: ')
elem_list.append(word_input)
if word_input not in dict_unique_word:
dict_unique_word[word_input] = 1
else:
dict_unique_word[word_input] += 1
return elem_list, dict_unique_word
result_1, result_2 = unique_elements()
# result_1 holds the list of all inputted elements
# result_2 contains unique words with their count
print(result_2)
This question already has answers here:
Find all the keys cluster in a list
(2 answers)
Closed 3 years ago.
I have a list of list with strings in it:
list = [["a","b"],["c","d"],["a", "e"],["f","d"],["x","y"]]
Now i want to merge all lists, that have 1 similar item in it like this:
grouped_list = [["a", "b", "e"],["c","d","f"],["x","y"]]
my code is this til now:
list = [["a","b"],["b","c"],["d","e"],["x","y"]]
clist = list.copy()
result = []
counter = 0
del_list = []
def oneofsame(L1, L2):
counter = 0
for i in L1:
for j in L2:
if i == j:
counter += 1
if counter == 0:
return False
else:
return True
for l in list:
try:
del clist[clist.index(l)]
except:
pass
result.append([])
for i in l:
for cl in clist:
if oneofsame(l, cl):
for j in l:
if j not in result[counter]:
result[counter].append(j)
for j in cl:
if j not in result[counter]:
result[counter].append(j)
del_list.append(cl)
else:
result.append(cl)
del_list.append(cl)
for j in del_list:
del clist[clist.index(j)]
del_list = []
counter += 1
del_list = []
cresult = result.copy()
for i in range(len(cresult)-1, 0, -1):
if cresult[i] == []:
del result[i]
print(result)
but this code doesn't merge all of my example input (I can't paste my example input, because its sensitiv data)
Here is a way to do it.
For each pair:
if we find a group that contains one of the values, we append the pair to the group
if we find a second group that contains the other value, we merge the groups.
if we found no matching group, then our pair constitutes a new one.
def group_equals(lst):
groups = []
for pair in lst:
pair = set(pair)
equals_found = 0
for idx, group in enumerate(groups):
if group.intersection(pair):
equals_found += 1
if equals_found == 1:
# We found a first group that contains one of our values,
# we can add our pair to the group
group.update(pair)
first_group = group
elif equals_found == 2:
# We found a second group that contains the other one of
# our values, we merge it with the first one
first_group.update(group)
del groups[idx]
break
# If none of our values was found, we create a new group
if not equals_found:
groups.append(pair)
return [list(sorted(group)) for group in groups]
tests = [ [["a", "b"], ["c", "d"], ["b", "c"]], # all equal
[["a","b"],["c","d"],["a", "e"],["f","d"]],
[["a","b"],["c","d"],["a", "e"],["f","d"],["x","y"]]
]
for lst in tests:
print(group_equals(lst))
# [['a', 'b', 'c', 'd']]
# [['a', 'b', 'e'], ['c', 'd', 'f']]
# [['a', 'b', 'e'], ['c', 'd', 'f'], ['x', 'y']]
I hope my below code will solve your problem:
import itertools
import copy
lista = [["a","b"],["c","d"],["a", "e"],["f","d"],["x","y"]] #[["a","b"],["e","d1"],["a", "e"],["a","d"],["d","y"]]
def grouped_list(lista):
aa = []
bbc = copy.deepcopy(lista)
flag = False
for a, b in itertools.combinations(lista,2):
bb = a+b
if len(set(bb)) < len(bb):
flag = True
cc = list(set(bb))
cc.sort()
if cc not in aa: aa.append(cc)
if a in lista: lista.remove(a)
if b in lista: lista.remove(b)
if lista: aa = aa + lista
if not flag: return bbc
else: return grouped_list(aa)
print ("Grouped list -->", grouped_list(lista))
Feel free to ask/suggest anything in the above code.
I have few global variables and I have a list. Within a function I am using the list and updating the values as below , but the global variables doesn't seem to be updated.
a = "hello"
b ="how"
c = "are you"
data = ([a,"abc","xyz"],[b,"pqr","mno"],[c,"test","quest"])
def checklist():
global data , a, b, c
for values in data:
values[0] = values[1]
checklist()
print a + ":" + b + ":"+ c
Now when i expect the global variables to be updated which is not happening, I still see old variables, could some one explain how to update global variables from the list.
The loop in data changes data's value, which won't change other variable.
When you run values[0] = values[1], it means values[0] repoints to another object, but a will stays the same.
In [52]: a = '12'
In [53]: li = [a, 'b', 'c']
In [54]: id(li[0])
Out[54]: 140264171560632
In [55]: id(a)
Out[55]: 140264171560632
In [56]: li[0] = 'a'
In [57]: li
Out[57]: ['a', 'b', 'c']
In [58]: a
Out[58]: '12'
In [60]: id(li[0])
Out[60]: 140264267728616
In [61]: id(a)
Out[61]: 140264171560632
You intend the values in data to be changes in the for loop?
The reason that's not happening is that you're changing value, not the actual data.
a,b,c = "hello", "how", "are you"
data = ([a,"abc","xyz"],[b,"pqr","mno"],[c,"test","quest"])
def checklist():
global data , a, b, c
for values in data:
values[0] = values[1]
checklist()
print a + ":" + b + ":"+ c
hello:how:are you
print(data)
(['abc', 'abc', 'xyz'], ['pqr', 'pqr', 'mno'], ['test', 'test', 'quest'])
You are wrong if you want do something like this, edit a variable that you inserted in a list.
values[0] = values[1]
Set in the position 0 of values -> values[1]. But no modify a-b-c!!
I'm trying to read a txt that look like:
Animals
Dog
Cat
Bird
Cities
Paris
London
Chicago
into a dictionary. (The first line into a key followed by three lines of values, and so on) It should be looking something like this:
{"Animals":["Dog","Cat","Bird"],"Cities":["Paris","London","Chicago"]
Please help me!
Here is a solution using islice, that helps you select the next n lines to read from a file.
from itertools import islice
dic = {}
with open('file.txt', 'r') as f:
while True:
lines = list(islice(f, 4))
lines = map(str.rstrip, lines)
if not lines:
break
dic[lines[0]] = lines[1:]
print dic
That gives the result you need:
{'Cities': ['Paris', 'London', 'Chicago'], 'Animals': ['Dog', 'Cat', 'Bird']}
More on islice
import itertools
d = {}
with open("C:\path\to\text.txt", "r") as f:
lines = f.readlines()
# Separate keys from values
keys = itertools.islice(lines, 0, None, 4)
first_values = itertools.islice(lines, 1, None, 4)
second_values = itertools.islice(lines, 2, None, 4)
third_values = itertools.islice(lines, 3, None, 4)
# Set keys and values
for key in keys:
key = key.rstrip()
value1 = first_values.next().rstrip()
value2 = second_values.next().rstrip()
value3 = third_values.next().rstrip()
# Create dict
l = [value1, value2, value3]
d[key] = l
Try something like this:
f = open('first.txt').readlines()
f = [i.strip('\n') for i in f]
datafile = [i.split() for i in f]
dct = {}
for i in range(len(datafile)):
if i%4 == 0:
new_lst = datafile[i+1:i+4]
final_list = [b[0] for b in new_lst]
print datafile[i][0]
dct[datafile[i][0]] = final_list
print dct
This should work:
ind = 0
my_dict = {}
my_list = []
for line in myfile:
val = line.rstrip().split()[0]
ind += 1
if ind % 5 == 0:
my_list = []
if ind % 4 == 1:
x = val
else:
my_list.append(val)
if ind % 4 == 0:
my_dict[x] = my_list
print(my_dict)
Here is another possible solution:
fin = open(f, 'rb')
lines = [word.strip('\n') for word in fin.readlines()]
keys = [word.strip('\n') for idx, word in enumerate(fin.readlines()) if idx%4 == 0]
values = [lines[idx+1:idx+4] for idx, _ in enumerate(lines) if idx%4 == 0]
d = dict(zip(keys, values))
{'Animals': ['Dog', 'Cat', 'Bird'], 'Cities': ['Paris', 'London', 'Chicago']}