Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Python

erikpc43
 Share

Hello,

So all that written function does is that it reads two lists and it gives back another list which contains numers that appear in both list compaired only once.
So the function on picture should return (16,2,1). But instead it gives me back list (16,4,2,1). Order in list doesn't matter. What could I try? I would appreciate any help. Thanks

fasdf.PNG.d2aa07cc87eaeddfe5b1979c2c4a422e.PNG

Link to comment
Share on other sites

Link to post
Share on other sites

I'd try putting this on Stack Overflow instead, you'll get better help there.

import shittyTechAdvice as RollTime

 

Link to comment
Share on other sites

Link to post
Share on other sites

Give your variables meaningful names. If you were doing this in a program you'll be maintaining, you'll be ripping your hair out trying to remember what these non-descriptively named variables do.[/rant]

 

So a few things:

  • g != c and c != g are testing the same thing, so this is redundant.
  • The second array being tested in if c not in e and s and if g not in e and t  isn't needed, because the value is always going to be in that array. You only need to test if the value is not in e.

 

If you really want to know what's up:

Spoiler

You need to look ahead to see what numbers both lists have before you can combine them. This method has a problem that it's limited to only looking at single values at a time, so it doesn't know the entire contents of the lists.

 

Link to comment
Share on other sites

Link to post
Share on other sites

two things off the bat:

 

1) please copy paste your code in a formatted code block, like this:

def razlika(s, t):
    e=[]
    for c in s:
        for g in t:
            if g!=c:
                if c not in e and s:
                    e.append(c)
            if c!=g:
                if g not in e and t:
                    e.append(g)
    return e
print(razlika([16, 4, 2], [4,1]))

2) DEFINITELY get into the habit of using more descriptive variable names. s, t, c, g, mean absolutely nothing, while names like first_list instead of s, second_list instead of t, combined_list instead of e, etc. give far more contextual information

 

 

anyway, it's actually far easier using hashes than just iterating through lists. something like:

def razlika(first_list, second_list):
	combined_list = []
	number_dictionary = {}
    
	for elem in first_list:
		numdict[elem] = True
    
	for elem in second_list:
		# if the element exists in the dictionary already, change it's value to False to indicate it's in both
		# else if the element doesn't already exist, add it with the value of True to indicate it's in only one list
    
	for item in number_dictionary:
		# if the items value is True, we know it's in only 1 list, so add it to the combined_list
    
	return combined_list


I didn't write out the full thing since I don't think just giving code solutions is a good way to teach, but hopefully this gives you a good direction to go down. The above, if correctly implemented, provides a list of [16, 2, 1] from the lists [16, 4, 2] and [4,1]

also, your description was a bit confusing. Based on your sample data and sample example, I assume you mean it should produce a list of items that only appear in one list, not both, even though your description says "which contains numbers that appear in both list"

Gaming build:

CPU: i7-7700k (5.0ghz, 1.312v)

GPU(s): Asus Strix 1080ti OC (~2063mhz)

Memory: 32GB (4x8) DDR4 G.Skill TridentZ RGB 3000mhz

Motherboard: Asus Prime z270-AR

PSU: Seasonic Prime Titanium 850W

Cooler: Custom water loop (420mm rad + 360mm rad)

Case: Be quiet! Dark base pro 900 (silver)
Primary storage: Samsung 960 evo m.2 SSD (500gb)

Secondary storage: Samsung 850 evo SSD (250gb)

 

Server build:

OS: Ubuntu server 16.04 LTS (though will probably upgrade to 17.04 for better ryzen support)

CPU: Ryzen R7 1700x

Memory: Ballistix Sport LT 16GB

Motherboard: Asrock B350 m4 pro

PSU: Corsair CX550M

Cooler: Cooler master hyper 212 evo

Storage: 2TB WD Red x1, 128gb OCZ SSD for OS

Case: HAF 932 adv

 

Link to comment
Share on other sites

Link to post
Share on other sites

this could work, btw i havent tested it since i cant be bothered going into python, so lets hope i didnt write down something small and incorrect

Quote

def comparisonland(listA,listB):

    temp=[]

    for i in listA:

        if i in listB:

            temp.append(i)

    return temp

 

Sadness is the one true emotion, and happiness, well, that's just a lie, sadness is all many of us feel, and is all we need to feel, because having it any other way, would just be wrong, why be happy when you can just be miserable like myself. 

Link to comment
Share on other sites

Link to post
Share on other sites

Using list comprehension:

def func(s,t):
	return [i for i in s+t if (s+t).count(i) == 1]
Link to comment
Share on other sites

Link to post
Share on other sites

5 hours ago, elpiop said:

Using list comprehension:


def func(s,t):
	return [i for i in s+t if (s+t).count(i) == 1]

I do like my one-liners but that is far harder to read and not very "pythonic".

here is my solution

 

def razlika(s, t):
    e=[]
    for c in s:
      if c not in t:
        e.append(c)
    for d in t:
      if d not in s:
        e.append(d)
      
    return e
print(razlika([16, 4, 2], [4,1]))

 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, fizzlesticks said:

list(set(s) ^ set(t))

 

Totally forgot about sets :S

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share


×